0

So I noticed that on some devices my captured image (bitmap) is weirdly rotated - to 90 degrees usually. I did create a function that can rotate it correctly, but the problem now is, that sometimes the image comes in (is captured) perfectly okay. Is there a way I can find out if captured image is rotated (it's orientation)?

I found a code fragment like this:

  try {
     ExifInterface exif = new ExifInterface(filename); 
     int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                      ExifInterface.ORIENTATION_NORMAL);
     int rotate = 0;
     switch(orientation) {
        case  ExifInterface.ORIENTATION_ROTATE_270:
             rotate-=90;break;
        case  ExifInterface.ORIENTATION_ROTATE_180:
             rotate-=90;break;
        case  ExifInterface.ORIENTATION_ROTATE_90:
             rotate-=90;break;
        }
      ...    
    }

But ExifInterface exif = new ExifInterface(filename); expects a file or file uri to be created. Does this mean I have to change my bitmap to file first and then use this approach or is there other ways to fight with this thing?

P.S. I'm using Android's Camera X

MaaAn13
  • 264
  • 5
  • 24
  • 54

3 Answers3

0

It is recommended to use the AndroidX ExifInterface Library. You can use the ExifInterface(InputStream) if the captured image is Jpeg by wrapping the buffer with InputStream.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
0

The sensor orientation is different across devices. CameraX respects that and returns the image with a rotation value in the callback, which can be used to rotate it upright. https://developer.android.com/reference/androidx/camera/core/ImageCapture.OnImageCapturedListener.html#onCaptureSuccess(androidx.camera.core.ImageProxy,%20int)

0

you can try this

        Bitmap rotateBitmap(String src, Bitmap bitmap) {
        DebugLog.write();

        int orientation;

        try {
            orientation = getExifOrientation(src);
        } catch (IOException e) {
            e.printStackTrace();
            return bitmap;
        }

        Matrix matrix = new Matrix();

        switch (orientation) {
            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;
            case ExifInterface.ORIENTATION_NORMAL:
            default:
                return bitmap;
        }

        try {
            Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle(); //wht
            return oriented;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    private int getExifOrientation(String src) throws IOException {
        DebugLog.write();
        ExifInterface exifInterface = new ExifInterface(src);
        return exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    }
Ali Korkmaz
  • 324
  • 2
  • 4