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

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

问题描述

我正在尝试在小部件中使用摄像头 LED 手电筒.我发现了几个关于这个主题的线程(即 后面提到的那个 ..) ,现在我正在尝试使用以下方法控制灯光:

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. 根据这个 处理相同问题的问题,它在 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.

感谢您的任何见解!

顺便说一下,我很快就用提到的 quick-settings 进行了测试在这里几次.手电筒也不适用于快速设置.

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.就好像 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 并在 startPreview 之后更改它.

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