图像在捕获后的某些设备中模糊无法找到合适的解决方案

时间:2016-08-20 08:09:44

标签: android bitmap android-camera

我创建自定义相机应用。在某些设备上工作得很完美。像三星音符4

我在( YUVA 5010A )图像模糊时遇到问题,当我在相机中看到它看起来像这样

我查看了此链接,但在我的情况下无法正常工作LINK请帮帮我

enter image description here

使用自定义相机捕获后看起来像这样

enter image description here

这是我的代码

gh-pages

我的Surfaceview类

 private class SavePhotoTask extends AsyncTask<Void, Void, String> {

        private String albumId;
        private String strDate, photo_cache1, photo_cache2;
        private File file, photo, file1, photo1;
        private byte[] data;
        private int angleToRotate;
        private boolean isShareEnable = false;
        private int orientation1;

        private SavePhotoTask(String albumId, byte[] data, int angleToRotate, boolean isShareEnable, int oritation) {
            this.albumId = albumId;
            this.data = data;
            this.angleToRotate = angleToRotate;
            this.isShareEnable = isShareEnable;
            this.orientation1 = oritation;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            final Date date = new Date();
            final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
            strDate = sdf.format(date);

            final File cacheDir = new File(getActivity().getExternalCacheDir().toString() + "/Album/");
            final File cacheDir1 = new File(getActivity().getExternalCacheDir().toString() + "/Albums/");
            if (!cacheDir.exists()) {
                cacheDir.mkdir();
            }
            if (!cacheDir1.exists()) {
                cacheDir1.mkdir();
            }

//            file = new File(Environment.getExternalStorageDirectory()
//                    + "/Pictures/" + getString(R.string.app_name));
            file = new File(cacheDir.getAbsolutePath() + "/" + albumId);
            if (!file.exists()) file.mkdir();
            file1 = new File(cacheDir1.getAbsolutePath() + "/" + albumId);
            if (!file1.exists()) file1.mkdir();


        }


        @Override
        protected String doInBackground(Void... str) {

            photo = new File(file.toString(), strDate + ".jpg");
            photo1 = new File(file1.toString(), strDate + ".jpg");

            photo_cache1 = photo.getAbsolutePath();
            photo_cache2 = photo1.getAbsolutePath();

            if (photo.exists()) {
                photo.delete();
            }
            if (photo1.exists()) {
                photo1.delete();
            }

            try {
                final String pictureFile = photo.getAbsolutePath();

                final BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
                sizeOptions.inJustDecodeBounds = false;
                sizeOptions.inScaled = false;
                sizeOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
                BitmapFactory.decodeByteArray(data, 0, data.length, sizeOptions);
                Log.d("TAG", "Bitmap is " + sizeOptions.outWidth + "x"
                        + sizeOptions.outHeight);

//                final float widthSampling = sizeOptions.outWidth / deviceWidth;
//                sizeOptions.inJustDecodeBounds = false;
//                sizeOptions.inSampleSize = (int) widthSampling;
                FileOutputStream fos;
                Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length, sizeOptions);
//                realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
//                fos.close();

//                realImage = getRotatedImage(photo.getAbsolutePath(),currentCameraId);

                Log.d("TAG", "Oriantaion :" + orientation1);

                int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
                Log.d("TAG", "Oriantaion :" + rotation);

                fos = new FileOutputStream(pictureFile);
                if (orientation1 == 0) {
                    realImage = rotate(realImage, angleToRotate);
                    Log.e(getClass().getSimpleName(), "portrait");
                } else if (orientation1 == 1) {
                    realImage = rotate(realImage, 180);
                    Log.e(getClass().getSimpleName(), "landscape");
                } else if (orientation1 == 2) {
                    realImage = rotate(realImage, 0);
                    Log.e(getClass().getSimpleName(), "landscape");
                } else if (orientation1 == 3) {
                    realImage = rotate(realImage, 270);
                    Log.e(getClass().getSimpleName(), "portrait");
                }
                image_to_camera_folder = realImage;
                realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.close();


                if (!realImage.isRecycled())
                    realImage.recycle();

            } catch (FileNotFoundException e) {
                Log.d("Info", "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d("TAG", "Error accessing file: " + e.getMessage());
            }

            try {
                final String pictureFile = photo1.getPath();

                final BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
                sizeOptions.inJustDecodeBounds = false;
                sizeOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
                /*BitmapFactory.decodeByteArray(data, 0, data.length, sizeOptions);
                Log.d("TAG", "Bitmap is " + sizeOptions.outWidth + "x"
                        + sizeOptions.outHeight);

                final float widthSampling = sizeOptions.outWidth / deviceWidth;
                sizeOptions.inJustDecodeBounds = false;
                sizeOptions.inSampleSize = (int) widthSampling;
*/
                FileOutputStream fos = new FileOutputStream(pictureFile);
                Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length, sizeOptions);
//                realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
//                fos.close();

//                realImage = getRotatedImage(photo1.getAbsolutePath(),currentCameraId);
                fos = new FileOutputStream(pictureFile);
                if (orientation1 == 0) {
                    realImage = rotate(realImage, angleToRotate);
                    Log.e(getClass().getSimpleName(), "portrait");
                } else if (orientation1 == 1) {
                    realImage = rotate(realImage, 180);
                    Log.e(getClass().getSimpleName(), "landscape");
                } else if (orientation1 == 2) {
                    realImage = rotate(realImage, 0);
                    Log.e(getClass().getSimpleName(), "landscape");
                } else if (orientation1 == 3) {
                    realImage = rotate(realImage, 270);
                    Log.e(getClass().getSimpleName(), "portrait");
                }
                realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.close();
                if (!realImage.isRecycled())
                    realImage.recycle();

            } catch (FileNotFoundException e) {
                Log.d("Info", "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d("TAG", "Error accessing file: " + e.getMessage());
            }

            return (null);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);


            if (isShareEnable) {
                if (getActivity() != null && Util.isOnline(getActivity())) {
                    String userId = Util.ReadSharePrefrence(getActivity(), Constant.SHRED_PR.KEY_USERID);
                    /*Intent intent = new Intent(getActivity(), UploadPhotoIntentService.class);
                    intent.putExtra(Constant.SHRED_PR.KEY_USERID, userId);
                    intent.putExtra(Constant.SHRED_PR.KEY_Current_AlbumID, albumId);
                    intent.putExtra(Constant.SHRED_PR.KEY_Photo_Name, photo.getAbsolutePath());
                    intent.putExtra(Constant.SHRED_PR.KEY_Data, CaptureActivity.class.getSimpleName());
                    getActivity().startService(intent);*/
                    MyApp.uploadingImage.add(photo.getAbsolutePath());

                    uploadPhotoTask task = new uploadPhotoTask(getActivity(), userId, albumId, photo.getAbsolutePath(), CaptureActivity.class.getSimpleName(), photo_cache1, photo_cache2);
                    task.execute();
                }
            }
            // save image to camera folder sdcard
            try {
                saveImageToCameraFolder(image_to_camera_folder, photo.getAbsolutePath());

                Log.e("SAVE", "Image Save To Camera Folder");
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (getActivity() != null) {
                Util.WriteSharePrefrence(getActivity(), Constant.SHRED_PR.KEY_RELOAD_Home, "5");
                Toast.makeText(getActivity(), "photo captured", Toast.LENGTH_SHORT).show();
                if (!isChecked) {
                    photoCnt++;
//                    tvPhotoCount.setText("" + photoCnt);
                    Util.WriteSharePrefrence(getActivity(), Constant.SHRED_PR.KEY_GET_PHOTO_COUNT, "" + photoCnt);
                }

            }
            HomeFragment.getInstance().reloadOfflinePhoto();
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为您应该应用oriontation 0.0并查看它是否解决了问题