1

有没有办法获取您用相机拍摄的照片的方向?

我正在使用下面的方法来恢复我画廊中图像的方向,但是如果我用相机拍照,它就不起作用。

   public static int getImageOrientation(Context context, Uri photoUri) {
    int orientation = -1;
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    if (cursor != null && cursor.getCount() == 1) {
        cursor.moveToFirst();
        orientation = cursor.getInt(0);
    }

    return orientation;
}
4

1 回答 1

3

您可以从以下代码中获取图像的方向:

try {
    context.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.i("RotateImage", "Exif orientation: " + orientation);
    Log.i("RotateImage", "Rotate value: " + rotate);
} catch (Exception e) {
    e.printStackTrace();
}

这里首先您必须将捕获的图像保存在 sd 卡上,然后从保存图像的路径中获取图像 Uri。然后在发布的代码中将该 Uri 作为 imageUri 传递。

于 2013-09-02T16:18:46.400 回答