[英]How to find out if the image from the gallery was taken in portrait or landscape from camera
portrait
or landscape
我试图找出图像是portrait
还是landscape
I have the path obtained from below code as 我有从下面的代码获得的路径
/storage/emulated/0/DCIM/Camera/IMG_20181110_211757.jpg
Then i am using the ExifInterface to find the orientation: 然后我正在使用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();
}
}
I always get orientation as ExifInterface.ORIENTATION_UNDEFINED
我总是以ExifInterface.ORIENTATION_UNDEFINED
作为ExifInterface.ORIENTATION_UNDEFINED
How to resolve this 如何解决这个问题
I have a image in gallery which was taken from camera 我的图库中有一个从相机拍摄的图像
There is no way for a developer to guarantee that any particular image came from a camera app, unless the developer is the author of that camera app. 开发人员无法保证任何特定图像都来自相机应用程序,除非开发人员是该相机应用程序的作者。
I am trying to find out if the image is portrait or landscape 我试图找出图像是纵向还是横向
There is no requirement for an image to be either portrait or landscape. 不需要图像是纵向或横向。 For example, the camera app might crop its photos to be circular or square. 例如,相机应用可以将其照片裁剪为圆形或正方形。
I have the path obtained from below code 我有从下面的代码获得的路径
That code will fail for lots of Uri
values on lots of devices. 由于许多设备上的许多Uri
值,该代码将失败。 If you want to use ExifInterface
for a Uri
, use getContentResolver().openInputStream()
to get an InputStream
on the content identified by the Uri
, then pass that InputStream
to this ExifInterface
constructor . 如果要将ExifInterface
用作Uri
,请使用getContentResolver().openInputStream()
获取有关Uri
标识的内容的InputStream
,然后将该InputStream
传递给此ExifInterface
构造函数 。
I always get orientation as ExifInterface.ORIENTATION_UNDEFINED 我总是以ExifInterface.ORIENTATION_UNDEFINED作为方向
Images do not have to have EXIF headers, let alone ExifInterface.TAG_ORIENTATION
. 图像不必具有EXIF标头,更不用说ExifInterface.TAG_ORIENTATION
。
If that tag does not exist, you could examine the width and height of the image and make a guess: 如果该标签不存在,则可以检查图像的宽度和高度并进行猜测:
This will fail for square images, and this will fail for cropped images (eg, the photo was taken portrait, but the user cropped it such that the width is now greater than the height). 对于正方形图像,这将失败,而对于裁剪后的图像,这将失败(例如,照片是人像拍摄,但用户将其裁剪为现在的宽度大于高度)。
The best solution is to change your app to not care whether the image is portrait or landscape. 最好的解决方案是更改应用程序,而不管图像是纵向还是横向。 The next-best solution is to ask the user how they want to handle the image, where perhaps you use the above algorithm to set the default option. 下一个最佳解决方案是询问用户他们要如何处理图像,也许您可以使用上述算法在此处设置默认选项。
Try it by changing path accessing process like below 通过更改如下所示的路径访问过程来尝试
File imFile = new File(imagePath);
ExifInterface ei = new ExifInterface(imFile.getAbsolutePath());
this will explain Android getAbsolutePath() not returning full path it 这将说明Android getAbsolutePath()不会返回完整路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.