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

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

问题描述

这似乎是世界上最简单的事情:使用默认的相机活动在您的 Android 应用中拍照.但是,StackOverflow 和网络上的几篇文章中都涵盖了许多陷阱,例如传回 Null Intent、图片方向不正确或 OutOfMemoryErrors.

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 日:我非常努力地避免根据设备制造商实施不同的策略.不幸的是,我没有绕过它.在浏览了数百篇帖子并与几位开发人员交谈后,没有人找到一种无需实施设备制造商特定代码即可适用于所有设备的解决方案.

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 上发布我的代码.所以现在是:AndroidCameraUtil on 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

该代码已在 Android API 级别 >= 8 的各种设备上成功测试.有关完整列表,请参阅 github 上的自述文件.

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.

CameraIntentHelperActivity 提供了主要的功能,下面也有更详细的描述.

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

调用默认相机活动:

  • 对于三星和索尼设备:我通过调用 startActivityForResult 的方法调用相机活动.我只设置了常量 CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE.我没有设置任何其他意图附加项.
  • 对于所有其他设备:我像以前一样通过调用 startActivityForResult 的方法调用相机活动.但是,这一次,我额外设置了意图 extra 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 extra: 其次,我尝试从返回意图的 intent.getData() 中获取图像 Uri.如果这也不成功,我会继续第 3 步.
  3. 默认照片 Uri:如果上述所有步骤都不起作用,我将使用我传递给相机活动的图像 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.

此时,我检索了我传递给 UploadPhotoActivity 的照片 Uri 及其方向.

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

图像处理

请仔细查看我的 BitmapHelper 类.它基于在该教程中详细描述的代码.

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天全站免登陆