来自画廊/相机意图的图片方向

发布于 2024-10-09 01:49:47 字数 311 浏览 5 评论 0原文

我正在从相机/图库意图获取图片到我的应用程序。在许多手机图片中,我从意图/Uri 中读取的图片已经旋转到正确的方向。比如N1、Legend、Desire就是这样。

但在某些手机(例如 Milestone1、GalaxyS)上,无论以哪种方式拍摄照片,照片始终都是横向的。这意味着在我的应用程序中,肖像图片以错误的方式呈现给用户。我尝试读取图片的 EXIF 信息,但方向标签始终为 0。必须有一种方法可以找出图片的正确方向,因为在 Milestone1 中,图库应用程序可以正确显示肖像图片。

我如何知道在向用户显示图片之前是否需要自己旋转图片?

谢谢你的帮助!

I'm getting picture to my app from camera / gallery intent. In many phones picture that I read from the intent / Uri is already rotated to correct orientation. For example N1, Legend, Desire that is the case.

But then on some phones ( for example Milestone1, GalaxyS) the picture is always in landscape more no matter which way the picture was taken. This means that in my application portrait picture is presented wrong way to the user. I tried to read EXIF info of the picture but orientation tag is always 0. There has to be a way to find out the right orientation of the picture because in Milestone1 the gallery application shows the portrait pictures correctly.

How do I know if I need to rotate the picture myself before showing it to the user?

Thank you for you help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

被你宠の有点坏 2024-10-16 01:49:47

弗洛里安的答案适用于画廊图像。

以下代码适用于捕获的图像,尚未尝试使用图库图像,但我相信它应该有效。希望这对任何人都有帮助。

代码:

public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
     int rotate = 0;
     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.v(TAG, "Exif orientation: " + orientation);
     } catch (Exception e) {
         e.printStackTrace();
     }
    return rotate;
 }

编辑:
从评论中可以看出,有些设备不支持 Exif 信息,尚未检查,但我认为 HTC 不支持。请务必检查哪些设备并创建替代方案。

Florian answer is working for gallery images.

The following code works for captured images, havn't tried with gallery images but i believe it should work. Hope this helps anybody.

Code :

public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
     int rotate = 0;
     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.v(TAG, "Exif orientation: " + orientation);
     } catch (Exception e) {
         e.printStackTrace();
     }
    return rotate;
 }

EDIT:
As can be read in the comments, some devices do not support Exif information, havn't checked which but i think HTC doesn't. be sure to check what devices and create an alternative.

时常饿 2024-10-16 01:49:47

以下方法返回图像的方向(以度为单位)。它适用于画廊中的图像。返回值:

  • 正常横向:0
  • 正常纵向:90
  • 颠倒横向:180
  • 颠倒纵向:270
  • 未找到图像:-1

代码:

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

    try {
        if (cursor.moveToFirst()) {
            return cursor.getInt(0);
        } else {
            return -1;
        }
    } finally {
        cursor.close();
    }
}

The following method returns the orientation of an image in degrees. It works with images from the gallery. Return values for:

  • normal landscape: 0
  • normal portrait: 90
  • upside-down landscape: 180
  • upside-down portrait: 270
  • image not found: -1

The code:

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

    try {
        if (cursor.moveToFirst()) {
            return cursor.getInt(0);
        } else {
            return -1;
        }
    } finally {
        cursor.close();
    }
}
黑凤梨 2024-10-16 01:49:47
        int rotate = 0;
        try {
            File imageFile = new File(sourcepath);
            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;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Matrix matrix = new Matrix();
        matrix.postRotate(rotate);
        bitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        int rotate = 0;
        try {
            File imageFile = new File(sourcepath);
            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;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Matrix matrix = new Matrix();
        matrix.postRotate(rotate);
        bitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
哆兒滾 2024-10-16 01:49:47

这是我找到的最好答案,作者:ramar。 https://stackoverflow.com/a/20480741/2258389
非常适合画廊或相机

This is the best answer I have found, by ramaral. https://stackoverflow.com/a/20480741/2258389
Works great for gallery or camera

鼻尖触碰 2024-10-16 01:49:47

您必须首先使用 contentresolver 创建图库对象,然后将创建的 uri 传递给图像捕获意图。然后您可以查看 exif 数据,而不是图库方向数据。

You have to create the gallery object using contentresolver first and then pass the uri created to the image capture intent. Then you can look at the exif data, NOT the gallery orientation data.

℉服软 2024-10-16 01:49:47
Matrix matrix = new Matrix();

ExifInterface exifReader = new ExifInterface(filePath);

int orientation = exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);

if (orientation ==ExifInterface.ORIENTATION_NORMAL) {

      // Do nothing. The original image is fine.

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {

       matrix.postRotate(90);

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {

       matrix.postRotate(180);

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {

      matrix.postRotate(270);

}
Matrix matrix = new Matrix();

ExifInterface exifReader = new ExifInterface(filePath);

int orientation = exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);

if (orientation ==ExifInterface.ORIENTATION_NORMAL) {

      // Do nothing. The original image is fine.

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {

       matrix.postRotate(90);

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {

       matrix.postRotate(180);

} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {

      matrix.postRotate(270);

}
路弥 2024-10-16 01:49:47

我对 S1 也有同样的问题,我注意到即使您使用图库应用程序打开图像,它也会以横向模式打开(手机不知道照片的正确方向)。
如果您在相机应用程序中将手机旋转为纵向,图标(拍照、对焦和设置)将不会旋转,请尝试在 S2 或任何支持横向/纵向相机的设备中,这些图标将旋转。

我确信库存相机应用程序不支持按比例模式拍照。

I have the same problem with S1, and I noted even if you open the image using stock gallery app it will open in landscape mode(the phone doesn’t know the correct orientation for photo).
And if you rotate the phone to portrait in camera app the icons (take pic, focus and settings) will not rotated, try that in S2 or any device support landscape/portrait camera those icons will be rotated.

What I am sure the stock camera app doesn’t support taking photos in prorate mode.

呆° 2024-10-16 01:49:47

我已经在一个项目中这样做了,因为我遇到了同样的问题(Android 认为你只会在横向上制作图片)。我所做的就是检测当时手机的方向,然后旋转图像。不过,它假设收到意图时方向仍然相同。

I already did that for a project, because I had the same problem (Android thinking you'll only do a picture on landscape). What I did was detecting the phone orientation at the time, and then rotate the image. It supposes the orientation is still the same when the intent is received, though.

德意的啸 2024-10-16 01:49:47

这看起来与此问题中解决的问题相同:
Android:从图库加载的位图在 ImageView 中旋转

This looks like the same problem that was solved in this question:
Android: Bitmaps loaded from gallery are rotated in ImageView

和我恋爱吧 2024-10-16 01:49:47

就这样用吧!

private static int exifToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
        return 90;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
        return 180;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
        return 270;
    }
    return 0;
}

在代码中,

Bitmap newImage = Bitmap.createBitmap(actualImage, 0, 0, width, height, matrix, true);

很简单!

Use it this way!

private static int exifToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
        return 90;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
        return 180;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
        return 270;
    }
    return 0;
}

And in the code,

Bitmap newImage = Bitmap.createBitmap(actualImage, 0, 0, width, height, matrix, true);

Simple!

世界如花海般美丽 2024-10-16 01:49:47

我在做什么:
首先使用其元数据信息检查相机拍摄的图像的方向,如果我们在纵向中发现这一点,那么我们必须将图像旋转 90 度并显示,否则仅显示。

为了获取有关图像方向的信息,我们可以使用 Exif 接口。
就是这样!

What I am doing :
first check the orientation of image taken by camera using its meta data information , and If we found this in portrait then we have to rotate the image by 90 and display otherwise only display.

For getting the Information about orientation of image we can use Exif interface.
That's It!

权谋诡计 2024-10-16 01:49:47

简单的 exifto Degree 答案只有在您拍照并且我用它来拍摄照片时才始终有效。对于那些在选择照片和获得正确方向时遇到问题的人,请参阅我的答案:图片方向 - Android

The simple exiftodegree answers only consistently work when you have taken a photo and i use it for such. For those of you experiencing issues with choosing a photo and getting the correct orientation, see my answer here: Image Orientation - Android

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文