相机文件夹中的图片出现了旋转

3

我正在尝试在我的应用程序中处理图像。目前我面临的问题与图像的方向有关。从Android相机文件夹中选择的图像缩略图显示为旋转了90度的状态。我获取的缩略图如下所示:

    Uri thumbUri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, uri.getLastPathSegment());
    if (thumbUri != null){
        try {
            List<String> parts = uri.getPathSegments();
            String lastPart = parts.get(parts.size() - 1);
            int index = lastPart.indexOf(":");
            if (index != -1)
            {
                lastPart = lastPart.substring(index+1);
            }
            long id = Long.parseLong(lastPart);

            // get a thumbnail for the image
            Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
                    context.getContentResolver(),
                    id,
                    MediaStore.Images.Thumbnails.MINI_KIND,
                    null
            );
            if (bitmap != null)
            {
                return bitmap;
            }
        }
        catch (Exception e)
        {
            Log.e(LOG_TAG, "Unable to generate thumbnail from thumbnail uri " + e.getMessage(), e);
        }

    }

也尝试通过从ExifInterface读取方向来修复它;

ExifInterface ei = new ExifInterface(uri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

但是 orientation 的返回值总是 0 ORIENTATION_UNDEFINED。我不知道我做错了什么。

@HBizhi,我在问题中已经提到了,ExifInterface对我没有帮助。 - Ammar
2个回答

2
我找到了解决方案。我使用了来自BitmapUtilgetThumbnail(ContentResolver contentResolver, long id)方法,该方法从游标中读取图像的元数据,然后相应地进行处理。
感谢Jason Fry提供这个有用的工具。

1

您可以通过使用 Matrix 对位图执行额外的旋转,来进行简单的解决方案。这是非常简单的。

//we don't need a NullPointerException now, do we?
if (bitmap != null)
{
    //ever so simply initialize a Matrix
    Matrix matrix = new Matrix();

    //tell the Matrix it should rotate everything it's applied to by -90 degreeds
    matrix.postRotate(-90);

    //create a clone of the bitmap while applying the mentioned Matrix
    //for width and height attributes, you must always use old bitmap's width and height
    bitmap = Bitmap.createBitmap(bitmap, 0, 0,
                                 bitmap.getWidth(), bitmap.getHeight(), 
                                 matrix, true);

    return bitmap;
}

编辑:

如果你需要确定照片拍摄的方向(横屏/竖屏),然后决定哪些需要旋转,你可以使用来自这个问题/答案的代码和ExifInterface。 我看到你已经应用了类似的东西,所以也许你应该尝试这个只有一个参数的修改,就像链接中的答案一样:

exif.getAttribute(ExifInterface.TAG_ORIENTATION);

更新:

好的,它也不起作用。但是它应该起作用,对吧?那就是所提到的方法的作用。所以我建议你检查一下uri.getPath()实际返回的内容(使用调试器或System.out.println()),看看是否正确。无效路径错误经常发生在我身上,每次都需要花费一些时间才能找出问题所在。

String path = uri.getPath();
//breakpoint somewhere below

更新2:

我做了一些研究,显然你无法从URI获取绝对文件路径。因此,我在StackOverflow.com上找到了这个解决方案,提供了一个超级简单的解决方法。

链接


好的。但首先我需要知道图像是否旋转。我不能将旋转应用于所有图像,因为其他所有图像都是正确方向的。问题出在从相机文件夹中选择的那些图像上。 - Ammar
如果问题是它们以不同的角度(横向/纵向)拍摄,那么您可以使用ExifInterface确定哪个角度。我刚在另一个StackOverflow问题中找到了解决方案。我将在几分钟内将其添加到我的答案中。 - cruelcore1
我看到你已经尝试过了,但是你在getAttribute方法中使用了2个参数。请尝试只使用1个参数ExifInterface.TAG_ORIENTATION,就像我在答案的EDIT部分中发布的那样。 - cruelcore1
我已经发布了另一个更新,建议查看uri.getPath()实际返回了什么。 - cruelcore1
尝试了提到的链接,但不幸的是它也没有帮助。但还是谢谢你的建议。我已经找到了解决方案,即https://dev59.com/IIbca4cB1Zd3GeqPXZGE#27821963。 - Ammar
显示剩余3条评论

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