如何打开安卓手电筒 [英] How to turn on the Android Flashlight

查看:56
本文介绍了如何打开安卓手电筒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

看看我的回答

原创

我正在尝试在我的程序中打开 LG Revolution 上的相机手电筒.我使用适用于大多数手机但不适用于 LG 手机的手电筒模式方法.有谁知道如何让它在 LG 或特别是革命上工作?

I'm trying to turn on the camera flashlight on the LG Revolution within my program. I use the torch mode method which works on most phones but not on LG phone. Does anyone know how to get it to work on LG's or specifically the Revolution?

这是我的清单:

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

这是我当前的代码:

public Camera camera = Camera.open();
    public Camera.Parameters Flash = camera.getParameters();

在创建时:

            Flash.setFlashMode("torch");
            Parameters p = camera.getParameters();
            camera.setParameters(Flash);
            camera.startPreview();

我见过有人使用自动对焦,但我不知道这是否可行.

I've seen people use an auto focus but i don't know if that would work.

推荐答案

我想我会用一些适用于几乎所有 4.0+ 设备的项目符号 prof 代码来更新它.

I thought I would update this with some bullet prof code that works on almost all 4.0+ devices.

public void turnOn() {
    camera = Camera.open();
    try {
        Parameters parameters = camera.getParameters();
        parameters.setFlashMode(getFlashOnParameter());
        camera.setParameters(parameters);

        camera.setPreviewTexture(new SurfaceTexture(0));

        camera.startPreview();
        camera.autoFocus(this);
    } catch (Exception e) {
        // We are expecting this to happen on devices that don't support autofocus.
    }
}

private String getFlashOnParameter() {
    List<String> flashModes = camera.getParameters().getSupportedFlashModes();

    if (flashModes.contains(FLASH_MODE_TORCH)) {
        return FLASH_MODE_TORCH;
    } else if (flashModes.contains(FLASH_MODE_ON)) {
        return FLASH_MODE_ON;
    } else if (flashModes.contains(FLASH_MODE_AUTO)) {
        return FLASH_MODE_AUTO;
    }
    throw new RuntimeException();
}

真正的关键是设置假的 SurfaceTexture 以便实际开始预览.关闭它也很容易

The real key is setting that fake SurfaceTexture so that the preview will actually start. Turning it off is very easy as well

public void turnOff() {
        try {
            camera.stopPreview();
            camera.release();
            camera = null;
        } catch (Exception e) {
            // This will happen if the camera fails to turn on.
        }
}

这篇关于如何打开安卓手电筒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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