Camera capture orientation on samsung devices in android

后端 未结 3 1204
既然无缘
既然无缘 2020-12-05 15:42

I am creating a camera app. The image when captured is shown in the grid view. Now, the code is working completely fine on all the devices except for samsung devices.

<
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 16:05

    First you need to get the original file orientation--

          try {
                            ExifInterface exif = new ExifInterface("File AbsolutePath");
                            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
                            Bitmap bm = rotateBitmap("Old Bitmap", orientation);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
    

    You need to write a method which return the Bitmap after rotate it to the right direction.

    public Bitmap rotateBitmap(Bitmap bitmap, int orientation) throws IOException {
    
            Matrix matrix = new Matrix();
            switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
            default:
                return bitmap;
            }
            try {
                Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                bitmap.recycle();
                return bmRotated;
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
                return null;
            }
        }
    

提交回复
热议问题