Created
June 2, 2021 20:46
-
-
Save jacobaraujo7/07144af34f212ce5228f4476bc53f34b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:camera/camera.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_modular/flutter_modular.dart'; | |
import 'package:google_ml_kit/google_ml_kit.dart'; | |
import 'qrcode_controller.dart'; | |
class QrcodePage extends StatefulWidget { | |
final String title; | |
const QrcodePage({Key key, this.title = "Qrcode"}) : super(key: key); | |
@override | |
_QrcodePageState createState() => _QrcodePageState(); | |
} | |
class _QrcodePageState extends ModularState<QrcodePage, QrcodeController> { | |
//use 'controller' variable to access controller | |
List<CameraDescription> cameras; | |
CameraController controllerCamera; | |
final barcodeDetector = GoogleMlKit.vision.barcodeScanner(); | |
int camSelect = 0; | |
bool isReader = false; | |
@override | |
void initState() { | |
super.initState(); | |
availableCameras().then((cams) { | |
cameras = cams; | |
_initCam(0); | |
}); | |
} | |
InputImagePlaneMetadata _convertPlaneToInputImagePlaneMetadata(Plane e) { | |
return InputImagePlaneMetadata(bytesPerRow: e.bytesPerRow, height: e.height, width: e.width); | |
} | |
_takePhotoAndRecognizer(CameraImage image) async { | |
try { | |
if (!(controllerCamera?.value?.isInitialized == true) && isReader) return; | |
final size = Size(image.width.toDouble(), image.height.toDouble()); | |
final data = InputImageData( | |
planeData: image.planes.map(_convertPlaneToInputImagePlaneMetadata).toList(), | |
size: size, | |
imageRotation: InputImageRotation.Rotation_0deg, | |
); | |
final inputImage = InputImage.fromBytes(bytes: image.planes[0].bytes, inputImageData: data); | |
final barcodes = await barcodeDetector.processImage(inputImage); | |
if (barcodes.isNotEmpty) { | |
isReader = true; | |
await controllerCamera.stopImageStream(); | |
Navigator.of(context).pop(barcodes[0].info.rawValue); | |
} | |
} on Exception catch (e) { | |
print(e); | |
} | |
} | |
_initCam(int cam) { | |
camSelect = cam; | |
controllerCamera = CameraController(cameras[cam], ResolutionPreset.high); | |
controllerCamera.initialize().then((_) { | |
if (!mounted) { | |
return; | |
} | |
setState(() {}); | |
controllerCamera.startImageStream(_takePhotoAndRecognizer); | |
}); | |
} | |
@override | |
void dispose() { | |
controllerCamera?.dispose(); | |
super.dispose(); | |
} | |
_changeCamera() async { | |
if (cameras.length > 1) { | |
await controllerCamera?.dispose(); | |
_initCam(camSelect == 0 ? 1 : 0); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Colors.black, | |
body: Builder(builder: (_) { | |
final cameraIsReady = controllerCamera?.value?.isInitialized == true; | |
return Stack( | |
children: [ | |
if (!cameraIsReady) Container(color: Colors.grey[700]), | |
if (cameraIsReady) | |
Container( | |
height: MediaQuery.of(context).size.height, | |
width: MediaQuery.of(context).size.width, | |
child: FittedBox( | |
fit: BoxFit.cover, | |
alignment: Alignment.center, | |
child: Container( | |
alignment: Alignment.center, | |
width: controllerCamera.value.previewSize.width, | |
height: controllerCamera.value.previewSize.height, | |
child: CameraPreview(controllerCamera), | |
), | |
), | |
), | |
Align( | |
alignment: Alignment.center, | |
child: _Mask(), | |
), | |
Align( | |
alignment: Alignment.bottomCenter, | |
child: Container( | |
color: Colors.black, | |
width: double.infinity, | |
height: MediaQuery.of(context).size.height * 0.1, | |
alignment: Alignment.center, | |
child: IconButton( | |
color: Colors.white, | |
iconSize: 30, | |
icon: Icon(camSelect == 0 ? Icons.camera_rear : Icons.camera_front), | |
onPressed: _changeCamera, | |
), | |
), | |
), | |
Align( | |
alignment: Alignment.topLeft, | |
child: SafeArea( | |
child: Padding( | |
padding: EdgeInsets.only(top: 10, left: 10), | |
child: BackButton( | |
color: Colors.white, | |
), | |
), | |
), | |
), | |
], | |
); | |
}), | |
); | |
} | |
} | |
class _Mask extends StatelessWidget { | |
final Color colorMask = Colors.black45; | |
@override | |
Widget build(BuildContext context) { | |
final size = MediaQuery.of(context).size.width * 0.85; | |
return Column( | |
children: [ | |
Expanded(child: Container(color: colorMask)), | |
SizedBox( | |
width: MediaQuery.of(context).size.width, | |
height: size, | |
child: Row( | |
children: [ | |
Expanded(child: Container(color: colorMask)), | |
HardLine(size: size), | |
Expanded(child: Container(color: colorMask)), | |
], | |
), | |
), | |
Expanded(child: Container(color: colorMask)), | |
SizedBox(height: MediaQuery.of(context).size.height * 0.1), | |
], | |
); | |
} | |
} | |
class HardLine extends StatefulWidget { | |
final double size; | |
const HardLine({Key key, @required this.size}) : super(key: key); | |
@override | |
_HardLineState createState() => _HardLineState(); | |
} | |
class _HardLineState extends State<HardLine> with SingleTickerProviderStateMixin { | |
AnimationController animationController; | |
@override | |
void initState() { | |
super.initState(); | |
animationController = AnimationController(vsync: this, duration: Duration(seconds: 2)); | |
animationController.addListener(() { | |
setState(() {}); | |
}); | |
animationController.repeat(reverse: true); | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
animationController.stop(); | |
animationController.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
height: widget.size, | |
width: widget.size, | |
child: Stack( | |
children: [ | |
Transform.translate( | |
offset: Offset(0.0, widget.size * animationController.value), | |
child: Container( | |
width: widget.size, | |
height: 3, | |
color: Colors.red, | |
), | |
) | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment