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

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

问题描述

我要写一个活动:


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



    每个设备上是否有完整示例?

我的研究到目前为止

My research so far:

这是一个常见的情况,有很多问题和教程。

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

有两种主要方法:


  1. 使用android.provider.MediaStore.ACTION_IMAGE_CAPTURE事件。请参见此问题

  2. 直接使用Camera API。请参见此示例这个问题(有很多引用)

  1. Use the android.provider.MediaStore.ACTION_IMAGE_CAPTURE event. See this question
  2. Use the Camera API directly. See this example or this question (with lots of references).

方法1本来是完美的,问题是在每个设备上实现不同的意图。 在某些设备上工作得很好。但是,在某些设备上,您可以拍摄照片,但它永远不会返回到您的应用程序。在某些设备上,当您启动该意图时,没有任何反应。通常它还会将图片保存到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.

我遇到的一些问题:


  • 处理自动对焦

  • 处理闪光灯


  • 每个设备的屏幕分辨率,预览分辨率(不总是与屏幕分辨率一致)和图片分辨率的不同组合。

  • Handling autofocus
  • Handling flash
  • Supporting devices with a front camera, back camera or both
  • Each device has a different combination of screen resolution, preview resolutions (doesn't always match the screen resolution) and picture resolutions.

所以一般来说,我不建议去这条路线,除非没有其他方法。两年后,我抛弃了自定义代码,并切换回基于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.

如果你真的需要去的话,我可能只是我自己的无能。

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