21

当我使用 Android 的相机应用程序拍照时,它会检测手机的方向并相应地保存照片。因此,如果我拍摄建筑物的照片,屋顶将在顶部,无论我以横向还是纵向握持手机。

但是,当我使用

Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

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

如何设置意图以便相机将方向考虑在内?

或者我可以以某种方式推断照片是在什么方向拍摄的,然后自己旋转它?

还是有其他方法?

4

4 回答 4

27

我找到了答案。图像的Exif具有方向指示符。以防万一,Exif 可以像这样在 Android 中查看:

ExifInterface exif = new ExifInterface("filepath");  
exif.getAttribute(ExifInterface.TAG_ORIENTATION);
于 2010-02-19T21:35:40.757 回答
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;
}
于 2013-12-06T18:08:33.043 回答
4

为了快速修复,您可以检查图像的宽度是否大于图像的高度。这意味着它是横向的,您可以将其更改为纵向。

private Bitmap fromGallery(final Uri selectedImageUri) {
    try {
        Bitmap bm = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);

        ExifInterface exif = new ExifInterface(selectedImageUri.getPath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        int angle = 0;
        switch (orientation) {

            case ExifInterface.ORIENTATION_ROTATE_90:
                angle = 90;
                break;

            case ExifInterface.ORIENTATION_ROTATE_180:
                angle = 180;
                break;

            case ExifInterface.ORIENTATION_ROTATE_270:
                angle = 270;
                break;

            default:
                angle = 0;
                break;
        }

        Matrix mat = new Matrix();

        if (angle == 0 && bm.getWidth() > bm.getHeight())
            mat.postRotate(90);
        else
            mat.postRotate(angle);

        return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), mat, true);

    }
    catch (IOException e) {
        Log.e("", "-- Error in setting image");
    }
    catch (OutOfMemoryError oom) {
        Log.e("", "-- OOM Error in setting image");
    }
    return null;
}
于 2015-03-10T07:35:33.090 回答
2

我使用了一个文件提供程序,除了制作一个临时副本并从中读取数据外,没有任何效果。

public static int getOrientation(Context context, Uri uri) {

    int rotate = 0;

    try {

        ParcelFileDescriptor parcelFileDescriptor =
                context.getContentResolver().openFileDescriptor(uri, "r");

        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

        FileInputStream input = new FileInputStream(fileDescriptor);

        File tempFile = File.createTempFile("exif", "tmp");

        String tempFilename = tempFile.getPath();

        FileOutputStream output = new FileOutputStream(tempFile.getPath());

        int read;

        byte[] bytes = new byte[4096];

        while ((read = input.read(bytes)) != -1) {
            output.write(bytes, 0, read);
        }

        input.close();
        output.close();

        ExifInterface exif = new ExifInterface(tempFile.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;
        }

    } catch (Exception e) {
        Log.i(TAG, e.getLocalizedMessage());
    }

    return rotate;
}

奖金旋转功能

public static Bitmap rotateImage(Context context, Uri uri, int orientation) throws IOException {

    ParcelFileDescriptor parcelFileDescriptor =
            context.getContentResolver().openFileDescriptor(uri, "r");
    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
    Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
    parcelFileDescriptor.close();

    if (orientation > 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);

        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    return bitmap;
}
于 2018-05-08T19:58:47.237 回答