摘要:拍照使用相机意图,并显示照片的正确方向(适用于希望所有设备) [英] Summary: Take a picture utilizing Camera Intent and display the photo with correct orientation (works on hopefully all devices)

查看:144
本文介绍了摘要:拍照使用相机意图,并显示照片的正确方向(适用于希望所有设备)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是最简单的事情在世界:拍照在你的Andr​​oid应用程序使用的默认摄像头的活动。不过,也有很多陷阱这些内容包含在跨越计算器几个职位,并在网上为,例如,空意图被传回,的画面不被纠正或OutOfMemoryError异常。

It seems to be the simplest thing in the world: taking a picture within your Android app using the default camera activity. However, there are many pitfalls which are covered in several posts across StackOverflow and the web as, for instance, Null Intents being passed back, the orientation of the picture not being correct or OutOfMemoryErrors.

我正在寻找一个解决方案,让我

I'm looking for a solution that allows me to

  1. 通过摄像机意图启动摄像头的活动,
  2. 检索照片的URI和
  3. 检索照片的正确方向。

此外,我想避免设备配置(制造商,型号,操作系统版本)具体实施尽可能。所以我想知道:什么是实现这一目标的最佳途径

Moreover, I would like to avoid a device configuration (manufacturer, model, os version) specific implementation as far as possible. So I'm wondering: what is the best way to achieve this?

推荐答案

更新:2014年1月2日: 我想真的很难避免实施基于设备制造商不同的策略。不幸的是,我并没有避开它。经历数百个职位,并谈几个开发​​商,没有人找到了一个解决方案上的所有设备,工程没有实施设备制造商特定的code。

UPDATE: January 2nd, 2014: I tried really hard to avoid implementing different strategies based on the device manufacturer. Unfortunately, I did not get around it. Going through hundreds of posts and talking to several developers, nobody found a solution that works on all devices without implementing device manufacturer specific code.

在我贴我的解决方案在这里的StackOverflow,一些开发商要求我公布我在GitHub上code。因此,这里现在是: AndroidCameraUtil在github

After I posted my solution here on StackOverflow, some developers asked me to publish my code on github. So here it is now: AndroidCameraUtil on github

在code是在多种与Android API级别> = 8的设备有关完整列表的试验成功,请参阅GitHub上的Readme文件。

The code was successfully tested on a wide variety of devices with Android API-Level >= 8. For a complete list, please see the Readme file on github.

的<一个href="https://github.com/ralfgehrer/AndroidCameraUtil/blob/master/src/de/ecotastic/android/camerautil/lib/CameraIntentHelperActivity.java">CameraIntentHelperActivity提供的主要功能,这是在下文中也更详细地描述

The CameraIntentHelperActivity provides the main functionality, which is also described in more detail in the following.

调用默认的摄像头活动:

  • 三星和索尼的设备的:我的方法调用startActivityForResult相机活动。我只设置不变CAPTURE_IMAGE_ACTIVITY_REQUEST_ code。我没有设置任何其他意图附加功能。
  • 所有其它设备的:我的方法调用startActivityForResult为previously相机活动。但是这一次,我加设的意图额外MediaStore.EXTRA_OUTPUT,并提供一个URI,在这里我想对图像进行存储。
  • for Samsung and Sony devices: I call the camera activity with the method call to startActivityForResult. I only set the constant CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE. I do NOT set any other intent extras.
  • for all other devices: I call the camera activity with the method call to startActivityForResult as previously. This time, however, I additionally set the intent extra MediaStore.EXTRA_OUTPUT and provide an URI, where I want the image to be stored.

在这两种情况下我记得摄像头活动开始的时间。

In both cases I remember the time the camera activity was started.

在摄像头的活动的结果:

  1. Mediastore:的首先,我尝试读取从MediaStore被抓获的照片。使用上MediaStore内容一个mangedQuery,我检索最新图像被采取,以及其取向性和其时间戳。如果我发现一个图像,它没有采取相机意向调用之前,这是我一直在寻找的形象。否则,我解雇的结果,请尝试以下方法之一。
  2. 意图额外的:的第二点,我试图让返回意图的图像乌里从intent.getData()。如果这不是成功要么,我继续执行步骤3。
  3. 默认照片乌里的:如果所有上述步骤没有工作,我用的是图像的Uri我通过相机的活动。
  1. Mediastore: First, I try to read the photo being captured from the MediaStore. Using a mangedQuery on the MediaStore content, I retrieve the latest image being taken, as well as its orientation property and its timestamp. If I find an image and it was not taken before the camera intent was called, it is the image I was looking for. Otherwise, I dismiss the result and try one of the following approaches.
  2. Intent extra: Second, I try to get an image Uri from intent.getData() of the returning intent. If this is not successful either, I continue with step 3.
  3. Default photo Uri: If all of the above mentioned steps did not work, I use the image Uri I passed to the camera activity.

在这一点上,我检索到的照片URI和它的方向,我传递给我的UploadPhotoActivity。

At this point, I retrieved the photo Uri and its orientation which I pass to my UploadPhotoActivity.

图像处理

请仔细看我的BitmapHelper类。它是基于详细在教程中描述的code

Please take a close look at my BitmapHelper class. It is based on the code described in detail in that tutorial.

此外,shrinkBitmap方法也转动,如果需要基于先前提取的取向信息的图像。

Moreover, the shrinkBitmap method also rotates the image if required based on the orientation information extracted earlier.

我希望这是有帮助的一些你。

I hope this is helpful to some of you.

这篇关于摘要:拍照使用相机意图,并显示照片的正确方向(适用于希望所有设备)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆