简体   繁体   English

如何检测照片是否垂直拍摄? (我找到了一种方法,但是没有用)

[英]How can I detect photo is taken vertical or not? (I find a way but it does not work)

I use this method to detect photo is taken vertical or not from camera : 我使用这种方法来检测照片是否是从相机垂直拍摄的:

 boolean isVertical = true ;     
        ExifInterface exif2;
        try {
            exif2 = new ExifInterface(path_img);

        int orientation = exif2.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                isVertical = false ; 
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                isVertical =false ; 
                break;
        }

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }   

But the problem is : 但是问题是:

orientation

s value is always 1 s的值始终为1

How can I solve this issue? 我该如何解决这个问题?

try this 尝试这个

          int rotate = 0;
                try {
                    getContentResolver().notifyChange(imageUri, null);
                    File imageFile = new File(imagePath);
                    ExifInterface exif = new ExifInterface(
                            imageFile.getAbsolutePath());
                    int orientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);

                    switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        rotate = 270;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        rotate = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        rotate = 90;
                        break;
                    }
                    Log.v(Common.TAG, "Exif orientation: " + orientation);
                } catch (Exception e) {
                    e.printStackTrace();
                }

I suggest you Create a new Bitmap and then get width and height of it. 我建议您创建一个新的位图,然后获取它的宽度和高度。

Bitmap bitmap = BitmapFactory.decodeFile("full_path_of_bitmap");
if(bitmap.getWidth() > bitmap.getHeight())
   // Picture is Landscape.
else
   // Picture is Portrait.

Kolay gelsin. Kolay明胶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM