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

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

问题描述

我通过在 onActivityResult 从相机绑定位图。

  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度。

解决方案

尝试使用内容光标中的信息。 / p>

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


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.

解决方案

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天全站免登陆