相机文件夹图像出现旋转
Camera folder images appear rotated
我正在尝试在我的应用程序中处理图像。我目前面临的问题与图像的方向有关。从 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
总是0ORIENTATION_UNDEFINED
。无法理解我在这里做错了什么。
您可以通过使用 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;
}
编辑:
如果您需要确定拍摄的照片的方向 (landscape/portrait),然后决定旋转哪些照片,您可以使用 this question/answer 中的代码和 ExifInterface。
我看到你已经应用了类似的东西,所以也许你应该只用 1 个参数尝试这个修改,如链接的答案:
exif.getAttribute(ExifInterface.TAG_ORIENTATION);
更新:
好的,所以它也不起作用。但它应该,对吧?这就是提到的方法所做的。所以我建议你检查一下 uri.getPath()
实际上 returns(使用调试器或 System.out.println()
),看看它是否正确。无效路径错误在我身上发生过很多次,每次都需要一段时间才能找出问题所在。
String path = uri.getPath();
//breakpoint somewhere below
更新 2:
我做了一些研究,显然,您无法从 URI 获取绝对文件路径。所以我在 whosebug.com 上找到了这个解决方案,它提供了一个超级简单的解决方法。
Link
得到解决方案。我使用了 BitmapUtil
中的 getThumbnail(ContentResolver contentResolver, long id)
方法,它从光标读取图像的元数据,然后进行相应的处理。
感谢 Jason Fry
这个有用的工具。
我正在尝试在我的应用程序中处理图像。我目前面临的问题与图像的方向有关。从 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
总是0ORIENTATION_UNDEFINED
。无法理解我在这里做错了什么。
您可以通过使用 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;
}
编辑:
如果您需要确定拍摄的照片的方向 (landscape/portrait),然后决定旋转哪些照片,您可以使用 this question/answer 中的代码和 ExifInterface。 我看到你已经应用了类似的东西,所以也许你应该只用 1 个参数尝试这个修改,如链接的答案:
exif.getAttribute(ExifInterface.TAG_ORIENTATION);
更新:
好的,所以它也不起作用。但它应该,对吧?这就是提到的方法所做的。所以我建议你检查一下 uri.getPath()
实际上 returns(使用调试器或 System.out.println()
),看看它是否正确。无效路径错误在我身上发生过很多次,每次都需要一段时间才能找出问题所在。
String path = uri.getPath();
//breakpoint somewhere below
更新 2:
我做了一些研究,显然,您无法从 URI 获取绝对文件路径。所以我在 whosebug.com 上找到了这个解决方案,它提供了一个超级简单的解决方法。
Link
得到解决方案。我使用了 BitmapUtil
中的 getThumbnail(ContentResolver contentResolver, long id)
方法,它从光标读取图像的元数据,然后进行相应的处理。
感谢 Jason Fry
这个有用的工具。