简体   繁体   English

使用 Camera API 在 Android 上切换相机

[英]Switch Cameras on Android using Camera API

Hello There I am newbiew to Camera API.您好,我是 Camera API 的新手。 I am learning it to my own!我正在自学! I just want to switch my cameras back-front and front-back!我只想将我的相机前后切换! My device do have both front and back cameras!我的设备确实有前后摄像头! I am doing this like:我这样做是这样的:


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
}

} }


The problem is that It always throws an Exception as:问题是它总是抛出一个异常:

I/#LOGTAG: EXCEPTION java.lang.RuntimeException: Fail to connect to camera service I/#LOGTAG: EXCEPTION java.lang.RuntimeException: 连接相机服务失败


if I use my code as:如果我使用我的代码:

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

My preview Freezes and comes back when i click the back button!当我单击返回按钮时,我的预览冻结并返回!


What I am doing Wrong?我在做什么错? Is it the right way to switch between cameras?在相机之间切换是否正确? Can somebody help me please?有人可以帮我吗?

Thanks in advance!提前致谢!

Simply open the desired camera like this : 只需打开所需的相机,如下所示:

For front camera: 对于前置摄像头:

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

For back camera: 对于后置摄像头:

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

And make sure that you always release your camera when you are done or in the onPause method of your fragment using following code otherwise camera instance will never be released and even your main camera app will not be able to acquire it. 并确保在完成后始终释放相机或使用以下代码释放片段的onPause方法,否则相机实例将永远不会被释放,甚至您的主相机应用程序也无法获取它。

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

For anyone in the future facing the same problem.对于将来面临同样问题的任何人。 The error happens when the camera is already in use, I fixed the problem by first releasing the camera then setting it to null and opening it again.当相机已经在使用时会发生错误,我通过首先释放相机然后将其设置为空并再次打开它来解决问题。

//for front camera
 camera.release();
 camera = null;
 camera =  Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);

//for back camera
 camera.release();
 camera = null;
 camera =  Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM