1
1
import 'dart:io' ;
2
- import 'dart:ui' ;
3
-
4
2
import 'package:camera/camera.dart' ;
5
3
import 'package:gallery_saver/gallery_saver.dart' ;
6
4
import 'package:get/get.dart' ;
@@ -11,6 +9,7 @@ import 'package:qrscanner/utils/snackbar.dart';
11
9
12
10
class CameraViewController extends GetxController {
13
11
final YOLOController _yoloController = Get .find <YOLOController >();
12
+
14
13
CameraController ? cameraController;
15
14
List <CameraDescription >? cameras;
16
15
CameraImage ? cameraImage;
@@ -20,7 +19,6 @@ class CameraViewController extends GetxController {
20
19
21
20
var zoomLevel = 1.0 .obs;
22
21
var maxZoomLevel = 1.0 .obs;
23
-
24
22
var isTakingPicture = false .obs;
25
23
26
24
@override
@@ -30,32 +28,35 @@ class CameraViewController extends GetxController {
30
28
}
31
29
32
30
Future <void > initializeCamera () async {
33
- cameras = await availableCameras ();
34
- if (cameras! .isNotEmpty) {
35
- cameraController = CameraController (
36
- cameras! [0 ],
37
- ResolutionPreset .medium,
38
- imageFormatGroup: Platform .isAndroid
39
- ? ImageFormatGroup .yuv420
40
- : ImageFormatGroup .bgra8888,
41
- );
42
-
43
- camFrameRotation = Platform .isAndroid ? cameras! [0 ].sensorOrientation : 0 ;
44
- await cameraController? .initialize ().then ((_) async {
45
- // Stream of image passed to [onLatestImageAvailable] callback
46
- await cameraController? .startImageStream ((CameraImage image) =>
47
- _yoloController.onEachCameraImage (
48
- image, camFrameRotation, setCameraZoomLevel));
49
-
50
- logger.i (cameraController! .value.previewSize.toString ());
51
-
52
- ratio = cameraController! .value.aspectRatio;
53
-
54
- logger.i (ratio);
55
- });
56
- maxZoomLevel.value = await cameraController! .getMaxZoomLevel ();
57
- logger.i (maxZoomLevel);
58
- update (); // Using update() to rebuild GetBuilder widgets
31
+ try {
32
+ cameras = await availableCameras (); // check for available cameras
33
+ if (cameras! .isNotEmpty) {
34
+ cameraController = CameraController (
35
+ cameras! [0 ],
36
+ ResolutionPreset .medium,
37
+ imageFormatGroup: Platform .isAndroid
38
+ ? ImageFormatGroup .yuv420
39
+ : ImageFormatGroup .bgra8888,
40
+ );
41
+
42
+ camFrameRotation =
43
+ Platform .isAndroid ? cameras! [0 ].sensorOrientation : 0 ;
44
+
45
+ await cameraController? .initialize ().then ((_) async {
46
+ maxZoomLevel.value = await cameraController! .getMaxZoomLevel ();
47
+ // Stream of image passed to [onLatestImageAvailable] callback
48
+ await cameraController? .startImageStream ((CameraImage image) async =>
49
+ _yoloController.onEachCameraImage (
50
+ image, camFrameRotation, setCameraZoomLevel));
51
+
52
+ ratio = cameraController! .value.aspectRatio;
53
+ });
54
+
55
+ update (); // Using update() to rebuild GetBuilder widgets
56
+ }
57
+ } catch (e) {
58
+ showSnackBar ('Error' , 'something went wrong!' );
59
+ logger.log (level, e);
59
60
}
60
61
}
61
62
@@ -69,40 +70,59 @@ class CameraViewController extends GetxController {
69
70
if (cameraController! .value.isTakingPicture) {
70
71
return ;
71
72
}
73
+
74
+ // capture picture
72
75
final XFile file = await cameraController! .takePicture ();
76
+
77
+ //save image to gallery
73
78
await GallerySaver .saveImage (file.path).then ((bool ? isSaved) {
74
79
if (isSaved ?? false ) {
75
80
showSnackBar ('Success' , 'Picture saved to Gallery' );
76
81
}
77
82
});
78
83
} on CameraException catch (e) {
79
84
showSnackBar ('Error' , 'something went wrong!' );
80
- print ( e);
85
+ logger. log (level, e);
81
86
return ;
82
87
}
83
88
}
84
89
90
+ /// takes List of predictions and increases or decreases zoom level according to width of the QR code
85
91
void setCameraZoomLevel (List <ResultObjectDetection > predictions) async {
86
92
double level = zoomLevel.value;
87
93
94
+ predictions.sort ((a, b) =>
95
+ b.score.compareTo (a.score)); // sort predictions according to score
96
+
88
97
if (predictions.isNotEmpty) {
89
98
if (predictions.first.rect.width <= 0.8 &&
90
99
predictions.first.rect.left >= 0.1 &&
91
100
predictions.first.rect.right >= 0.1 &&
92
101
predictions.first.rect.top >= 0.1 &&
93
102
predictions.first.rect.bottom >= 0.1 ) {
94
- print (
95
- "Since width is ${predictions .first .rect .width } -> increasing level to ${level + 0.15 }" );
96
- level += 0.15 ;
97
- if (level > maxZoomLevel.value) level = maxZoomLevel.value;
98
- } else if (predictions.first.rect.width > 0.9 ) {
99
- print (
100
- "Since width is ${predictions .first .rect .width } -> decreasing level to ${level - 0.2 }" );
103
+ if (predictions.first.rect.width > 0 &&
104
+ predictions.first.rect.width < 0.3 ) {
105
+ level += 0.3 ;
106
+ } else if (predictions.first.rect.width >= 0.3 &&
107
+ predictions.first.rect.width < 0.5 ) {
108
+ level += 0.2 ;
109
+ } else if (predictions.first.rect.width >= 0.5 &&
110
+ predictions.first.rect.width < 0.7 ) {
111
+ level += 0.15 ;
112
+ } else if (predictions.first.rect.width >= 0.7 &&
113
+ predictions.first.rect.width <= 0.75 ) {
114
+ level += 0.1 ;
115
+ }
116
+ } else if (predictions.first.rect.width >= 0.9 &&
117
+ predictions.first.rect.width < 1.5 ) {
101
118
level -= 0.2 ;
102
- if (level < 1 ) level = 1 ;
103
119
}
104
120
}
105
-
121
+ if (level > maxZoomLevel.value) {
122
+ level = maxZoomLevel.value;
123
+ } else if (level < 1 ) {
124
+ level = 1 ;
125
+ }
106
126
if (zoomLevel.value != level) {
107
127
zoomLevel.value = level;
108
128
await cameraController? .setZoomLevel (level);
0 commit comments