使用相机手电筒在Android中 [英] Use camera flashlight in Android

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

问题描述

我试图用相机LED手电筒在一个小部件。我已经发现了几个线程有关此主题(即<一href="http://stackoverflow.com/questions/5017455/how-to-use-camera-flash-led-as-torch-on-a-samsung-galaxy-tab">the 之一后面提到。),现在我想控制使用光:

I'm trying to use the cameras LED flashlight in a widget. I've found several threads about this topic (i.e. the one mentioned later..) , now I'm trying to control the light using:

Camera cam = Camera.open();     
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.release();

在AndroidManifest.xml中尝试了不同的权限,目前我有:

In the AndroidManifest.xml tried different permissions, currently I have:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />

我测试这对我的Galaxy Tab,因为我没有手头有任何其他Android设备:灯不亮。所以,我有几个问题,现在:

I'm testing this on my Galaxy Tab as I don't have any other Android devices at hand: the light does not turn on. So I have a few questions now:

  1. 有没有什么办法可以在模拟器中测试的LED灯的行为?
  2. 难道我做错了吗?
  3. 根据<一href="http://stackoverflow.com/questions/5017455/how-to-use-camera-flash-led-as-torch-on-a-samsung-galaxy-tab">this问题其中涉及同样的问题,它的工作方式不同的的Galaxy Tab。怎么样?
  4. 最后,如果它的工作方式不同,我开始怀疑,如果它只是Galaxy Tab的,或者其它设备使用不同的方法了。这将是难以测试的话,它似乎很奇怪,我。
  1. Is there any way to test the led light behavior in the Emulator?
  2. Am I doing something wrong here?
  3. According to this question which deals with the same problem, it works differently on the Galaxy Tab. How?
  4. And finally, if it does work differently, I'm starting to wonder if it's just the Galaxy Tab or if other devices use different methods too. It would be hard to test then and it seems rather odd to me.

感谢您的见解!

顺便说一句,我迅速用快速设置测试其中被提过几次这里。手电筒不与快速设置任何工作。

By the way, I quickly tested with quick-settings which gets mentioned a few times here. The flashlight doesn't work with quick-settings either.

请注意,Galaxy Tab的STIL采用Android 2.2。我看到有分别为2.2和2.3之间的一些变化。

Note that the Galaxy Tab stil uses android 2.2. I see there were some changes between 2.2 and 2.3.

评论: 我知道它的工作不知何故,我发现市场上其他的应用程序,与Galaxy Tab的正常工作。

Comment: I know it has to work somehow as I have found other apps in the market that work perfectly with the Galaxy Tab.

注释2: 如果我设置cam.setParameters(P);并直接询问相机的当前状态getFlashMode()是正确返回FLASH_MODE_TORCH。不过,如果我松开相机并重新打开,它会返回FLASH_MODE_OFF。这几乎就像aknowledges请求的Camera对象,但并没有真正将其传递给硬件!?

Comment 2: If I set cam.setParameters(p); and directly ask the camera for the current state with getFlashMode() it correctly returns FLASH_MODE_TORCH. However, if I release the camera and re-open it, it returns FLASH_MODE_OFF. It's almost as if the Camera object aknowledges the request but doesn't really pass it on to the hardware!?

-

在Konstantins评论,我删除了cam.release();部分。他是对的,如果你释放相机的设置不会保留。如果您使用cam.open()再次,你会得到与光掀起了新的实例。光仍然没有工作银河标签上虽然。 所以,我想这是很难保持的,如果你想通过一个小窗口来控制它,然后轻。只要后台服务结束后,相机对象被自动释放,因此,光再次关闭。我的问题仍然存在,特别是为什么相机没有在第一时间打开。

After Konstantins comment, I removed the cam.release(); part. He is right, the settings are not persisted if you release the camera. If you use cam.open() again, you will get a fresh instance with the light off. The light's still not working on the galaxy tab though. So, I guess it's hard to keep the light on if you're trying to control it through a widget then. As soon as the background service is finished, the camera object is released automatically and therefore the light switches off again. My questions still remain, especially why the camera doesn't switch on in the first place.

推荐答案

每个设备是一个有点不同。三星特别喜欢把事情复杂化的应用程序开发。

Every device is a bit different. Samsung especially likes to make things complicated for app developers.

在Galaxy Tab的,你应该是不错的搭配:

On the Galaxy Tab you should be good with:

Camera cam;
void ledon() {
    cam = Camera.open();     
    Parameters params = cam.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_ON);
    cam.setParameters(params);
    cam.startPreview();
    cam.autoFocus(new AutoFocusCallback() {
                public void onAutoFocus(boolean success, Camera camera) {
                }
            });
}

void ledoff() {
    cam.stopPreview();
    cam.release();
}

如果不工作,那么它可能是设置FLASH_MODE_OFF开始后开始preVIEW改变它的问题。

If that doesn't work then it might be a matter of setting FLASH_MODE_OFF initially and changing it after the startPreview.

这篇关于使用相机手电筒在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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