Skip to content

Commit

Permalink
fix: rotation compensation
Browse files Browse the repository at this point in the history
  • Loading branch information
fbernaly committed Jul 6, 2023
1 parent ed1c254 commit 010c60c
Showing 1 changed file with 21 additions and 39 deletions.
60 changes: 21 additions & 39 deletions packages/example/lib/vision_detector_views/camera_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -317,54 +317,36 @@ class _CameraViewState extends State<CameraView> {
widget.onImage(inputImage);
}

final _orientations = {
DeviceOrientation.portraitUp: 0,
DeviceOrientation.landscapeLeft: 90,
DeviceOrientation.portraitDown: 180,
DeviceOrientation.landscapeRight: 270,
};

InputImage? _inputImageFromCameraImage(CameraImage image) {
if (_controller == null) return null;

// get camera rotation
final camera = _cameras[_cameraIndex];
var rotation =
InputImageRotationValue.fromRawValue(camera.sensorOrientation);
final sensorOrientation = camera.sensorOrientation;
var rotation = InputImageRotationValue.fromRawValue(sensorOrientation);
// print(
// 'lensDirection: ${camera.lensDirection}, rotation: ${camera.sensorOrientation} [$rotation], ${_controller?.value.deviceOrientation} ${_controller?.value.lockedCaptureOrientation} ${_controller?.value.isCaptureOrientationLocked}');
if (Platform.isAndroid) {
switch (camera.lensDirection) {
case CameraLensDirection.front:
switch (_controller!.value.deviceOrientation) {
case DeviceOrientation.portraitUp:
rotation = InputImageRotation.rotation270deg;
break;
case DeviceOrientation.landscapeLeft:
rotation = InputImageRotation.rotation0deg;
break;
case DeviceOrientation.portraitDown:
rotation = InputImageRotation.rotation90deg;
break;
case DeviceOrientation.landscapeRight:
rotation = InputImageRotation.rotation180deg;
break;
}
break;

case CameraLensDirection.back:
switch (_controller!.value.deviceOrientation) {
case DeviceOrientation.portraitUp:
rotation = InputImageRotation.rotation90deg;
break;
case DeviceOrientation.landscapeLeft:
rotation = InputImageRotation.rotation0deg;
break;
case DeviceOrientation.portraitDown:
rotation = InputImageRotation.rotation270deg;
break;
case DeviceOrientation.landscapeRight:
rotation = InputImageRotation.rotation180deg;
break;
}
break;

case CameraLensDirection.external:
break;
var rotationCompensation =
_orientations[_controller!.value.deviceOrientation];
if (rotationCompensation == null) return null;
if (camera.lensDirection == CameraLensDirection.front) {
// front-facing
rotationCompensation = (sensorOrientation + rotationCompensation) % 360;
} else {
// back-facing
rotationCompensation =
(sensorOrientation - rotationCompensation + 360) % 360;
}
rotation = InputImageRotationValue.fromRawValue(rotationCompensation);
// print('rotationCompensation: $rotationCompensation');
}
if (rotation == null) return null;
// print('final rotation: $rotation');
Expand Down

0 comments on commit 010c60c

Please sign in to comment.