如何找到使用Intent MediaStore.ACTION_IMAGE_CAPTURE拍摄的照片的方向?

Arc*_*tos 21 camera android android-intent

当我使用Androids相机应用程序拍照时,它会检测手机的方向并相应地保存照片.因此,如果我拍摄建筑物的照片,无论是将手机置于横向位置还是纵向,屋顶都将位于顶部.

但是,当我使用时

Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

要获得图片,相机应用程序不会对方向做出反应.如果我垂直握住手机(人像),结果图片将会旋转,所述建筑物的屋顶位于屏幕左侧.

如何设置意图以使相机考虑方向?

或者我可以在某种程度上推断出拍摄照片的方向,然后自己旋转它?

或者任何其他建议将不胜感激.

〜提前致谢,最好的问候.

Arc*_*tos 27

找到了答案.图像的exif具有方向的指示符.以防万一,exif可以在Android中查看,如下所示:

ExifInterface exif = new ExifInterface("filepath");  
exif.getAttribute(ExifInterface.TAG_ORIENTATION);
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,所有的方向看起来都像API Level 5:http://developer.android.com/reference/android/media/ExifInterface.html#TAG_ORIENTATION那是Android 2.0:http://developer.android.com/guide/appendix /api-levels.html我在2.1/Api等级7中成功使用它. (4认同)

小智 11

如果可用,请从Exif中读取,否则从MediaStore中读取

public static int getImageOrientation(Context context, String imagePath) {
    int orientation = getOrientationFromExif(imagePath);
    if(orientation <= 0) {
        orientation = getOrientationFromMediaStore(context, imagePath);
    }

    return orientation;
}

private static int getOrientationFromExif(String imagePath) {
    int orientation = -1;
    try {
        ExifInterface exif = new ExifInterface(imagePath);
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                ExifInterface.ORIENTATION_NORMAL);

        switch (exifOrientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                orientation = 270;

                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                orientation = 180;

                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                orientation = 90;

                break;

            case ExifInterface.ORIENTATION_NORMAL:
                orientation = 0;

                break;
            default:
                break;
        }
    } catch (IOException e) {
        Log.e(LOG_TAG, "Unable to get image exif orientation", e);
    }

    return orientation;
}

private static int getOrientationFromMediaStore(Context context, String imagePath) {
    Uri imageUri = getImageContentUri(context, imagePath);
    if(imageUri == null) {
        return -1;
    }

    String[] projection = {MediaStore.Images.ImageColumns.ORIENTATION};
    Cursor cursor = context.getContentResolver().query(imageUri, projection, null, null, null);

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

    return orientation;
}

private static Uri getImageContentUri(Context context, String imagePath) {
    String[] projection = new String[] {MediaStore.Images.Media._ID};
    String selection = MediaStore.Images.Media.DATA + "=? ";
    String[] selectionArgs = new String[] {imagePath};
    Cursor cursor = context.getContentResolver().query(IMAGE_PROVIDER_URI, projection, 
            selection, selectionArgs, null);

    if (cursor != null && cursor.moveToFirst()) {
        int imageId = cursor.getInt(0);
        cursor.close();

        return Uri.withAppendedPath(IMAGE_PROVIDER_URI, Integer.toString(imageId));
    } 

    if (new File(imagePath).exists()) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DATA, imagePath);

        return context.getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    } 

    return null;
}
Run Code Online (Sandbox Code Playgroud)

  • 什么是IMAGE_PROVIDER_URI常量?谢谢. (11认同)