来自画廊/相机意图的图片方向
我正在从相机/图库意图获取图片到我的应用程序。在许多手机图片中,我从意图/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
弗洛里安的答案适用于画廊图像。
以下代码适用于捕获的图像,尚未尝试使用图库图像,但我相信它应该有效。希望这对任何人都有帮助。
代码:
编辑:
从评论中可以看出,有些设备不支持 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 :
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.
以下方法返回图像的方向(以度为单位)。它适用于画廊中的图像。返回值:
代码:
The following method returns the orientation of an image in degrees. It works with images from the gallery. Return values for:
The code:
这是我找到的最好答案,作者: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
您必须首先使用 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.
我对 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.
我已经在一个项目中这样做了,因为我遇到了同样的问题(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.
这看起来与此问题中解决的问题相同:
Android:从图库加载的位图在 ImageView 中旋转
This looks like the same problem that was solved in this question:
Android: Bitmaps loaded from gallery are rotated in ImageView
就这样用吧!
在代码中,
很简单!
Use it this way!
And in the code,
Simple!
我在做什么:
首先使用其元数据信息检查相机拍摄的图像的方向,如果我们在纵向中发现这一点,那么我们必须将图像旋转 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!
简单的 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