Android 上的相机示例 [英] Camera on Android Example

查看:22
本文介绍了Android 上的相机示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个活动:

  1. 显示相机预览(取景器),并有一个捕捉"按钮.
  2. 当按下捕获"按钮时,拍摄一张照片并将其返回给调用活动(setResult() & finish()).

是否有适用于每台设备完整示例?一个可以拍照的简单开源应用程序的链接将是理想的答案.

Are there any complete examples out there that works on every device? A link to a simple open source application that takes pictures would be the ideal answer.

我目前的研究:

这是一个常见的场景,有很多关于这个的问题和教程.

This is a common scenario, and there are many questions and tutorials on this.

主要有两种方法:

  1. 使用 android.provider.MediaStore.ACTION_IMAGE_CAPTURE 事件.请参阅这个问题
  2. 直接使用相机 API.请参阅 此示例这个问题(有很多参考资料).

方法 1 本来是完美的,但问题是每个设备上的意图实现方式不同.在某些设备上运行良好.但是,在某些设备上,您可以拍照,但它永远不会返回到您的应用程序.在某些设备上,当您启动 Intent 时没有任何反应. 通常它还会将图片保存到 SD 卡,并且需要 SD 卡存在.每个设备上的用户交互也不同.

Approach 1 would have been perfect, but the issue is that the intent is implemented differently on each device. On some devices it works well. However, on some devices you can take a picture but it is never returned to your app. On some devices nothing happens when you launch the intent. Typically it also saves the picture to the SD card, and requires the SD card to be present. The user interaction is also different on every device.

方法 2 的问题是稳定性.我尝试了一些示例,但我设法阻止相机在某些设备上工作(直到重新启动)并完全冻结另一台设备.在另一台设备上捕获成功,但预览仍然是黑色的.

With approach 2 the issues is stability. I tried some examples, but I've managed to stop the camera from working (until a restart) on some devices and completely freeze another device. On another device the capture worked, but the preview stayed black.

我会使用 ZXing 作为示例应用程序(我经常使用它),但它只使用预览(取景器),不拍照.我还发现,在某些设备上,ZXing 没有在光照条件发生变化时自动调整白平衡,而原生相机应用可以正常调整(不确定是否可以修复).

I would have used ZXing as an example application (I work with it a lot), but it only uses the preview (viewfinder), and doesn't take any pictures. I also found that on some devices, ZXing did not automatically adjust the white balance when the lighting conditions changed, while the native camera app did it properly (not sure if this can be fixed).

更新:

有一段时间我直接使用相机 API.这提供了更多控制(自定义 UI 等),但我不会向任何人推荐它.我会在 90% 的设备上工作,但时不时会发布新设备,但会遇到不同的问题.

For a while I used the camera API directly. This gives more control (custom UI, etc), but I would not recommend it to anyone. I would work on 90% of devices, but every now and again a new device would be released, with a different problem.

我遇到的一些问题:

  • 处理自动对焦
  • 处理闪光灯
  • 支持带有前置摄像头、后置摄像头或两者的设备
  • 每台设备都有不同的屏幕分辨率、预览分辨率(并不总是与屏幕分辨率匹配)和图片分辨率的组合.

所以总的来说,我不建议走这条路,除非没有其他办法.两年后,我放弃了自定义代码并切换回基于 Intent 的方法.从那以后,我遇到的麻烦少了很多.我过去在基于 Intent 的方法中遇到的问题可能只是我自己的无能.

So in general, I'd not recommend going this route at all, unless there is no other way. After two years I dumped by custom code and switched back to the Intent-based approach. Since then I've had much less trouble. The issues I've had with the Intent-based approach in the past was probably just my own incompetence.

如果你真的需要走这条路,我听说如果你只支持 Android 4.0+ 的设备会容易得多.

If you really need to go this route, I've heard it's much easier if you only support devices with Android 4.0+.

推荐答案

方法 2 的问题是稳定性.我尝试了一些示例,但我设法阻止相机在某些设备上工作(直到重新启动)并完全冻结另一台设备.在另一台设备上捕获成功,但预览仍然是黑色的.

With approach 2 the issues is stability. I tried some examples, but I've managed to stop the camera from working (until a restart) on some devices and completely freeze another device. On another device the capture worked, but the preview stayed black.

示例中存在错误或设备存在兼容性问题.

Either there is a bug in the examples or there is a compatibility issue with the devices.

这篇关于Android 上的相机示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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