如何在按钮点击时从后置摄像头切换到前置摄像头?

时间:2015-11-30 06:12:30

标签: android camera android-camera

我知道我问的问题可能是重复的。 但我尝试了所有的事情,但没有成功。 我已经尝试以下方法来切换相机:

numberOfCamera = Camera.getNumberOfCameras();
            if(camId == Camera.CameraInfo.CAMERA_FACING_BACK){
                camId = Camera.CameraInfo.CAMERA_FACING_FRONT;
                Toast.makeText(getApplicationContext(), "BACK TO FRONT" ,
    1000).show();
                    try {
                        camera.release();
                        camera = Camera.open(camId);
                        camera.setPreviewDisplay(surfaceHolder);
                        camera.startPreview();
                        previewing = true;
                    } catch (RuntimeException e) {

                } catch (IOException e) {}
            }else if(camId == Camera.CameraInfo.CAMERA_FACING_FRONT){
                camId = Camera.CameraInfo.CAMERA_FACING_BACK;
                Toast.makeText(getApplicationContext(), "FRONT TO BACK" , 



     1000).show();
                        try {
                            camera.release();
                            camera = Camera.open(camId);
                            camera.setPreviewDisplay(surfaceHolder);
                            camera.startPreview();
                        } catch (RuntimeException e) {

                } catch (IOException e) {}
            }

- 此代码无法正常工作..它正在显示吐司..但没有切换相机。

1 个答案:

答案 0 :(得分:1)

这就是我在侧面项目中的表现。它是custom surface view,用于定义切换摄像机的方法。

注意:根据Alex的评论,我的初步答案不正确。我已经更新了答案,但我还没有对其进行测试,也没有更新上面链接的文件。

以下是对所做工作的总结:

您需要在类变量中使用当前的相机ID

user_tommail.rb

备份Camera.Parameters

您需要稍后重复使用高度和宽度信息。

int currentCameraId;

停止预览

Camera.Parameters parameters = camera.getParameters();

发布相机

if (inPreview) {
  camera.stopPreview();
}

获取新的相机ID

camera.release();

switchCamera方法(未经测试)

int cameraCount = Camera.getNumberOfCameras();
if (cameraCount == 2) {
    // simply switch between 0 and 1 if there are only 2 cameras
    currentCameraId = currentCameraId == 0 ? 1 : 0;
} else {
    Camera.CameraInfo currentCameraInfo = new Camera.CameraInfo();
    Camera.getCameraInfo(currentCameraId, currentCameraInfo);
    // switch between front and back facing
    int wantedCameraFacing = currentCameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ? Camera.CameraInfo.CAMERA_FACING_BACK : Camera.CameraInfo.CAMERA_FACING_FRONT;
    currentCameraId = switchCamera(cameraCount, wantedCameraFacing);
}

打开相机

int switchCamera(int cameraCount, int wantedCameraFacing) {
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    for (int camId = 0; camId < cameraCount; camId++) {
        Camera.getCameraInfo(camId, cameraInfo);
        if (cameraInfo.facing == wantedCameraFacing) {
            // selects the 1st camera which faces the correct way
            return camId;
        }
    }
}

初始预览并保存参数

camera = Camera.open(currentCameraId);

开始预览

camera.setPreviewDisplay(sHolder);
// define parameters, e.g. parameters.setPreviewSize()
camera.setParameters(parameters);

<强>用法

在活动布局中设置此自定义视图。为按钮的camera.startPreview(); 操作创建回调函数。然后你可以简单地打电话:

onClick