从照片中获取方向Exif标签

时间:2016-01-11 09:49:42

标签: android android-camera

我正在尝试检测本机相机拍摄的照片的方向。这是我的代码:

ExifInterface exifInterface = new ExifInterface(photoPath);
String orientation = exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);

if (Configuration.ORIENTATION_PORTRAIT == Integer.parseInt(orientation)) {
    Log.d(TAG, "Portrait! " + orientation);
} else {
    Log.d(TAG, "Landscape! " + orientation);
}

但是,如果我以纵向模式拍摄照片,则会打印Landscape! 6。如果我以横向模式拍摄,则会打印Portrait! 1

编译和目标SDK版本为21.在android.content.res.Configuration类中有以下两个常量:

public static final int ORIENTATION_PORTRAIT = 1; 
public static final int ORIENTATION_LANDSCAPE = 2;

为什么我在横向时获得1而在纵向时获得6

1 个答案:

答案 0 :(得分:1)

Use "ExifInterface.getAttributeInt() or getAttribute()..."

此方法是为了获得需要旋转角度的图像的正确值。

 - "ExifInterface.getAttributeInt" will returns angle of rotation you
   need to make,not portrait or landscape

See the following snippet for your  understanding.

    ExifInterface exif = null;
           try {
               exif = new ExifInterface(path);
           } catch (IOException e) {
               e.printStackTrace();
           }
           try {
               switch (exif
                       .getAttributeInt(ExifInterface.TAG_ORIENTATION, 1)) {
                   case ExifInterface.ORIENTATION_ROTATE_180:
                       return 180;
                   case ExifInterface.ORIENTATION_ROTATE_90:
                       return 90;
                   case ExifInterface.ORIENTATION_ROTATE_270:
                       return 270;
                   case ExifInterface.ORIENTATION_NORMAL:
                       return 0;
                   default:
                       return 0;
               }
           } catch (NullPointerException e) {
               e.printStackTrace();
           }
           return 0;