2

我正在关注以下示例:http: //developer.android.com/guide/topics/media/camera.html

用于开发相机应用程序。

这个应用程序显示一个相机预览和一个按钮,当用户点击按钮时,照片被拍摄并保存,相机预览再次显示。

但我现在想切换到前置摄像头。关于如何做到这一点的任何想法?可以从前置或后置摄像头开始,但我无法在运行时更改它,就好像我调用了一个新的摄像头实例,应用程序崩溃了。

知道如何做到这一点吗?

提前致谢,佩马尔

4

4 回答 4

3

您一次只能打开一台相机。也就是说,您必须在前置摄像release()头之前使用后置摄像头。open(1)

于 2012-07-11T22:00:29.067 回答
1

您可以打开任何您想要的相机,请参见下面的链接

http://developer.android.com/reference/android/hardware/Camera.html#open(int)

如上面的链接所示,您应该使用

c = Camera.open(int) 而不是 c = Camera.open()

于 2012-04-18T13:42:14.173 回答
1

Here is how I've done:

private Camera mCamera=null;
private CameraPreview mPreview;
public FrameLayout preview;

        @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_preview);
    View myView= (View) findViewById(R.id.camera_previeww);
    myView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

    cameraID= Camera.CameraInfo.CAMERA_FACING_FRONT;
    mCamera=openCamera(cameraID);
    mCamera.startPreview();

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

public void switchCamera(){

    mCamera.stopPreview();
    releaseCamera();
    if (cameraID==Camera.CameraInfo.CAMERA_FACING_BACK){
        cameraID=Camera.CameraInfo.CAMERA_FACING_FRONT;
    }else{
        cameraID=Camera.CameraInfo.CAMERA_FACING_BACK;
    }

    mCamera=openCamera(cameraID);
    mCamera.startPreview();

    CameraPreview mPreview = new CameraPreview(this, mCamera);
    preview = (FrameLayout) this.findViewById(R.id.camera_previeww);
    preview.removeAllViews();
    preview.addView(mPreview);

}
public  Camera openCamera(int cameraIDD){
    Camera c=null;
    try{
        c=Camera.open(cameraIDD);
    }catch (Exception e){
        Log.d("Camera Activity", e.getMessage());
    }
    return c;

}

You have only to call switch() method to change from back to front or front to back. The activity was starting by calling startActivity(this, Class) from another activity.

于 2017-02-08T12:22:55.590 回答
0

一个可能的解决方案是获取 SurfaceView 的持有者(在我试图修复的场景是从前置摄像头切换到后置摄像头,反之亦然)。

    public void switchCamera(){
    Log.i(TAG, "Switching Camera");
    if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
        //mCamera = null;
    }

    //swap the id of the camera to be used
    if (camId == Camera.CameraInfo.CAMERA_FACING_BACK) {
        camId = Camera.CameraInfo.CAMERA_FACING_FRONT;
    }else {
        camId = Camera.CameraInfo.CAMERA_FACING_BACK;
    }
    try {
        mCamera = Camera.open(camId);
        //mCamera.setDisplayOrientation(90);
        //You must get the holder of SurfaceView!!!
        mCamera.setPreviewDisplay(mView.getHolder());
        //Then resume preview...
        mCamera.startPreview();
    }
    catch (Exception e) { e.printStackTrace(); }
}

希望它对遇到类似问题的人有所帮助。

于 2015-05-22T17:07:59.757 回答