Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

android - Getting rotation from ExifInterface always returns 0

I'm passing a bitmap via bundle on onActivityResult from a camera.

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "picture");
mCapturedImageURI =     getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);

startActivityForResult(intent, REQUEST_TAKE_PHOTO); 

I can get the bitmap:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), mCapturedImageURI);

However, I've noticed that the image is rotated on some devices. After searching posts on here, the typical solution seemed to get the rotation via:

String path = mCapturedImageURI.getPath();
ExifInterface exif = new ExifInterface(path);
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

Unfortunately, my int rotation is always 0 even though the bitmap is rotated.

I've also tried this which worked when I uploaded a picture already on the device but the orientation is still 0:

String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = managedQuery(mCapturedImageURI, orientationColumn, null, null, null);
if (cur != null && cur.moveToFirst()) {
      orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}

Anyone see anything I may be doing wrong here? Or another workaround?

Generally, the bitmap is rotated 90 degrees counter-clockwise with the back camera and 90 degrees clockwise with the front camera. Works ok on Moto G. Rotated on Galaxy S3 and LG G2.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to use the information in the content cursor.

float photoRotation = 0;
boolean hasRotation = false;
String[] projection = { Images.ImageColumns.ORIENTATION };
try {
    Cursor cursor = getActivity().getContentResolver().query(photoUri, projection, null, null, null);
    if (cursor.moveToFirst()) {
        photoRotation = cursor.getInt(0);
        hasRotation = true;
    }
    cursor.close();
} catch (Exception e) {}

if (!hasRotation) {
    ExifInterface exif = new ExifInterface(photoUri.getPath());
    int exifRotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
            ExifInterface.ORIENTATION_UNDEFINED);

    switch (exifRotation) {
        case ExifInterface.ORIENTATION_ROTATE_90: {
            photoRotation = 90.0f;
            break;
        }
        case ExifInterface.ORIENTATION_ROTATE_180: {
            photoRotation = 180.0f;
            break;
        }
        case ExifInterface.ORIENTATION_ROTATE_270: {
            photoRotation = 270.0f;
            break;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...