关闭后Android相机闪光灯无法开启

时间:2012-10-23 00:09:12

标签: android camera

我的相机预览屏幕上有一个按钮可以切换相机的闪光灯。相机开始自动闪光然后按下按钮时闪光灯关闭但是当我尝试重新开启闪光灯时它没有开启,我不知道为什么?

Log.d("flash",mCamera.getParameters().getFlashMode());

按下时显示关闭和自动。但它没有回头。这是我的完整代码

public void flashPressed(View v)
{
    ImageButton flashButton = (ImageButton)findViewById(R.id.flash);
    Camera.Parameters myP = mCamera.getParameters();
    if(flashOn == 0)
    {
        flashButton.setImageResource(R.drawable.device_access_flash_off);

        myP.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        flashOn = 1;

    }
    else if(flashOn == 1)
    {
        myP.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
        flashButton.setImageResource(R.drawable.device_access_flash_on);
        flashOn=2;
    }else{
        myP.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
        flashButton.setImageResource(R.drawable.device_access_flash_automatic);
        flashOn =0;
    }

    mCamera.setParameters(myP);
    Log.d("flash",mCamera.getParameters().getFlashMode());
}

2 个答案:

答案 0 :(得分:1)

这就是我用来创建Flash应用的方式。

public class Flash extends Activity {
    boolean cameraOpened;
    static Camera camFlash = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    @Override
    public void onPause() {
        super.onPause();
        if (camFlash != null) {
            camFlash.stopPreview();
            camFlash.release();
            camFlash = null;

        }

    }

    public void turnOnFlash(View view) {
        if (camFlash == null) {
            camFlash = Camera.open();
        }
        if (camFlash != null) {
            Parameters params = camFlash.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camFlash.setParameters(params);
            camFlash.startPreview();
            camFlash.autoFocus(new AutoFocusCallback() {
                public void onAutoFocus(boolean success, Camera camera) {
                }
            });

        }
    }

    public void turnOffFlash(View view) {
        if (camFlash != null) {
            camFlash.stopPreview();
            camFlash.release();
            camFlash = null;
        }
    }
}

从xml android:onClick调用方法turnOnFlash()和turnOffFlash。

答案 1 :(得分:0)

为避免此问题,我每次更改闪光灯选项时都会创建一个新相机