android - 从 ExifInterface 获取旋转总是返回 0

标签 android bitmap camera exif

我在相机的 onActivityResult 上通过 bundle 传递位图。

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

但是,我注意到图像在某些设备上旋转了。在这里搜索帖子后,典型的解决方案似乎是通过以下方式进行轮换:

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

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

我也试过这个,当我上传一张已经在设备上的图片但方向仍然是 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]));
}

有人看到我在这里做错了什么吗?或者其他解决方法?

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

最佳答案

尝试使用内容游标中的信息。

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

关于android - 从 ExifInterface 获取旋转总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24128346/

相关文章:

java - Android 内存 - 声明全局变量

Android:如何在 View.onDraw() 中使用 RGB_565 颜色来匹配位图颜色

android - 如何从我们的应用程序中删除图像?

bitmap - Visual Studio 2017 中无法识别 System.Drawing.Bitmap

opencv - OpenCV在鱼眼图像中找不到棋盘角

ios - 点击对焦但曝光不起作用

java - 在 Android 中使用 XAdeS-BES 进行签名

android - 在 Android 教程中绘图

java - setontouchlistener x 秒

ios - "Communications error"- AVFoundation 相机