Android ImagePicker/MediaStore返回旋转的图像
创始人
2024-08-14 05:00:10
0

在Android开发中,有时候会遇到使用ImagePicker或MediaStore获取的图片会出现旋转的问题。这是因为图片的EXIF信息中包含了旋转角度信息,而有些手机相机会在拍摄时根据设备的方向来记录旋转角度。

解决这个问题的方法是读取图片的EXIF信息,然后根据旋转角度来对图片进行旋转调整。

以下是一个示例代码:

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.IOException;
import java.io.InputStream;

public class ImageUtils {

    public static Bitmap handleImageRotation(Context context, Uri uri) {
        Bitmap bitmap = null;
        try {
            InputStream inputStream = context.getContentResolver().openInputStream(uri);
            bitmap = BitmapFactory.decodeStream(inputStream);

            ExifInterface exifInterface = new ExifInterface(getRealPathFromURI(context, uri));
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);

            Matrix matrix = new Matrix();
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    matrix.setRotate(90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    matrix.setRotate(180);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    matrix.setRotate(270);
                    break;
                default:
                    break;
            }

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

            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    private static String getRealPathFromURI(Context context, Uri contentUri) {
        String[] projection = {MediaStore.Images.Media.DATA};
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(contentUri, projection, null, null, null);
        if (cursor == null) {
            return contentUri.getPath();
        } else {
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            String path = cursor.getString(columnIndex);
            cursor.close();
            return path;
        }
    }
}

这个示例代码中的handleImageRotation()方法接受一个Context和一个Uri参数,返回一个经过旋转调整的Bitmap对象。它首先通过ContentResolver获取图片的输入流,然后使用BitmapFactory.decodeStream()方法将输入流转换为Bitmap对象。接着,它通过ExifInterface类获取图片的EXIF信息中的旋转角度。最后,根据旋转角度使用Matrix类对Bitmap进行旋转调整,生成最终的旋转后的Bitmap对象。

使用示例代码:

// 假设使用ImagePicker选择了一张图片,并将其Uri保存在变量uri中
Bitmap rotatedBitmap = ImageUtils.handleImageRotation(this, uri);
// 使用旋转后的Bitmap进行显示或处理

这样,你就可以使用这个解决方法来处理从ImagePicker或MediaStore返回的旋转的图像。

相关内容

热门资讯

辅助透视!红龙poker透视工... 辅助透视!红龙poker透视工具,wpk辅助器,扑克教程(先前真的是有挂)1、许多玩家不知道红龙po...
透视神器!aapoker可以开... 透视神器!aapoker可以开挂吗,hhpoker辅助软件,秘笈分析(有挂黑科技)1、下载好aapo...
透视私人局!wpk辅助工具,w... 透视私人局!wpk辅助工具,wpk作弊最怕三个东西,详细教程(原生真的有挂)是一款可以让一直输的玩家...
透视苹果版!wepoker透视... 透视苹果版!wepoker透视,wpk辅助软件多少钱一个,必备科技(有挂透明);透视苹果版!wepo...
透视中牌率!wepoker透视... 透视中牌率!wepoker透视底牌脚本,德普之星透视辅助软件,玩家教程(从前是真的有挂);亲,其实确...
透视好友!aapoker透视脚... 透视好友!aapoker透视脚本安装包,wpk脚本下载,科技新动态(有挂技巧)是一款可以让一直输的玩...
透视苹果版!wpk辅助工具下载... 透视苹果版!wpk辅助工具下载,wepoker辅助透视软件,2025新版(原本是有挂);人气非常高,...
透视黑科技!wpk透视脚本,w... 透视黑科技!wpk透视脚本,wepoker透视脚本苹果版,科技分享(有挂介绍);小薇(透视辅助)致您...
透视教程!wepoker底牌透... 透视教程!wepoker底牌透视脚本,哈糖大菠萝开挂,大神讲解(原生真的有挂)1、这是跨平台的wep...
透视玄学!红龙poker有辅助... 1、透视玄学!红龙poker有辅助吗,wepoker透视,玩家分享(有挂透明)。2、红龙poker有...