import 'dart:io'; import 'dart:ui'; import 'dart:ui' as ui; import 'package:camera/camera.dart'; import 'package:flutter/material.dart'; import 'package:google_mlkit_barcode_scanning/google_mlkit_barcode_scanning.dart'; import 'coordinates_translator.dart'; class BarcodeDetectorPainter extends CustomPainter { BarcodeDetectorPainter( this.barcodes, this.imageSize, this.rotation, this.cameraLensDirection, ); final List barcodes; final Size imageSize; final InputImageRotation rotation; final CameraLensDirection cameraLensDirection; @override void paint(Canvas canvas, Size size) { final Paint paint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 3.0 ..color = Colors.lightGreenAccent; final Paint background = Paint()..color = Color(0x99000000); for (final Barcode barcode in barcodes) { final ParagraphBuilder builder = ParagraphBuilder( ParagraphStyle( textAlign: TextAlign.left, fontSize: 16, textDirection: TextDirection.ltr), ); builder.pushStyle( ui.TextStyle(color: Colors.lightGreenAccent, background: background)); builder.addText('${barcode.displayValue}'); builder.pop(); final left = translateX( barcode.boundingBox.left, size, imageSize, rotation, cameraLensDirection, ); final top = translateY( barcode.boundingBox.top, size, imageSize, rotation, cameraLensDirection, ); final right = translateX( barcode.boundingBox.right, size, imageSize, rotation, cameraLensDirection, ); // final bottom = translateY( // barcode.boundingBox.bottom, // size, // imageSize, // rotation, // cameraLensDirection, // ); // // // Draw a bounding rectangle around the barcode // canvas.drawRect( // Rect.fromLTRB(left, top, right, bottom), // paint, // ); final List cornerPoints = []; for (final point in barcode.cornerPoints) { final double x = translateX( point.x.toDouble(), size, imageSize, rotation, cameraLensDirection, ); final double y = translateY( point.y.toDouble(), size, imageSize, rotation, cameraLensDirection, ); cornerPoints.add(Offset(x, y)); } // Add the first point to close the polygon cornerPoints.add(cornerPoints.first); canvas.drawPoints(PointMode.polygon, cornerPoints, paint); canvas.drawParagraph( builder.build() ..layout(ParagraphConstraints( width: (right - left).abs(), )), Offset( Platform.isAndroid && cameraLensDirection == CameraLensDirection.front ? right : left, top), ); } } @override bool shouldRepaint(BarcodeDetectorPainter oldDelegate) { return oldDelegate.imageSize != imageSize || oldDelegate.barcodes != barcodes; } }