相机在Android例 [英] Camera on Android Example

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

问题描述

我想写一个活动是:

  1. 显示摄像机preVIEW(取景器),并具有捕获按钮。
  2. 在当前捕获按钮是pressed,拍照,并将其返回到调用活动(的setResult()及完成())。

是否有任何的完成的例子在那里的每个设备上的作品?链接到一个简单的开源应用程序,也可以拍照将是理想的答案。


我的研究至今:

这是一个常见的​​场景,还有很多问题和教程这一点。

有两种主要的方法:

  1. 使用android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE事件。请参阅<一href="http://stackoverflow.com/questions/3442462/how-to-capture-an-image-and-store-it-with-the-native-android-camera">this问题
  2. 直接使用摄像头API。请参见这个例子或的这个问题(与大量的参考文献)

方法1将是完美的,但问题是,其意图是每个设备上的实现方式不同。 <删除>在某些设备上它工作得很好。然而,在某些设备上,你可以拍照,但它永远不会返回到您的应用程序。在某些设备上,当您启动的意图没有任何反应。通常还保存图片到SD卡,并且需要SD卡为present。用户交互也是每一个设备上的不同。

使用方法2的问题是稳定性。我尝试了一些例子,但我已经成功地从工作(直到重新启动)在某些设备上停止摄像,完全冻结其他设备。在其他设备上拍摄工作,但preVIEW留下黑色。

我会用ZXing作为一个示例应用程序(我用它工作了很多),但只使用了preVIEW(取景器),并且不采取任何照片。我还发现,在一些设备中,ZXing没有自动调整白平衡当照明条件改变,而原生相机应用做了正确的(不知道这是否可以固定)。


更新:

有一段时间,我直接用相机API。这使更多的控制权(自定义用户界面等),但我不会把它推荐给任何人。我将努力在90%的设备,但现在每一次新的设备将被释放,有一个不同的问题。

一些我遇到的问题:

  • 处理自动对焦
  • 处理闪存
  • 在支持的设备了前置摄像头,背面摄像头或两者
  • 在每个设备都有屏幕分辨率,preVIEW决议(并不总是与屏幕分辨率)和图像分辨率的不同组合。

因此​​,在一般情况下,我建议不要去这条路线可言,除非有没有其他办法。两年后我甩了自定义的code和切换回意图为基础的方法。从那时起,我已经少得多麻烦。我已经与过去的基于意图的方法的问题可能只是我自己的无能。

如果您的真正的需要走这条路,我听说它更容易,如果你只支持设备与Android 4.0 +。

解决方案
  

使用方法2的问题是稳定性。我尝试了一些例子,但我已经成功地从工作(直到重新启动)在某些设备上停止摄像,完全冻结其他设备。在其他设备上拍摄工作,但preVIEW留下黑色。

则可能是在例子中的错误或存在兼容性问题的设备。

I want to write an activity that:

  1. Shows the camera preview (viewfinder), and has a "capture" button.
  2. When the "capture" button is pressed, takes a picture and returns it to the calling activity (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.


My research so far:

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

There are two main approaches:

  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).

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.

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.

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).


Update:

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.

Some of the problems I've encountered:

  • 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.

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+.

解决方案

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