【问题标题】:Camera folder images appear rotated相机文件夹图像出现旋转
【发布时间】:2015-03-01 13:05:12
【问题描述】:

我正在尝试在我的应用中处理图像。我目前面临的问题与图像的方向有关。从 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。无法理解我在这里做错了什么。

【问题讨论】:

标签: android android-gallery


【解决方案1】:

找到解决方案。我使用了BitmapUtil中的getThumbnail(ContentResolver contentResolver, long id)方法,它从光标中读取图像的元数据,然后进行相应的处理。

感谢 Jason Fry 提供这个有用的实用程序。

【讨论】:

    【解决方案2】:

    您可以通过使用 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;
    }
    

    编辑:

    如果您需要确定拍摄照片的方向(横向/纵向),然后决定旋转哪些方向,您可以使用来自this question/answer 的代码和 ExifInterface。 我看到您已经应用了类似的东西,所以也许您应该尝试仅使用 1 个参数进行此修改,如链接答案中所示:

    exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    

    更新:

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

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

    更新 2:

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

    Link

    【讨论】:

    • 对。但首先我需要知道图像是否旋转。我无法对所有图像应用旋转,因为所有其他图像都处于正确的方向。问题出在从相机文件夹中选择的那些。
    • 如果问题是它们是在不同的角度(风景/肖像)拍摄的,那么您可以使用 ExifInterface 来确定是哪个角度。我刚刚在另一个 StackOverflow 问题中找到了解决方案。我会在几分钟后将其添加到我的答案中。
    • 我看到您已经尝试过了,但是您为 getAttribute 方法使用了 2 个参数。尝试只使用 1,ExifInterface.TAG_ORIENTATION,正如我在答案的编辑部分中发布的那样
    • 试过exif.getAttribute(ExifInterface.TAG_ORIENTATION)。它也给0ORIENTATION_UNDEFINED
    • 我发布了另一个更新,建议检查 uri.getPath() 实际返回的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 2011-10-14
    • 2014-01-20
    • 1970-01-01
    相关资源
    最近更新 更多