Julian Steenbakker

bug: fixed rotation on return image on iOS

@@ -85,38 +85,33 @@ class _BarcodeScannerReturningImageState @@ -85,38 +85,33 @@ class _BarcodeScannerReturningImageState
85 child: Row( 85 child: Row(
86 mainAxisAlignment: MainAxisAlignment.spaceEvenly, 86 mainAxisAlignment: MainAxisAlignment.spaceEvenly,
87 children: [ 87 children: [
88 - ColoredBox(  
89 - color: arguments != null && !arguments!.hasTorch  
90 - ? Colors.red  
91 - : Colors.white,  
92 - child: IconButton(  
93 - // color: ,  
94 - icon: ValueListenableBuilder(  
95 - valueListenable: controller.torchState,  
96 - builder: (context, state, child) {  
97 - if (state == null) { 88 + IconButton(
  89 + color: Colors.white,
  90 + icon: ValueListenableBuilder(
  91 + valueListenable: controller.torchState,
  92 + builder: (context, state, child) {
  93 + if (state == null) {
  94 + return const Icon(
  95 + Icons.flash_off,
  96 + color: Colors.grey,
  97 + );
  98 + }
  99 + switch (state as TorchState) {
  100 + case TorchState.off:
98 return const Icon( 101 return const Icon(
99 Icons.flash_off, 102 Icons.flash_off,
100 color: Colors.grey, 103 color: Colors.grey,
101 ); 104 );
102 - }  
103 - switch (state as TorchState) {  
104 - case TorchState.off:  
105 - return const Icon(  
106 - Icons.flash_off,  
107 - color: Colors.grey,  
108 - );  
109 - case TorchState.on:  
110 - return const Icon(  
111 - Icons.flash_on,  
112 - color: Colors.yellow,  
113 - );  
114 - }  
115 - },  
116 - ),  
117 - iconSize: 32.0,  
118 - onPressed: () => controller.toggleTorch(), 105 + case TorchState.on:
  106 + return const Icon(
  107 + Icons.flash_on,
  108 + color: Colors.yellow,
  109 + );
  110 + }
  111 + },
119 ), 112 ),
  113 + iconSize: 32.0,
  114 + onPressed: () => controller.toggleTorch(),
120 ), 115 ),
121 IconButton( 116 IconButton(
122 color: Colors.white, 117 color: Colors.white,
@@ -230,16 +230,16 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -230,16 +230,16 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
230 } 230 }
231 } 231 }
232 232
233 - /// Convert image buffer to jpeg  
234 - private func ciImageToJpeg(ciImage: CIImage) -> Data {  
235 -  
236 - // let ciImage = CIImage(cvPixelBuffer: latestBuffer)  
237 - let context:CIContext = CIContext.init(options: nil)  
238 - let cgImage:CGImage = context.createCGImage(ciImage, from: ciImage.extent)!  
239 - let uiImage:UIImage = UIImage(cgImage: cgImage, scale: 1, orientation: UIImage.Orientation.up)  
240 -  
241 - return uiImage.jpegData(compressionQuality: 0.8)!;  
242 - } 233 +// /// Convert image buffer to jpeg
  234 +// private func ciImageToJpeg(ciImage: CIImage) -> Data {
  235 +//
  236 +// // let ciImage = CIImage(cvPixelBuffer: latestBuffer)
  237 +// let context:CIContext = CIContext.init(options: nil)
  238 +// let cgImage:CGImage = context.createCGImage(ciImage, from: ciImage.extent)!
  239 +// let uiImage:UIImage = UIImage(cgImage: cgImage, scale: 1, orientation: UIImage.Orientation.up)
  240 +//
  241 +// return uiImage.jpegData(compressionQuality: 0.8)!;
  242 +// }
243 243
244 /// Rotates images accordingly 244 /// Rotates images accordingly
245 func imageOrientation( 245 func imageOrientation(
@@ -21,7 +21,7 @@ extension CVBuffer { @@ -21,7 +21,7 @@ extension CVBuffer {
21 var image: UIImage { 21 var image: UIImage {
22 let ciImage = CIImage(cvPixelBuffer: self) 22 let ciImage = CIImage(cvPixelBuffer: self)
23 let cgImage = CIContext().createCGImage(ciImage, from: ciImage.extent) 23 let cgImage = CIContext().createCGImage(ciImage, from: ciImage.extent)
24 - return UIImage(cgImage: cgImage!) 24 + return UIImage(cgImage: cgImage!, scale: 1.0, orientation: UIImage.Orientation.left)
25 } 25 }
26 26
27 var image1: UIImage { 27 var image1: UIImage {
@@ -52,24 +52,30 @@ class _MobileScannerState extends State<MobileScanner> @@ -52,24 +52,30 @@ class _MobileScannerState extends State<MobileScanner>
52 if (!controller.isStarting) controller.start(); 52 if (!controller.isStarting) controller.start();
53 } 53 }
54 54
55 - AppLifecycleState? _lastState; 55 + bool resumeFromBackground = false;
56 56
57 @override 57 @override
58 void didChangeAppLifecycleState(AppLifecycleState state) { 58 void didChangeAppLifecycleState(AppLifecycleState state) {
  59 +
  60 + // App state changed before it is initialized.
  61 + if (controller.isStarting) {
  62 + return;
  63 + }
  64 +
59 switch (state) { 65 switch (state) {
60 case AppLifecycleState.resumed: 66 case AppLifecycleState.resumed:
61 - if (!controller.isStarting &&  
62 - widget.autoResume &&  
63 - _lastState != AppLifecycleState.inactive) controller.start(); 67 + resumeFromBackground = false;
  68 + controller.start();
64 break; 69 break;
65 case AppLifecycleState.paused: 70 case AppLifecycleState.paused:
66 - case AppLifecycleState.detached:  
67 - controller.stop(); 71 + resumeFromBackground = true;
  72 + break;
  73 + case AppLifecycleState.inactive:
  74 + if (!resumeFromBackground) controller.stop();
68 break; 75 break;
69 default: 76 default:
70 break; 77 break;
71 } 78 }
72 - _lastState = state;  
73 } 79 }
74 80
75 @override 81 @override
@@ -202,7 +202,11 @@ class MobileScannerController { @@ -202,7 +202,11 @@ class MobileScannerController {
202 202
203 /// Stops the camera, but does not dispose this controller. 203 /// Stops the camera, but does not dispose this controller.
204 Future<void> stop() async { 204 Future<void> stop() async {
205 - await _methodChannel.invokeMethod('stop'); 205 + try {
  206 + await _methodChannel.invokeMethod('stop');
  207 + } catch (e) {
  208 + debugPrint('$e');
  209 + }
206 } 210 }
207 211
208 /// Switches the torch on or off. 212 /// Switches the torch on or off.