0

I am using Camera intent to capture a photo on android, when intent from onActivityResult returns bitmap it has wrong orientation on some phones. I know there are ways to fix this,but all the solutions I have seen talk about image stored in file. What I am retrieving from intent is directly bitmap image. I want to know how I can get exif data of a bitmap and then correct its orientation. I repeat I have seen answers which deal with file and not bitmap, so please consider this before down voting.

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
      startActivityForResult(takePictureIntent, Constants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
 }

And result is as follows

Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");

How to get orientation and rotate it.

vishal dharankar
  • 7,536
  • 7
  • 57
  • 93

1 Answers1

0

UPDATE

Exif is a file format that inserts some information data to JPEG. https://www.media.mit.edu/pia/Research/deepview/exif.html

And Bitmap is data structior data holds row pixel data, no exif info.
So I think it is impossible to get exif info from Bitmap.
There is no method to get exif info.
https://developer.android.com/reference/android/graphics/Bitmap.html

ORIGINAL

I agree with @DzMobNadjib . I think the info of rotation is only in exif. To take exif, I recommend you to take following steps.

1. Start camera activity with file path.

See [Save the Full-size Photo] capture of this document.

You can start the camera activity with file path.The camera activity will save the image to the file path that you passed.

2. In 'onActivityResult', Follow this answer (as @DzMobNadjib suggested)

Your code will be like this:
(Sorry I'm not tested. Please read carefuly and follow the above answer)

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == Constants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            Uri uri = data.getData();
            Bitmap bitmap = getAdjustedBitmap(uri);
        }
    }
}

private Bitmap getAdjustedBitmap(Uri uri) {
    FileInputStream is = null;
    try {
        ExifInterface exif = new ExifInterface(uri.getPath());

        int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        int rotationInDegrees = exifToDegrees(rotation);

        Matrix matrix = new Matrix();
        if (rotation != 0f) {
            matrix.preRotate(rotationInDegrees);
        }

        is = new FileInputStream(new File(uri.getPath()));
        Bitmap sourceBitmap = BitmapFactory.decodeStream(is);

        int width = sourceBitmap.getWidth();
        int height = sourceBitmap.getHeight();

        return Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}

private static int exifToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {  return 180; }
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {  return 270; }
    return 0;
}
Community
  • 1
  • 1
nshmura
  • 5,940
  • 3
  • 27
  • 46