简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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