Android 在前后摄像头之间切换

Android 在前后摄像头之间切换,android,camera,rotation,android-camera,Android,Camera,Rotation,Android Camera,我使用的是谷歌的camera2示例,效果很好 但是我怎样才能用一个按钮在前后镜头之间切换呢 private void setUpCameraOutputs(int width, int height) { Activity activity = getActivity(); CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); try {

我使用的是谷歌的camera2示例,效果很好

但是我怎样才能用一个按钮在前后镜头之间切换呢

private void setUpCameraOutputs(int width, int height) {
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics
                    = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample. Para trocar, colocar != no lugar de ==.
            if (characteristics.get(CameraCharacteristics.LENS_FACING)
                    == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }

            StreamConfigurationMap map = characteristics.get(
                    CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            // For still image captures, we use the largest available size.
            Size largest = Collections.max(
                    Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                    new CompareSizesByArea());
            mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
                    ImageFormat.JPEG, /*maxImages*/2);
            mImageReader.setOnImageAvailableListener(
                    mOnImageAvailableListener, mBackgroundHandler);

            // Danger, W.R.! Attempting to use too large a preview size could  exceed the camera
            // bus' bandwidth limitation, resulting in gorgeous previews but the storage of
            // garbage capture data.
            mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                    width, height, largest);

            // We fit the aspect ratio of TextureView to the size of preview we picked.
            int orientation = getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                mTextureView.setAspectRatio(
                        mPreviewSize.getWidth(), mPreviewSize.getHeight());
            } else {
                mTextureView.setAspectRatio(
                        mPreviewSize.getHeight(), mPreviewSize.getWidth());
            }

            mCameraId = cameraId;
            return;
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        // Currently an NPE is thrown when the Camera2API is used but not supported on the
        // device this code runs.
        new ErrorDialog().show(getFragmentManager(), "dialog");
    }
}
`

以及此处的函数“closeCamera()”

 private void closeCamera() {
    mSession.close();
    mSession = null;
    mCameraDevice.close();
    mCameraDevice = null;
}

OP再也没有回来,但我演示了这段代码,它成功了。这正是我所需要的。如果前后摄像头的尺寸/纵横比不同,此方法将使用错误的前向摄像头纵横比。您有解决方案吗?感谢您提供此解决方案。此代码在旋转前置摄像头和再次旋转后置摄像头时运行良好。但当单击“保存图片”按钮时,在我使用前置摄像头时,它不会保存图片。后置摄像头拍照正常
 private void closeCamera() {
    mSession.close();
    mSession = null;
    mCameraDevice.close();
    mCameraDevice = null;
}