-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
preview_overlay_example.dart
111 lines (102 loc) · 3.12 KB
/
preview_overlay_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import 'dart:async';
import 'package:camera_app/utils/file_utils.dart';
import 'package:camera_app/utils/mlkit_utils.dart';
import 'package:camera_app/widgets/barcode_preview_overlay.dart';
import 'package:camerawesome/camerawesome_plugin.dart';
import 'package:flutter/material.dart';
import 'package:google_mlkit_barcode_scanning/google_mlkit_barcode_scanning.dart';
void main() {
runApp(const CameraAwesomeApp());
}
class CameraAwesomeApp extends StatelessWidget {
const CameraAwesomeApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Preview Overlay',
home: CameraPage(),
);
}
}
class CameraPage extends StatefulWidget {
const CameraPage({super.key});
@override
State<CameraPage> createState() => _CameraPageState();
}
class _CameraPageState extends State<CameraPage> {
final _barcodeScanner = BarcodeScanner(formats: [BarcodeFormat.all]);
List<Barcode> _barcodes = [];
AnalysisImage? _image;
@override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
body: CameraAwesomeBuilder.awesome(
saveConfig: SaveConfig.photoAndVideo(
initialCaptureMode: CaptureMode.photo,
),
sensorConfig: SensorConfig.single(
flashMode: FlashMode.auto,
aspectRatio: CameraAspectRatios.ratio_16_9,
),
previewFit: CameraPreviewFit.fitWidth,
onMediaTap: (mediaCapture) {
mediaCapture.captureRequest
.when(single: (single) => single.file?.open());
},
previewDecoratorBuilder: (state, preview) {
return BarcodePreviewOverlay(
state: state,
barcodes: _barcodes,
analysisImage: _image,
preview: preview,
);
},
topActionsBuilder: (state) {
return AwesomeTopActions(
state: state,
children: [
AwesomeFlashButton(state: state),
if (state is PhotoCameraState)
AwesomeAspectRatioButton(state: state),
],
);
},
middleContentBuilder: (state) {
return const SizedBox.shrink();
},
bottomActionsBuilder: (state) {
return const Padding(
padding: EdgeInsets.only(bottom: 20),
child: Text(
"Scan your barcodes",
style: TextStyle(
color: Colors.white,
fontSize: 30,
),
),
);
},
onImageForAnalysis: (img) => _processImageBarcode(img),
imageAnalysisConfig: AnalysisConfig(
androidOptions: const AndroidAnalysisOptions.nv21(
width: 256,
),
maxFramesPerSecond: 3,
),
),
);
}
Future _processImageBarcode(AnalysisImage img) async {
try {
var recognizedBarCodes =
await _barcodeScanner.processImage(img.toInputImage());
setState(() {
_barcodes = recognizedBarCodes;
_image = img;
});
} catch (error) {
debugPrint("...sending image resulted error $error");
}
}
}