1
1
import 'package:flutter/services.dart' ;
2
- import 'package:flutter/widgets.dart' ;
3
2
import 'package:mobile_scanner/src/enums/camera_facing.dart' ;
4
3
import 'package:mobile_scanner/src/enums/mobile_scanner_error_code.dart' ;
5
4
import 'package:mobile_scanner/src/mobile_scanner_exception.dart' ;
@@ -10,10 +9,10 @@ import 'package:mobile_scanner/src/utils/parse_device_orientation_extension.dart
10
9
class AndroidSurfaceProducerDelegate {
11
10
/// Construct a new [AndroidSurfaceProducerDelegate] .
12
11
AndroidSurfaceProducerDelegate ({
13
- required this .cameraIsFrontFacing ,
14
- required this .isPreviewPreTransformed ,
15
- required this .naturalOrientation ,
16
- required this .sensorOrientation ,
12
+ required this .cameraFacingDirection ,
13
+ required this .handlesCropAndRotation ,
14
+ required this .initialDeviceOrientation ,
15
+ required this .sensorOrientationDegrees ,
17
16
});
18
17
19
18
/// Construct a new [AndroidSurfaceProducerDelegate]
@@ -26,18 +25,19 @@ class AndroidSurfaceProducerDelegate {
26
25
) {
27
26
if (config
28
27
case {
29
- 'isPreviewPreTransformed ' : final bool isPreviewPreTransformed ,
28
+ 'handlesCropAndRotation ' : final bool handlesCropAndRotation ,
30
29
'naturalDeviceOrientation' : final String naturalDeviceOrientation,
31
- 'sensorOrientation' : final int sensorOrientation
30
+ 'sensorOrientation' : final int sensorOrientation,
32
31
}) {
33
32
final DeviceOrientation naturalOrientation =
34
33
naturalDeviceOrientation.parseDeviceOrientation ();
35
34
36
35
return AndroidSurfaceProducerDelegate (
37
- cameraIsFrontFacing: cameraDirection == CameraFacing .front,
38
- isPreviewPreTransformed: isPreviewPreTransformed,
39
- naturalOrientation: naturalOrientation,
40
- sensorOrientation: sensorOrientation,
36
+ cameraFacingDirection: cameraDirection,
37
+ handlesCropAndRotation: handlesCropAndRotation,
38
+ initialDeviceOrientation: naturalOrientation,
39
+ // FIXME: This is bad, will cause a flash/frame in the wrong rotation if started in another rotation.
40
+ sensorOrientationDegrees: sensorOrientation.toDouble (),
41
41
);
42
42
}
43
43
@@ -49,97 +49,17 @@ class AndroidSurfaceProducerDelegate {
49
49
);
50
50
}
51
51
52
- /// The rotation degrees corresponding to each device orientation.
53
- static const Map <DeviceOrientation , int > _degreesForDeviceOrientation =
54
- < DeviceOrientation , int > {
55
- DeviceOrientation .portraitUp: 0 ,
56
- DeviceOrientation .landscapeRight: 90 ,
57
- DeviceOrientation .portraitDown: 180 ,
58
- DeviceOrientation .landscapeLeft: 270 ,
59
- };
52
+ /// The facing direction of the active camera.
53
+ final CameraFacing cameraFacingDirection;
60
54
61
- /// Whether the current camera is a front facing camera .
55
+ /// Whether the underlying surface producer handles crop and rotation .
62
56
///
63
- /// This is used to determine whether the orientation correction
64
- /// should apply an additional correction for front facing cameras.
65
- final bool cameraIsFrontFacing;
57
+ /// If this is false, the preview needs to be manually rotated.
58
+ final bool handlesCropAndRotation;
66
59
67
- /// Whether the camera preview is pre-transformed,
68
- /// and thus does not need an orientation correction.
69
- final bool isPreviewPreTransformed;
60
+ /// The initial device orientation when this [AndroidSurfaceProducerDelegate] is created.
61
+ final DeviceOrientation initialDeviceOrientation;
70
62
71
- /// The initial orientation of the device, when the camera was started.
72
- ///
73
- /// The camera preview will use this orientation as the natural orientation
74
- /// to correct its rotation with respect to, if necessary.
75
- final DeviceOrientation naturalOrientation;
76
-
77
- /// The sensor orientation of the current camera, in degrees.
78
- final int sensorOrientation;
79
-
80
- /// Apply a rotation correction to the given [texture] widget.
81
- ///
82
- /// The [currentDeviceOrientation] is the current device orientation
83
- /// at the time this method is called.
84
- Widget applyRotationCorrection (
85
- Widget texture,
86
- DeviceOrientation currentDeviceOrientation,
87
- ) {
88
- int naturalDeviceOrientationDegrees =
89
- _degreesForDeviceOrientation[naturalOrientation]! ;
90
-
91
- if (isPreviewPreTransformed) {
92
- // If the camera preview is backed by a SurfaceTexture, the transformation
93
- // needed to correctly rotate the preview has already been applied.
94
- //
95
- // However, the camera preview rotation may need to be corrected if the
96
- // device is naturally landscape-oriented.
97
- if (naturalOrientation == DeviceOrientation .landscapeLeft ||
98
- naturalOrientation == DeviceOrientation .landscapeRight) {
99
- final int quarterTurns = (- naturalDeviceOrientationDegrees + 360 ) ~ / 4 ;
100
-
101
- return RotatedBox (
102
- quarterTurns: quarterTurns,
103
- child: texture,
104
- );
105
- }
106
-
107
- return texture;
108
- }
109
-
110
- // If the camera preview is not backed by a SurfaceTexture,
111
- // the camera preview rotation needs to be manually applied,
112
- // while also taking into account devices that are naturally landscape-oriented.
113
- final int signForCameraDirection = cameraIsFrontFacing ? 1 : - 1 ;
114
-
115
- // For front-facing cameras, the preview is rotated counterclockwise,
116
- // so determine the rotation needed to correct the camera preview with
117
- // respect to the natural orientation of the device, based on the inverse of
118
- // of the natural orientation.
119
- if (signForCameraDirection == 1 &&
120
- (currentDeviceOrientation == DeviceOrientation .landscapeLeft ||
121
- currentDeviceOrientation == DeviceOrientation .landscapeRight)) {
122
- naturalDeviceOrientationDegrees += 180 ;
123
- }
124
-
125
- // See https://developer.android.com/media/camera/camera2/camera-preview#orientation_calculation
126
- final double rotation = (sensorOrientation +
127
- naturalDeviceOrientationDegrees * signForCameraDirection +
128
- 360 ) %
129
- 360 ;
130
-
131
- int quarterTurnsToCorrectPreview = rotation ~ / 90 ;
132
-
133
- // Correct the camera preview rotation for devices that are naturally landscape-oriented.
134
- if (naturalOrientation == DeviceOrientation .landscapeLeft ||
135
- naturalOrientation == DeviceOrientation .landscapeRight) {
136
- quarterTurnsToCorrectPreview +=
137
- (- naturalDeviceOrientationDegrees + 360 ) ~ / 4 ;
138
- }
139
-
140
- return RotatedBox (
141
- quarterTurns: quarterTurnsToCorrectPreview,
142
- child: texture,
143
- );
144
- }
63
+ /// The orientation of the camera sensor on the device, in degrees.
64
+ final double sensorOrientationDegrees;
145
65
}
0 commit comments