Camera2CameraInfo implements CameraInfo { @Override public int getSensorRotationDegrees(@RotationValue int relativeRotation) { Integer sensorOrientation = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); Preconditions.checkNotNull(sensorOrientation); int relativeRotationDegrees = CameraOrientationUtil.surfaceRotationToDegrees(relativeRotation); boolean isOppositeFacingScreen = LensFacing.BACK.equals(getLensFacing()); return CameraOrientationUtil.getRelativeImageRotation( relativeRotationDegrees, sensorOrientation, isOppositeFacingScreen); } Camera2CameraInfo(CameraManager cameraManager, String cameraId); @Nullable @Override LensFacing getLensFacing(); @Override int getSensorRotationDegrees(@RotationValue int relativeRotation); @Override int getSensorRotationDegrees();  }
@Test public void cameraInfo_canReturnSensorOrientation() throws CameraInfoUnavailableException { CameraInfo cameraInfo = new Camera2CameraInfo(mCameraManager, CAMERA0_ID); assertThat(cameraInfo.getSensorRotationDegrees()).isEqualTo(CAMERA0_SENSOR_ORIENTATION); }
@Test public void cameraInfo_canCalculateCorrectRelativeRotation_forBackCamera() throws CameraInfoUnavailableException { CameraInfo cameraInfo = new Camera2CameraInfo(mCameraManager, CAMERA0_ID); assertThat(cameraInfo.getSensorRotationDegrees(Surface.ROTATION_0)) .isEqualTo(CAMERA0_SENSOR_ORIENTATION); assertThat(cameraInfo.getSensorRotationDegrees(Surface.ROTATION_90)) .isEqualTo((CAMERA0_SENSOR_ORIENTATION - 90 + 360) % 360); assertThat(cameraInfo.getSensorRotationDegrees(Surface.ROTATION_180)) .isEqualTo((CAMERA0_SENSOR_ORIENTATION - 180 + 360) % 360); assertThat(cameraInfo.getSensorRotationDegrees(Surface.ROTATION_270)) .isEqualTo((CAMERA0_SENSOR_ORIENTATION - 270 + 360) % 360); }
@Test public void cameraInfo_canCalculateCorrectRelativeRotation_forFrontCamera() throws CameraInfoUnavailableException { CameraInfo cameraInfo = new Camera2CameraInfo(mCameraManager, CAMERA1_ID); assertThat(cameraInfo.getSensorRotationDegrees(Surface.ROTATION_0)) .isEqualTo(CAMERA1_SENSOR_ORIENTATION); assertThat(cameraInfo.getSensorRotationDegrees(Surface.ROTATION_90)) .isEqualTo((CAMERA1_SENSOR_ORIENTATION + 90) % 360); assertThat(cameraInfo.getSensorRotationDegrees(Surface.ROTATION_180)) .isEqualTo((CAMERA1_SENSOR_ORIENTATION + 180) % 360); assertThat(cameraInfo.getSensorRotationDegrees(Surface.ROTATION_270)) .isEqualTo((CAMERA1_SENSOR_ORIENTATION + 270) % 360); }