[英]Photos loaded onto canvas taken from a camera in portrait mode are landscape
[英]How to find out if the image from the gallery was taken in portrait or landscape from camera
portrait
还是landscape
我有从下面的代码获得的路径
/storage/emulated/0/DCIM/Camera/IMG_20181110_211757.jpg
然后我正在使用ExifInterface查找方向:
public static void getCameraPhotoOrientation(Activity activity, String imagePath) {
String path =getPath(activity,imagePath);
try {
ExifInterface ei = new ExifInterface(path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
Timber.d("Type-1");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Timber.d("Type-2");
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Timber.d("Type-3");
break;
case ExifInterface.ORIENTATION_UNDEFINED:
Timber.d("Type-4");
break;
}
}catch (IOException ex){
ex.printStackTrace();
}
}
我总是以ExifInterface.ORIENTATION_UNDEFINED
作为ExifInterface.ORIENTATION_UNDEFINED
如何解决这个问题
我的图库中有一个从相机拍摄的图像
开发人员无法保证任何特定图像都来自相机应用程序,除非开发人员是该相机应用程序的作者。
我试图找出图像是纵向还是横向
不需要图像是纵向或横向。 例如,相机应用可以将其照片裁剪为圆形或正方形。
我有从下面的代码获得的路径
由于许多设备上的许多Uri
值,该代码将失败。 如果要将ExifInterface
用作Uri
,请使用getContentResolver().openInputStream()
获取有关Uri
标识的内容的InputStream
,然后将该InputStream
传递给此ExifInterface
构造函数 。
我总是以ExifInterface.ORIENTATION_UNDEFINED作为方向
图像不必具有EXIF标头,更不用说ExifInterface.TAG_ORIENTATION
。
如果该标签不存在,则可以检查图像的宽度和高度并进行猜测:
对于正方形图像,这将失败,而对于裁剪后的图像,这将失败(例如,照片是人像拍摄,但用户将其裁剪为现在的宽度大于高度)。
最好的解决方案是更改应用程序,而不管图像是纵向还是横向。 下一个最佳解决方案是询问用户他们要如何处理图像,也许您可以使用上述算法在此处设置默认选项。
通过更改如下所示的路径访问过程来尝试
File imFile = new File(imagePath);
ExifInterface ei = new ExifInterface(imFile.getAbsolutePath());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.