从图库选择的Android图片方向始终为0:Exif标签

12

当我从图库选择一张图片在我的安卓应用程序中设置它到我的imageView时,我发现它会上下颠倒180度、270度或90度。
为了检查/解决这个问题,我使用了EXIF方向,但它总是给我“0”。

我不知道问题出在哪里。

代码:

Uri selectedImageUri = data.getData();

                    absolutePath = selectedImageUri.getPath();

                    exifMedia = new ExifInterface(absolutePath);
                    String exifOrint = exifMedia.getAttribute(ExifInterface.TAG_ORIENTATION);

                    exifOrientation = Integer.parseInt(exifOrint);

                    System.out.println("Orientation Tag is:"+exifOrientation);

                    /** Convert URI into byte */
                    ContentResolver cr = getBaseContext()
                    .getContentResolver();
                    InputStream inputStream = cr
                    .openInputStream(selectedImageUri);
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                    rotatedBMP = getResizedBitmapImage(
                            bitmap, 100, 100,exifOrientation);

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    rotatedBMP.compress(Bitmap.CompressFormat.PNG, 100,
                            stream);
                    byteArray = stream.toByteArray();
                    mimProfileImageBrowse.setImageBitmap(rotatedBMP);

方法:

public  Bitmap getResizedBitmapImage(Bitmap bm, int newHeight, int newWidth, int exifOrientation) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        /**
         *  create a matrix for the manipulation
         */

        Matrix matrix = new Matrix();
        /**
         *  resize the bit map
         */


        /*

         1 = Horizontal (normal) 
2 = Mirror horizontal 
3 = Rotate 180 
4 = Mirror vertical 
5 = Mirror horizontal and rotate 270 CW 
6 = Rotate 90 CW 
7 = Mirror horizontal and rotate 90 CW 
8 = Rotate 270 CW

         */


        switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:

            rotate = 270;

            break;


        case ExifInterface.ORIENTATION_ROTATE_180:

            rotate = 180;


            break;

        case ExifInterface.ORIENTATION_ROTATE_90:

            rotate = 90;

            break;

        case ExifInterface.ORIENTATION_TRANSPOSE:

            rotate = 45;

            break;

        default:
            break;
        }


        matrix.postScale(scaleWidth, scaleHeight);
        matrix.postRotate(rotate);

        /**
         * recreate the new Bitmap
         */
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        resizedBitmap = Bitmap.createScaledBitmap(bm, 65, 65, true);

        return resizedBitmap;

    }

请帮忙。另外matrix.postROTATE没有旋转位图。我不知道为什么。

谢谢

8个回答

28

获取所选图片的Uri后,请使用以下方法调用下面的函数,
像这样:

Uri selectedImageUri = data.getData();
Bitmap bitmap = scaleImage(this,selectedImageUri);

你的活动中需要包含的两个函数是:
 public static Bitmap scaleImage(Context context, Uri photoUri) throws IOException {
        InputStream is = context.getContentResolver().openInputStream(photoUri);
        BitmapFactory.Options dbo = new BitmapFactory.Options();
        dbo.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, dbo);
        is.close();

        int rotatedWidth, rotatedHeight;
        int orientation = getOrientation(context, photoUri);

        if (orientation == 90 || orientation == 270) {
            rotatedWidth = dbo.outHeight;
            rotatedHeight = dbo.outWidth;
        } else {
            rotatedWidth = dbo.outWidth;
            rotatedHeight = dbo.outHeight;
        }

        Bitmap srcBitmap;
        is = context.getContentResolver().openInputStream(photoUri);
        if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
            float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
            float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
            float maxRatio = Math.max(widthRatio, heightRatio);

            // Create the bitmap from file
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = (int) maxRatio;
            srcBitmap = BitmapFactory.decodeStream(is, null, options);
        } else {
            srcBitmap = BitmapFactory.decodeStream(is);
        }
        is.close();

        /*
         * if the orientation is not 0 (or -1, which means we don't know), we
         * have to do a rotation.
         */
        if (orientation > 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);

            srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
                    srcBitmap.getHeight(), matrix, true);
        }

        String type = context.getContentResolver().getType(photoUri);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        if (type.equals("image/png")) {
            srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        } else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
            srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        }
        byte[] bMapArray = baos.toByteArray();
        baos.close();
        return BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
    }

    public static int getOrientation(Context context, Uri photoUri) {
        /* it's on the external media. */
        Cursor cursor = context.getContentResolver().query(photoUri,
                new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

        if (cursor.getCount() != 1) {
            return -1;
        }

        cursor.moveToFirst();
        return cursor.getInt(0);
    }

要从文件中获取Uri,请使用以下方法:

使用以下方法:

public static Uri getImageContentUri(Context context, File imageFile) {
        String filePath = imageFile.getAbsolutePath();
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Images.Media._ID },
                MediaStore.Images.Media.DATA + "=? ",
                new String[] { filePath }, null);
        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.MediaColumns._ID));
            Uri baseUri = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(baseUri, "" + id);
        } else {
            if (imageFile.exists()) {
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, filePath);
                return context.getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } else {
                return null;
            }
        }
    }

问:请问“MAX_IMAGE_DIMENSION”是什么?- Dhrupal Mar 13 '13

答:它基于您的ImageView大小或要显示的图像大小。如果是60X60,则为60,如果是100X100,则为100 @Dhrupal


请问“MAX_IMAGE_DIMENSION”是什么意思? - Dhrupal
1
它基于您的ImageView大小或要显示的图像大小。无论是60X60还是100X100,都会相应地调整大小。@Dhrupal - MKJParekh
7
抱歉,这在KitKat上不起作用,因为它返回与Intent.ACTION_GET_CONTENT不同的Uri。 - ılǝ
1
static int getOrientation 存在泄漏问题,光标未正确关闭。 - John Pang
谢谢你提供的代码,但我认为你应该在这里指定MAX_IMAGE_DIMENSION是什么。 - Hardik Joshi
显示剩余5条评论

2

自从问问题以来已经很长时间了。但是我遇到了相同的问题,并解决了这个问题。当你将文件路径放入exifInterface的构造函数中时,使用真实路径,例如"/storage/sdcard0/DCIM/Camera/blahbalh.jpg",而不是使用类似于/content:/media/external/images/media/blah的东西,这总是给出0信息。希望有人会受益。谢谢。

哎呀,下面的方法也是一样的。这里还有更多的内容。使用这个。

/**
 * @ref https://dev59.com/fnA75IYBdhLWcg3wPmd8
 * @param contentUri
 * @return
 */
public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

@Piotr,你的设备是什么? - minimanimo
LG G2。它只是简单地返回null。 - Piotr
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - peter.bartos

2
我想你不应该使用“selectedImageUri.getPath()”来获取路径,因为它实际上并没有返回图像文件路径。相反,您应该使用ContentResolver查询MediaStore.Images.ImageColumns.DATA的值,这是图像的实际路径。
事实上,如果您只想获得方向,则不必从Exif中获取,只需查询MediaStore.Images.ImageColumns.ORIENTATION的值即可。

我已经做对了,但是还有一个问题,就是我的matrix.postRotate正在执行,但是我的位图没有旋转。 - Gaurav Arora
很酷。只是好奇,为什么你要两次分配resizedBitmap? Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); resizedBitmap = Bitmap.createScaledBitmap(bm, 65, 65, true); - Neil

1
你应该使用 getRealPathFromURI 函数:
public static String getRealPathFromURI(Context context, Uri uri,
            String data) {

    String[] largeFileProjection = { MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.ImageColumns.DATA };

    String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
    Cursor myCursor = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            largeFileProjection, null, null, largeFileSort);
    String largeImagePath = "";
    try {
        myCursor.moveToFirst();
        largeImagePath = myCursor
                .getString(myCursor
                        .getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
    } finally {
        myCursor.close();
    }

    return largeImagePath;
}

1
这个答案对我有效:'documentFilePath=getRealPathFromURI(getApplicationContext(), uri, data.getDataString()); ' - Alexiscanny

1
尝试以下代码:-
ExifInterface exif = new ExifInterface(filename);  
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);  
Bitmap bmRotated = rotateBitmap(bitmap, orientation); 

调用下面的方法:-
public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

    try{
        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
           case ExifInterface.ORIENTATION_ROTATE_90:
               matrix.setRotate(90);
               break;
           case ExifInterface.ORIENTATION_TRANSVERSE:
               matrix.setRotate(-90);
               matrix.postScale(-1, 1);
               break;
           case ExifInterface.ORIENTATION_ROTATE_270:
               matrix.setRotate(-90);
               break;
           default:
               return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return bmRotated;
        }
        catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }
    catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return bitmap;
}

0

我在编程中无法让光标或Exif接口方法正常工作。我无法准确获取三星前后摄像头的可靠旋转数。

对于那些寻找适用于三星和其他主要厂商的前后摄像头的解决方案的人,nvhausid的答案非常出色:

https://dev59.com/_mUo5IYBdhLWcg3wvBeX#18915443

对于那些不想点击查看的人来说,关键的技巧是使用CameraInfo而不是依赖EXIF。
Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(mCurrentCameraId, info);
Bitmap bitmap = rotate(realImage, info.orientation);

完整代码请查看链接。


这个问题是关于从相册中选择图片而不是相机。 - Sharp Edge

-2

当图像被捕获时,它会存储一个exif数据作为旋转类型(纵向-横向)的信息。因此,你需要检查的唯一事情就是exif数据:

ExifInterface exifData = new ExifInterface(uri);
        int orientation = exifData.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        Matrix matrix;
        switch (orientation) {
        case 6:
             matrix = new Matrix();
            matrix.postRotate(90);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            break;
        case 3:
             matrix = new Matrix();
            matrix.postRotate(180);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            break;
        case 8:
            matrix = new Matrix();
            matrix.postRotate(270);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            break;
        default:
            break;

-2

你已经检查过在你的清单文件中是否设置了写入和读取权限了吗?因为如果你没有设置它们,你就无法访问位图的exif数据,我说的是Android M。 这是基于exif数据旋转位图的代码片段。

public static Bitmap rotateBitmap(String filePath, Bitmap bitmap) {
    ExifInterface exif;
    try {
        exif = new ExifInterface(filePath);

        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        Log.d("EXIF", "Exif: " + orientation);
        Matrix matrix = new Matrix();
        if (orientation == 6) {
            matrix.postRotate(90);
            Log.d("EXIF", "Exif: " + orientation);
        } else if (orientation == 3) {
            matrix.postRotate(180);
            Log.d("EXIF", "Exif: " + orientation);
        } else if (orientation == 8) {
            matrix.postRotate(270);
            Log.d("EXIF", "Exif: " + orientation);
        }
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }


}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接