相机预览方向为横向,但设备为纵向

时间:2017-08-04 18:19:56

标签: android android-orientation android-camera2

我在camera2 API中预览的方向有问题。即使设备处于纵向模式,我所获得的预览仍处于横向状态。更准确地说,我得到的传感器旋转是90,因此传感器数据也被映射到90.观察结果在模拟器上(Nexus 5)。

private void startPreview() {
    SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
    surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
    Surface previewSurface = new Surface(surfaceTexture);

    try {
        mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mCaptureRequestBuilder.addTarget(previewSurface);

        mCameraDevice.createCaptureSession(Arrays.asList(previewSurface),
                new CameraCaptureSession.StateCallback() {
            @Override
            public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                try {
                    cameraCaptureSession.setRepeatingRequest(mCaptureRequestBuilder.build(),
                            null, mBackgroundHanlder);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {

            }
        }, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

}

private void setupCamera(int width, int height) {
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        for(String cameraId: cameraManager.getCameraIdList()) {
            CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
            if (cameraCharacteristics.get(CameraCharacteristics.LENS_FACING)
                    == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }
            StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            int deviceOrientation = getWindowManager().getDefaultDisplay().getRotation();
            mTotalRotation = sensorToDeviceRotation(cameraCharacteristics, deviceOrientation);
            boolean swapRotation = mTotalRotation == 90 || mTotalRotation == 270;
            int rotatedWidth = width;
            int rotatedHeight = height;

            if (swapRotation) {
                rotatedHeight = width;
                rotatedWidth = height;
            }
            mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedWidth, rotatedHeight);
            mVideoSize = chooseOptimalSize(map.getOutputSizes(MediaRecorder.class), rotatedWidth, rotatedHeight);

            mcameraId = cameraId;
            return;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}


private static int sensorToDeviceRotation(CameraCharacteristics cameraCharacteristics, int deviceOrientation) {
    int sensorOrientation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
    deviceOrientation = ORIENTATIONS.get(deviceOrientation);
    return (sensorOrientation+deviceOrientation+360)%360;
}

1 个答案:

答案 0 :(得分:0)

看起来你实际上并没有将旋转变换应用于输出帧。只是交换宽度/高度不会做任何事情,因为通常相机只支持横向分辨率。

您需要使用TextureView的getTransform和setTransform函数来添加必要的旋转。

具体做法是:

  1. 使用getTransform
  2. 读入当前转换
  3. 将您根据UI方向计算的必要旋转变换乘以变换
  4. 使用setTransform
  5. 将其设置为TextureView变换

    Camera2Basic示例应用程序的代码可能对此有所帮助,但我不确定它是否设置为纵向方向:https://github.com/googlesamples/android-Camera2Basic/blob/master/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java