Showing
1 changed file
with
92 additions
and
230 deletions
| 1 | - | ||
| 2 | -// import 'dart:ui'; | ||
| 3 | - | ||
| 4 | -// import 'package:flutter/material.dart'; | ||
| 5 | -// import 'package:mobile_scanner/mobile_scanner.dart'; | ||
| 6 | - | ||
| 7 | -// void main() { | ||
| 8 | -// runApp(const AnalyzeView()); | ||
| 9 | -// } | ||
| 10 | - | ||
| 11 | -// class AnalyzeView extends StatefulWidget { | ||
| 12 | -// const AnalyzeView({Key? key}) : super(key: key); | ||
| 13 | - | ||
| 14 | -// @override | ||
| 15 | -// _AnalyzeViewState createState() => _AnalyzeViewState(); | ||
| 16 | -// } | ||
| 17 | - | ||
| 18 | -// class _AnalyzeViewState extends State<AnalyzeView> | ||
| 19 | -// with SingleTickerProviderStateMixin { | ||
| 20 | -// List<Offset> points = []; | ||
| 21 | - | ||
| 22 | -// // CameraController cameraController = CameraController(context, width: 320, height: 150); | ||
| 23 | - | ||
| 24 | -// String? barcode; | ||
| 25 | - | ||
| 26 | -// @override | ||
| 27 | -// Widget build(BuildContext context) { | ||
| 28 | -// return MaterialApp( | ||
| 29 | -// home: Scaffold( | ||
| 30 | -// body: Builder(builder: (context) { | ||
| 31 | -// return Stack( | ||
| 32 | -// children: [ | ||
| 33 | -// MobileScanner( | ||
| 34 | -// // fitScreen: false, | ||
| 35 | -// // controller: cameraController, | ||
| 36 | -// onDetect: (barcode) { | ||
| 37 | -// if (this.barcode != barcode.rawValue) { | ||
| 38 | -// this.barcode = barcode.rawValue; | ||
| 39 | -// if (barcode.corners != null) { | ||
| 40 | -// ScaffoldMessenger.of(context).showSnackBar(SnackBar( | ||
| 41 | -// content: Text(barcode.rawValue), | ||
| 42 | -// duration: const Duration(milliseconds: 200), | ||
| 43 | -// animation: null, | ||
| 44 | -// )); | ||
| 45 | -// setState(() { | ||
| 46 | -// final List<Offset> points = []; | ||
| 47 | -// // double factorWidth = args.size.width / 520; | ||
| 48 | -// // double factorHeight = wanted / args.size.height; | ||
| 49 | -// final size = MediaQuery.of(context).devicePixelRatio; | ||
| 50 | -// debugPrint('Size: ${barcode.corners}'); | ||
| 51 | -// for (var point in barcode.corners!) { | ||
| 52 | -// final adjustedWith = point.dx; | ||
| 53 | -// final adjustedHeight = point.dy; | ||
| 54 | -// points.add( | ||
| 55 | -// Offset(adjustedWith / size, adjustedHeight / size)); | ||
| 56 | -// // points.add(Offset((point.dx ) / size, | ||
| 57 | -// // (point.dy) / size)); | ||
| 58 | -// // final differenceWidth = (args.wantedSize!.width - args.size.width) / 2; | ||
| 59 | -// // final differenceHeight = (args.wantedSize!.height - args.size.height) / 2; | ||
| 60 | -// // points.add(Offset((point.dx + differenceWidth) / size, | ||
| 61 | -// // (point.dy + differenceHeight) / size)); | ||
| 62 | -// } | ||
| 63 | -// this.points = points; | ||
| 64 | -// }); | ||
| 65 | -// } | ||
| 66 | -// } | ||
| 67 | -// // Default 640 x480 | ||
| 68 | -// }), | ||
| 69 | -// CustomPaint( | ||
| 70 | -// painter: OpenPainter(points), | ||
| 71 | -// ), | ||
| 72 | -// // Container( | ||
| 73 | -// // alignment: Alignment.bottomCenter, | ||
| 74 | -// // margin: EdgeInsets.only(bottom: 80.0), | ||
| 75 | -// // child: IconButton( | ||
| 76 | -// // icon: ValueListenableBuilder( | ||
| 77 | -// // valueListenable: cameraController.torchState, | ||
| 78 | -// // builder: (context, state, child) { | ||
| 79 | -// // final color = | ||
| 80 | -// // state == TorchState.off ? Colors.grey : Colors.white; | ||
| 81 | -// // return Icon(Icons.bolt, color: color); | ||
| 82 | -// // }, | ||
| 83 | -// // ), | ||
| 84 | -// // iconSize: 32.0, | ||
| 85 | -// // onPressed: () => cameraController.torch(), | ||
| 86 | -// // ), | ||
| 87 | -// // ), | ||
| 88 | -// ], | ||
| 89 | -// ); | ||
| 90 | -// }), | ||
| 91 | -// ), | ||
| 92 | -// ); | ||
| 93 | -// } | ||
| 94 | - | ||
| 95 | -// @override | ||
| 96 | -// void dispose() { | ||
| 97 | -// // cameraController.dispose(); | ||
| 98 | -// super.dispose(); | ||
| 99 | -// } | ||
| 100 | - | ||
| 101 | -// void display(Barcode barcode) { | ||
| 102 | -// Navigator.of(context).popAndPushNamed('display', arguments: barcode); | ||
| 103 | -// } | ||
| 104 | -// } | ||
| 105 | - | ||
| 106 | -// class OpenPainter extends CustomPainter { | ||
| 107 | -// final List<Offset> points; | ||
| 108 | - | ||
| 109 | -// OpenPainter(this.points); | ||
| 110 | -// @override | ||
| 111 | -// void paint(Canvas canvas, Size size) { | ||
| 112 | -// var paint1 = Paint() | ||
| 113 | -// ..color = const Color(0xff63aa65) | ||
| 114 | -// ..strokeWidth = 10; | ||
| 115 | -// //draw points on canvas | ||
| 116 | -// canvas.drawPoints(PointMode.points, points, paint1); | ||
| 117 | -// } | ||
| 118 | - | ||
| 119 | -// @override | ||
| 120 | -// bool shouldRepaint(CustomPainter oldDelegate) => true; | ||
| 121 | -// } | ||
| 122 | - | ||
| 123 | -// class OpacityCurve extends Curve { | ||
| 124 | -// @override | ||
| 125 | -// double transform(double t) { | ||
| 126 | -// if (t < 0.1) { | ||
| 127 | -// return t * 10; | ||
| 128 | -// } else if (t <= 0.9) { | ||
| 129 | -// return 1.0; | ||
| 130 | -// } else { | ||
| 131 | -// return (1.0 - t) * 10; | ||
| 132 | -// } | ||
| 133 | -// } | ||
| 134 | -// } | ||
| 135 | - | ||
| 136 | import 'package:flutter/material.dart'; | 1 | import 'package:flutter/material.dart'; |
| 137 | -import 'package:flutter/rendering.dart'; | ||
| 138 | import 'package:mobile_scanner/mobile_scanner.dart'; | 2 | import 'package:mobile_scanner/mobile_scanner.dart'; |
| 139 | -import 'package:mobile_scanner_example/barcode_scanner_window.dart'; | ||
| 140 | import 'package:mobile_scanner_example/scanner_error_widget.dart'; | 3 | import 'package:mobile_scanner_example/scanner_error_widget.dart'; |
| 141 | 4 | ||
| 142 | -void main() { | ||
| 143 | - debugPaintSizeEnabled = false; | ||
| 144 | - runApp(HomePage()); | ||
| 145 | -} | ||
| 146 | - | ||
| 147 | -class HomePage extends StatefulWidget { | ||
| 148 | - @override | ||
| 149 | - HomeState createState() => HomeState(); | ||
| 150 | -} | ||
| 151 | - | ||
| 152 | -class HomeState extends State<HomePage> { | ||
| 153 | - @override | ||
| 154 | - Widget build(BuildContext context) { | ||
| 155 | - return MaterialApp(home: BarcodeScannerWithOverlay()); | ||
| 156 | - } | ||
| 157 | -} | ||
| 158 | - | ||
| 159 | class BarcodeScannerWithOverlay extends StatefulWidget { | 5 | class BarcodeScannerWithOverlay extends StatefulWidget { |
| 160 | @override | 6 | @override |
| 161 | - _MyAppState createState() => _MyAppState(); | 7 | + _BarcodeScannerWithOverlayState createState() => |
| 8 | + _BarcodeScannerWithOverlayState(); | ||
| 162 | } | 9 | } |
| 163 | 10 | ||
| 164 | -class _MyAppState extends State<BarcodeScannerWithOverlay> { | ||
| 165 | - String? qr; | 11 | +class _BarcodeScannerWithOverlayState extends State<BarcodeScannerWithOverlay> { |
| 166 | String overlayText = "Please scan QR Code"; | 12 | String overlayText = "Please scan QR Code"; |
| 167 | - bool camState = false; | ||
| 168 | - late BarcodeCapture currentBarcodeCapture ; | 13 | + bool camStarted = false; |
| 14 | + BarcodeCapture? currentBarcodeCapture; | ||
| 169 | final MobileScannerController controller = MobileScannerController( | 15 | final MobileScannerController controller = MobileScannerController( |
| 170 | facing: CameraFacing.back, | 16 | facing: CameraFacing.back, |
| 171 | - autoStart: false | 17 | + formats: const [BarcodeFormat.qrCode], |
| 18 | + autoStart: false, | ||
| 172 | ); | 19 | ); |
| 173 | - @override | ||
| 174 | - initState() { | ||
| 175 | - super.initState(); | ||
| 176 | - } | ||
| 177 | 20 | ||
| 178 | @override | 21 | @override |
| 179 | dispose() { | 22 | dispose() { |
| 180 | - super.dispose(); | ||
| 181 | controller.dispose(); | 23 | controller.dispose(); |
| 24 | + super.dispose(); | ||
| 182 | } | 25 | } |
| 183 | 26 | ||
| 184 | - configureCameraSettings() { | 27 | + void startCamera() { |
| 185 | setState(() { | 28 | setState(() { |
| 186 | - camState = !camState; | 29 | + camStarted = !camStarted; |
| 187 | controller.start(); | 30 | controller.start(); |
| 188 | }); | 31 | }); |
| 189 | } | 32 | } |
| 190 | 33 | ||
| 191 | - onBarcodeDetect(BarcodeCapture barcodeCapture) { | ||
| 192 | - setState(() { | ||
| 193 | - currentBarcodeCapture = barcodeCapture; | ||
| 194 | - overlayText = barcodeCapture.barcodes.last.displayValue!; | ||
| 195 | - }); | 34 | + void onBarcodeDetect(BarcodeCapture barcodeCapture) { |
| 35 | + setState(() { | ||
| 36 | + currentBarcodeCapture = barcodeCapture; | ||
| 37 | + overlayText = barcodeCapture.barcodes.last.displayValue!; | ||
| 38 | + }); | ||
| 196 | } | 39 | } |
| 197 | 40 | ||
| 198 | - | ||
| 199 | - | ||
| 200 | @override | 41 | @override |
| 201 | Widget build(BuildContext context) { | 42 | Widget build(BuildContext context) { |
| 202 | final scanWindow = Rect.fromCenter( | 43 | final scanWindow = Rect.fromCenter( |
| @@ -204,8 +45,7 @@ class _MyAppState extends State<BarcodeScannerWithOverlay> { | @@ -204,8 +45,7 @@ class _MyAppState extends State<BarcodeScannerWithOverlay> { | ||
| 204 | width: 200, | 45 | width: 200, |
| 205 | height: 200, | 46 | height: 200, |
| 206 | ); | 47 | ); |
| 207 | - final height = MediaQuery.of(context).size.height; | ||
| 208 | - final width = MediaQuery.of(context).size.width; | 48 | + |
| 209 | return Scaffold( | 49 | return Scaffold( |
| 210 | appBar: AppBar( | 50 | appBar: AppBar( |
| 211 | title: const Text('Scanner with Overlay Example app'), | 51 | title: const Text('Scanner with Overlay Example app'), |
| @@ -216,67 +56,89 @@ class _MyAppState extends State<BarcodeScannerWithOverlay> { | @@ -216,67 +56,89 @@ class _MyAppState extends State<BarcodeScannerWithOverlay> { | ||
| 216 | mainAxisAlignment: MainAxisAlignment.center, | 56 | mainAxisAlignment: MainAxisAlignment.center, |
| 217 | children: <Widget>[ | 57 | children: <Widget>[ |
| 218 | Expanded( | 58 | Expanded( |
| 219 | - child: camState | ||
| 220 | - ? Stack( | 59 | + child: camStarted |
| 60 | + ? Stack( | ||
| 221 | fit: StackFit.expand, | 61 | fit: StackFit.expand, |
| 222 | children: [ | 62 | children: [ |
| 223 | Center( | 63 | Center( |
| 224 | - child: MobileScanner( | ||
| 225 | - fit: BoxFit.contain, | ||
| 226 | - onDetect: (BarcodeCapture barcodeCapture) => { | ||
| 227 | - onBarcodeDetect(barcodeCapture) | ||
| 228 | - | ||
| 229 | - }, | ||
| 230 | - overlay: | ||
| 231 | - Positioned( | ||
| 232 | - bottom: height * 0.2, | ||
| 233 | - child:Opacity(opacity: 0.7, child: | ||
| 234 | - Text(overlayText, style: const TextStyle(backgroundColor: Colors.black26, color: Colors.white, fontWeight: FontWeight.bold, fontSize: 24, overflow: TextOverflow.ellipsis,), | ||
| 235 | - maxLines: 1, | ||
| 236 | - ) | ||
| 237 | - ) , | ||
| 238 | - ), | ||
| 239 | - | ||
| 240 | - controller: controller, | ||
| 241 | - scanWindow: scanWindow, | ||
| 242 | - errorBuilder: (context, error, child) { | ||
| 243 | - return ScannerErrorWidget(error: error); | ||
| 244 | - }, | 64 | + child: MobileScanner( |
| 65 | + fit: BoxFit.contain, | ||
| 66 | + onDetect: onBarcodeDetect, | ||
| 67 | + overlay: Padding( | ||
| 68 | + padding: const EdgeInsets.all(16.0), | ||
| 69 | + child: Align( | ||
| 70 | + alignment: Alignment.bottomCenter, | ||
| 71 | + child: Opacity( | ||
| 72 | + opacity: 0.7, | ||
| 73 | + child: Text( | ||
| 74 | + overlayText, | ||
| 75 | + style: const TextStyle( | ||
| 76 | + backgroundColor: Colors.black26, | ||
| 77 | + color: Colors.white, | ||
| 78 | + fontWeight: FontWeight.bold, | ||
| 79 | + fontSize: 24, | ||
| 80 | + overflow: TextOverflow.ellipsis, | ||
| 81 | + ), | ||
| 82 | + maxLines: 1, | ||
| 83 | + ), | ||
| 84 | + ), | ||
| 85 | + ), | ||
| 86 | + ), | ||
| 87 | + controller: controller, | ||
| 88 | + scanWindow: scanWindow, | ||
| 89 | + errorBuilder: (context, error, child) { | ||
| 90 | + return ScannerErrorWidget(error: error); | ||
| 91 | + }, | ||
| 92 | + ), | ||
| 245 | ), | 93 | ), |
| 246 | - | ||
| 247 | - ), | ||
| 248 | CustomPaint( | 94 | CustomPaint( |
| 249 | - painter: ScannerOverlay(scanWindow), | 95 | + painter: ScannerOverlay(scanWindow), |
| 250 | ), | 96 | ), |
| 251 | - Positioned( | ||
| 252 | - bottom: 0.07 * height, | ||
| 253 | - left: 0.35 * width, | ||
| 254 | - child: Row( | ||
| 255 | - mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
| 256 | - children: [ | ||
| 257 | - IconButton(onPressed: ()=>{ | ||
| 258 | - controller.toggleTorch() | ||
| 259 | - }, icon: Icon(Icons.flashlight_on, color: controller.torchEnabled ? Colors.yellow : Colors.black,),), | ||
| 260 | - IconButton(onPressed: ()=>{ | ||
| 261 | - controller.switchCamera() | ||
| 262 | - }, icon: const Icon(Icons.cameraswitch_rounded, color: Colors.white,)), | ||
| 263 | - ], | 97 | + Padding( |
| 98 | + padding: const EdgeInsets.all(16.0), | ||
| 99 | + child: Align( | ||
| 100 | + alignment: Alignment.bottomCenter, | ||
| 101 | + child: Row( | ||
| 102 | + mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
| 103 | + children: [ | ||
| 104 | + IconButton( | ||
| 105 | + onPressed: () => controller.toggleTorch(), | ||
| 106 | + icon: Icon( | ||
| 107 | + Icons.flashlight_on, | ||
| 108 | + color: controller.torchEnabled | ||
| 109 | + ? Colors.yellow | ||
| 110 | + : Colors.black, | ||
| 111 | + ), | ||
| 112 | + ), | ||
| 113 | + IconButton( | ||
| 114 | + onPressed: () => controller.switchCamera(), | ||
| 115 | + icon: const Icon( | ||
| 116 | + Icons.cameraswitch_rounded, | ||
| 117 | + color: Colors.white, | ||
| 118 | + ), | ||
| 119 | + ), | ||
| 120 | + ], | ||
| 121 | + ), | ||
| 264 | ), | 122 | ), |
| 265 | - ) | 123 | + ), |
| 266 | ], | 124 | ], |
| 267 | ) | 125 | ) |
| 268 | - : const Center(child: Text("Tap on Camera to activate QR Scanner"))), | ||
| 269 | - | 126 | + : const Center( |
| 127 | + child: Text("Tap on Camera to activate QR Scanner")), | ||
| 128 | + ), | ||
| 270 | ], | 129 | ], |
| 271 | ), | 130 | ), |
| 272 | ), | 131 | ), |
| 273 | - floatingActionButton: camState ? null : FloatingActionButton( | ||
| 274 | - child: Icon( | ||
| 275 | - Icons.camera_alt | ||
| 276 | - ), | ||
| 277 | - onPressed: () { | ||
| 278 | - configureCameraSettings(); | ||
| 279 | - }), | 132 | + floatingActionButton: camStarted |
| 133 | + ? null | ||
| 134 | + : FloatingActionButton( | ||
| 135 | + child: const Icon( | ||
| 136 | + Icons.camera_alt, | ||
| 137 | + ), | ||
| 138 | + onPressed: () { | ||
| 139 | + startCamera(); | ||
| 140 | + }, | ||
| 141 | + ), | ||
| 280 | ); | 142 | ); |
| 281 | } | 143 | } |
| 282 | } | 144 | } |
| @@ -309,13 +171,13 @@ class ScannerOverlay extends CustomPainter { | @@ -309,13 +171,13 @@ class ScannerOverlay extends CustomPainter { | ||
| 309 | ..strokeWidth = 4.0; // Adjust the border width as needed | 171 | ..strokeWidth = 4.0; // Adjust the border width as needed |
| 310 | 172 | ||
| 311 | // Calculate the border rectangle with rounded corners | 173 | // Calculate the border rectangle with rounded corners |
| 312 | - final borderRadius = BorderRadius.circular(12.0); // Adjust the radius as needed | 174 | +// Adjust the radius as needed |
| 313 | final borderRect = RRect.fromRectAndCorners( | 175 | final borderRect = RRect.fromRectAndCorners( |
| 314 | scanWindow, | 176 | scanWindow, |
| 315 | - topLeft: Radius.circular(12.0), | ||
| 316 | - topRight: Radius.circular(12.0), | ||
| 317 | - bottomLeft: Radius.circular(12.0), | ||
| 318 | - bottomRight: Radius.circular(12.0), | 177 | + topLeft: const Radius.circular(12.0), |
| 178 | + topRight: const Radius.circular(12.0), | ||
| 179 | + bottomLeft: const Radius.circular(12.0), | ||
| 180 | + bottomRight: const Radius.circular(12.0), | ||
| 319 | ); | 181 | ); |
| 320 | 182 | ||
| 321 | // Draw the white border | 183 | // Draw the white border |
-
Please register or login to post a comment