使用Camera API在Android上切换相机

时间:2016-06-28 07:18:57

标签: java android camera

你好,我是Camera API的新手。我正在学习它!我只是想把我的相机转回前后! 我的设备有前后摄像头! 我这样做:

public class CameraFrag extends Fragment {

//Variables
private Camera mCamera;
private CameraPreview mPreview;
private ToggleButton flipCamera;
////////////////////////////////////

public CameraFrag() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //inflate the layout
    View v = inflater.inflate(R.layout.frag, container, false);

    flipCamera = (ToggleButton) v.findViewById(R.id.flipper);

    // Create an instance of Camera
    mCamera = getCameraInstance();

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

    flipCamera.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            try{
                mCamera.stopPreview();

                if(isChecked){
                    mCamera=openFrontFacingCamera();
                }
                else{
                    mCamera=openBackFacingCamera();
                }
                mCamera.startPreview();
            }catch(Exception exp){
                Log.i("#LOGTAG","EXCEPTION "+exp);
            }
        }
    });

    return v;
}

private Camera openBackFacingCamera() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
        Camera.getCameraInfo(camIdx, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
            cam = Camera.open(camIdx);
        }
    }

    return cam;
}

private Camera openFrontFacingCamera() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
        Camera.getCameraInfo(camIdx, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            cam = Camera.open(camIdx);
        }
    }

    return cam;
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

}

问题在于它始终抛出异常:

I /#LOGTAG:EXCEPTION java.lang.RuntimeException:无法连接到摄像头服务

如果我使用我的代码:

              try{
                mCamera.stopPreview();
                mCamera.release();
                if(isChecked){
                    mCamera=openFrontFacingCamera();
                }
                else{
                    mCamera=openBackFacingCamera();
                }
                mCamera.startPreview();
            }catch(Exception exp){
                Log.i("#LOGTAG","EXCEPTION "+exp);
            }

我的预览冻结并在我点击后退按钮时返回!

我做错了什么?这是在相机之间切换的正确方法吗?有人可以帮帮我吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

只需打开所需的相机:

对于前置摄像头:

c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);

对于后置摄像头:

c = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);

确保您在完成后或使用以下代码的片段的onPause方法时始终释放您的相机,否则相机实例将永远不会被释放,甚至您的主相机应用程序也无法获取它。

private void releaseCameraAndPreview() {
    if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }
}