如何打开Android手电筒

发布于 2024-11-28 18:59:00 字数 808 浏览 2 评论 0原文

更新

查看我的答案

原始

我正在尝试在我的程序中打开LG Revolution上的相机手电筒。我使用的是手电筒模式方法,该方法适用于大多数手机,但不适用于 LG 手机。有谁知道如何让它在 LG 或特别是 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();

我看到人们使用自动对焦,但我不知道这是否有效。

Update

Check out my answer

Original

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?

Here's my manifest:

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

Here's my current code:

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

With my on create:

            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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

这个俗人 2024-12-05 18:59:00

我想我会用一些适用于几乎所有 4.0+ 设备的子弹教授代码来更新它。

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,以便预览真正开始。关闭它也很容易

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

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();
}

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.
        }
}
赏烟花じ飞满天 2024-12-05 18:59:00

看起来像是 Android 上 Tiny Flashlight + LED 应用程序的开发者Market 弄清楚了如何让 LG Revolution 上的手电筒正常工作。

也许你可以联系他问问?
您还可以检查他在其应用程序中使用的权限,以尝试使您的应用程序正常运行!

祝你好运!

It seems like the developer of the Tiny Flashlight + LED app on the Android Market figured out how to make the flashlight work on LG Revolution.

Maybe you can contact him and ask?
You can also check the permissions he is using in his app to try to make your app work!

Good luck!

美人迟暮 2024-12-05 18:59:00

测试一下:

if(camera == null){

camera = Camera.open();
parameters = camera.getParameters();

List<String> flashModes = parameters.getSupportedFlashModes();

    if(flashModes != null && flashModes.contains(Parameters.FLASH_MODE_TORCH)){

        //appareil supportant le mode torch
        parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(parameters);
    } else if (flashModes != null && flashModes.contains(Parameters.FLASH_MODE_ON)){

        //spécial samsung
        parameters.setFlashMode(Parameters.FLASH_MODE_ON);
        camera.setParameters(parameters);
        camera.startPreview();
        camera.autoFocus(new AutoFocusCallback() {
            public void onAutoFocus(boolean success, Camera camera) { }
        });
    } else {
        parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(parameters);
    }  

        parameters.setFlashMode( Parameters.FLASH_MODE_OFF );
        camera.setParameters(parameters);
        camera.release();
        camera = null;

    } catch (RuntimeException e) {}

}//if

Test this :

if(camera == null){

camera = Camera.open();
parameters = camera.getParameters();

List<String> flashModes = parameters.getSupportedFlashModes();

    if(flashModes != null && flashModes.contains(Parameters.FLASH_MODE_TORCH)){

        //appareil supportant le mode torch
        parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(parameters);
    } else if (flashModes != null && flashModes.contains(Parameters.FLASH_MODE_ON)){

        //spécial samsung
        parameters.setFlashMode(Parameters.FLASH_MODE_ON);
        camera.setParameters(parameters);
        camera.startPreview();
        camera.autoFocus(new AutoFocusCallback() {
            public void onAutoFocus(boolean success, Camera camera) { }
        });
    } else {
        parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(parameters);
    }  

        parameters.setFlashMode( Parameters.FLASH_MODE_OFF );
        camera.setParameters(parameters);
        camera.release();
        camera = null;

    } catch (RuntimeException e) {}

}//if
难得心□动 2024-12-05 18:59:00

这对于 LG Nexus 来说效果很好:

camera = Camera.open();
camera.setPreviewTexture(new SurfaceTexture(0));
camera.setParameters(p);
camera.startPreview();

This worked well for LG Nexus:

camera = Camera.open();
camera.setPreviewTexture(new SurfaceTexture(0));
camera.setParameters(p);
camera.startPreview();
焚却相思 2024-12-05 18:59:00
/*TESTED LG G4 */
public void flashOnOff(){
    List<String> flashModes = parameter001.getSupportedFlashModes();

    if(flashModes != null &&    flashModes.contains(Parameters.FLASH_MODE_TORCH)){

        //appareil supportant le mode torch
        parameter001.setFlashMode(Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(parameter001);
    } else if (flashModes != null && flashModes.contains(Parameters.FLASH_MODE_ON)){

        //spécial samsung
        parameter001.setFlashMode(Parameters.FLASH_MODE_ON);
        mCamera.setParameters(parameter001);

        mCamera.autoFocus(new AutoFocusCallback() {
            public void onAutoFocus(boolean success, Camera camera) { }
        });
    } else {
        parameter001.setFlashMode(Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(parameter001);
    } 
    if (!isFlashOn) {
        if (mCamera == null || parameter001 == null) {
            return;
        }

        parameter001 = mCamera.getParameters();
        parameter001.setFlashMode(Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(parameter001);
        try {
            mCamera.setPreviewTexture(new SurfaceTexture(0));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mCamera.startPreview();
        isFlashOn = true;

        // changing button/switch image

    }else if (isFlashOn) {
        if (mCamera == null || parameter001 == null) {
            return;
        }

        parameter001 = mCamera.getParameters();
        parameter001.setFlashMode(Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(parameter001);
        mCamera.stopPreview();
        isFlashOn = false;


    }
}
/*TESTED LG G4 */
public void flashOnOff(){
    List<String> flashModes = parameter001.getSupportedFlashModes();

    if(flashModes != null &&    flashModes.contains(Parameters.FLASH_MODE_TORCH)){

        //appareil supportant le mode torch
        parameter001.setFlashMode(Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(parameter001);
    } else if (flashModes != null && flashModes.contains(Parameters.FLASH_MODE_ON)){

        //spécial samsung
        parameter001.setFlashMode(Parameters.FLASH_MODE_ON);
        mCamera.setParameters(parameter001);

        mCamera.autoFocus(new AutoFocusCallback() {
            public void onAutoFocus(boolean success, Camera camera) { }
        });
    } else {
        parameter001.setFlashMode(Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(parameter001);
    } 
    if (!isFlashOn) {
        if (mCamera == null || parameter001 == null) {
            return;
        }

        parameter001 = mCamera.getParameters();
        parameter001.setFlashMode(Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(parameter001);
        try {
            mCamera.setPreviewTexture(new SurfaceTexture(0));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mCamera.startPreview();
        isFlashOn = true;

        // changing button/switch image

    }else if (isFlashOn) {
        if (mCamera == null || parameter001 == null) {
            return;
        }

        parameter001 = mCamera.getParameters();
        parameter001.setFlashMode(Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(parameter001);
        mCamera.stopPreview();
        isFlashOn = false;


    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文