如何打开Android手电筒

时间:2011-08-10 01:38:05

标签: android camera lg flashlight

更新

查看我的答案

原始

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

这是我的清单:

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

我见过人们使用自动对焦,但我不知道这是否会奏效。

5 个答案:

答案 0 :(得分:7)

我想我会用一些适用于几乎所有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.
        }
}

答案 1 :(得分:1)

似乎Android Market上Tiny Flashlight + LED应用的开发者想出了如何让手电筒在LG Revolution上运作。

也许你可以联系他并问一下? 您还可以检查他在自己的应用中使用的权限,以尝试让您的应用运行!

祝你好运!

答案 2 :(得分:1)

测试一下:

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

答案 3 :(得分:0)

这对LG Nexus很有用:

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

答案 4 :(得分:0)

/*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;


    }
}