简体   繁体   English

如何确定图库中的图像是从相机拍摄的肖像还是风景

[英]How to find out if the image from the gallery was taken in portrait or landscape from camera

  • I have a image in gallery which was taken from camera 我的图库中有一个从相机拍摄的图像
  • I am trying to find out if the image is 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: 如果该标签不存在,则可以检查图像的宽度和高度并进行猜测:

  • If the width is greater than the height, it might be landscape 如果宽度大于高度,则可能是横向
  • If the height is greater than the width, it might be portrait 如果高度大于宽度,则可能是纵向

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.

相关问题 以人像模式从相机拍摄的加载到画布上的照片为横向 - Photos loaded onto canvas taken from a camera in portrait mode are landscape 如何在图库中的imageView上设置图像以及在Android中由相机拍摄的图像? - How to set an image on imageView from the gallery and image taken by camera in Android? 从相机拍摄后将图像保存到图库 - save image to the gallery after taken from the camera 如何检查图库中的图像是从相机还是屏幕截图中拍摄的? - How to check if image in gallery was taken from camera or screenshot? 如何旋转从相机或画廊拍摄的图像? - How rotate image taken from camera or gallery.? Android:如何在设置图像视图时检测从图库中选取的图像方向(纵向或横向)? - Android : How to detect the image orientation (portrait or landscape) picked from gallery while setting on an imageview? 当从相机拍摄图像时,意图始终是风景 - When Image is taken from camera Intent is always landscape Android通过使用文件系统保存从相机或图库拍摄的图像 - Android save taken image from camera or gallery by using file system ImageView不显示从手机摄像头或照片库拍摄的图像 - ImageView does not display image taken from phone camera or photo gallery 在上传之前,请调整从图库或照相机拍摄的图像的大小 - Resize image taken from gallery or camera, before being uploaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM