无法连接到Android中的相机服务

时间:2014-01-24 12:54:34

标签: android video android-camera surfaceview record

我想在后置和前置摄像头之间切换以录制视频。 这是我的代码:

public void surfaceCreated(SurfaceHolder holder) {

    if (usecamera) {
        camera = Camera.open(0);
        camera.setDisplayOrientation(90);

        try {
            camera.setPreviewDisplay(holder);
            camera.startPreview();
            previewRunning = true;
        }
        catch (IOException e) {
            Log.e(LOGTAG,e.getMessage());
            e.printStackTrace();
        }   
    }       

}


public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    if (!recording && usecamera) {
        if (previewRunning){
            camera.stopPreview();
        }

        try {
            Camera.Parameters p = camera.getParameters();

            p.setPreviewSize(camcorderProfile.videoFrameWidth, camcorderProfile.videoFrameHeight);

            camera.setParameters(p);

            camera.setPreviewDisplay(holder);
            camera.startPreview();
            previewRunning = true;
        }
        catch (IOException e) {
            Log.e(LOGTAG,e.getMessage());
            e.printStackTrace();
        }   

        prepareRecorder();  
    }
}


public void surfaceDestroyed(SurfaceHolder holder) {
    if (recording) {
        recorder.stop();
        recording = false;
    }
    recorder.release();
    if (usecamera) {
        previewRunning = false;
        //camera.lock();
        camera.release();
    }

}

在第一个例子中,后置摄像头的预览是正确的,但是当我尝试更换摄像头时崩溃:

更改按钮是:

changeCamera.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

             if (Camera.getNumberOfCameras()>=2) {
                    camera.stopPreview();
                    camera.release();
                    //"which" is just an integer flag
                    switch (which) {
                    case 0:
                        camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
                        which = 1;
                        break;
                    case 1:
                        camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
                        which = 0;
                        break;
                    }
                    try {
                        camera.setPreviewDisplay(holder);
                        camera.startPreview();
                    } catch (IOException exception) {
                        camera.release();
                        camera = null;
                    }
                }

        }
    });

1 个答案:

答案 0 :(得分:2)

不幸的是,Android Camera API并不容易理解。有两个 int constants

public final static int Camera.CameraInfo.CAMERA_FACING_BACK = 0;
public final static int Camera.CameraInfo.CAMERA_FACING_FRONT = 1;

并且有一些方法,例如期望int cameraId参数的Camera.open()

但是对Camera.open()CamcorderProfile.get()使用上面的常量是错误的。第一台相机 cameraId 0 ,第二台 1 ,第二台 2 ,依此类推到Camera.getNumberOfCameras()-1。您可以使用以下 cameraId 检查相机的方向

Camera camera = Camera.open(cameraId);
int facing = camera.getCameraInfo().facing();

面对的价值保证为Camera.CameraInfo.CAMERA_FACING_BACKCamera.CameraInfo.CAMERA_FACING_FRONT