Android的 - 找到照片的方向是把照相机照相机、方向、照片、Android

2023-09-05 07:40:34 作者:准备.

我要拍照,找出它的方向,绘制在画布上,并旋转画布。我需要帮助找出照片的方向

I need to take a photo, find out its orientation, draw it on canvas and rotate the canvas. I need help to find out orientation of the photo

推荐答案

您需要使用嵌入照片中的EXIF标签:

You need to use the EXIF tag embedded in the photo:

private int getExifOrientation() {
  ExifInterface exif;
  int orientation = 0;
  try {
    exif = new ExifInterface( mImagePath );
    orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, 1 );
  } catch ( IOException e ) {
    e.printStackTrace();
  }
  Log.d(TAG, "got orientation " + orientation);
  return orientation;
}

但是,返回的实际EXIF值几分怪异。它允许对各种旋转和镜像。最好的参考我发现是这里。在一般情况下,后,你的方向,你要通过查找功能,运行它来获取度的旋转:

However, the actual EXIF value returned is sorta weird. It allows for all variety of rotation and mirroring. The best reference I've found is here. In general, after you get the orientation, you'll want to run it through a lookup function to get the rotation in degrees:

private int getBitmapRotation() {
  int rotation = 0;
  switch ( getExifOrientation() ) {
    case ExifInterface.ORIENTATION_ROTATE_180:
      rotation = 180;
      break;
    case ExifInterface.ORIENTATION_ROTATE_90:
      rotation = 90;
      break;
    case ExifInterface.ORIENTATION_ROTATE_270:
      rotation = 270;
      break;
  }

  return rotation;
}