从 ExifInterface 获取旋转总是返回 0 [英] Getting rotation from ExifInterface always returns 0

查看:56
本文介绍了从 ExifInterface 获取旋转总是返回 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 onActivityResult 上的捆绑包从相机传递位图.

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); 

我可以得到位图:

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);

不幸的是,即使位图旋转了,我的 int rotation 始终为 0.

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

我也试过这个,当我已经在设备上上传了一张图片但方向仍然是 0 时:

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?

一般情况下,位图与后置摄像头逆时针旋转 90 度,与前置摄像头顺时针旋转 90 度.在 Moto G 上运行正常.在 Galaxy S3 和 LG G2 上旋转.

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.

推荐答案

尝试使用内容光标中的信息.

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;
        }
    }
}

这篇关于从 ExifInterface 获取旋转总是返回 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆