twonly-app/lib/src/views/camera/painters/barcode_detector_painter.dart
otsmr 9667ea21b6
Some checks are pending
Flutter analyze & test / flutter_analyze_and_test (push) Waiting to run
starting with #327
2025-12-07 03:23:54 +01:00

124 lines
3.2 KiB
Dart

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<Barcode> 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<Offset> cornerPoints = <Offset>[];
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;
}
}