相机和LED在一起

时间:2013-09-10 09:22:23

标签: android camera led flashlight

我尝试在我的Android设备上打开相机和LED。 使用此示例:http://developer.android.com/guide/topics/media/camera.html

这是我的代码:

mCamera = getCameraInstance();
    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

    params = mCamera.getParameters();
    params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    mCamera.setParameters(params);
    mCamera.startPreview();

但是在打开LED后,它会工作1秒然后关闭。

2 个答案:

答案 0 :(得分:0)

检查此代码..我没有尝试过..

code

答案 1 :(得分:0)

如果你评论//mCamera.startPreview();怎么办?你的手电筒可以继续工作吗?这是我为使手电筒继续在我的设备上工作而实施的代码。您可以验证手电筒是否可以在您的设备上正常工作。

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

public class MainActivity extends Activity {
    private Button mSwitchButton = null;
    private Camera mCamera;
    private Camera.Parameters mParameters;
    private boolean mbTorchEnabled = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mSwitchButton = new Button(this);
        mSwitchButton.setText("switch");
        mSwitchButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mParameters = mCamera.getParameters();

                List flashModes = mParameters.getSupportedFlashModes ();

                if(flashModes != null && flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)){
                    if(mbTorchEnabled){
                        //Set the flash parameter to off
                        mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    }
                    else{
                        //Set the flash parameter to use the torch
                        mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    }

                    //Commit the camera parameters
                    mCamera.setParameters(mParameters);

                    mbTorchEnabled = !mbTorchEnabled;
                }
            }
        });

        setContentView(mSwitchButton);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mCamera = Camera.open();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        mCamera.release();
    }
}