Skip to content

Commit

Permalink
scan: Not scanning every picture (#1065)
Browse files Browse the repository at this point in the history
* scan: Not scanning every picture

* Update ml_kit_scan_page.dart

* Update ml_kit_scan_page.dart
  • Loading branch information
M123-dev authored Feb 3, 2022
1 parent d066c22 commit 7d8e9c2
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions packages/smooth_app/lib/pages/scan/ml_kit_scan_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MLKitScannerPage extends StatefulWidget {
}

class MLKitScannerPageState extends State<MLKitScannerPage> {
static const int _SKIPPED_FRAMES = 10;
BarcodeScanner? barcodeScanner = GoogleMlKit.vision.barcodeScanner();
CameraLensDirection cameraLensDirection = CameraLensDirection.back;
late ContinuousScanModel _model;
Expand All @@ -26,6 +27,8 @@ class MLKitScannerPageState extends State<MLKitScannerPage> {
bool isBusy = false;
//Used when rebuilding to stop the camera
bool stoppingCamera = false;
//We don't scan every image for performance reasons
int frameCounter = 0;

@override
void initState() {
Expand Down Expand Up @@ -80,7 +83,9 @@ class MLKitScannerPageState extends State<MLKitScannerPage> {
// loading or stopped
if (_controller == null ||
_controller!.value.isInitialized == false ||
stoppingCamera) {
stoppingCamera ||
_controller!.value.isPreviewPaused ||
!_controller!.value.isStreamingImages) {
return Container();
}

Expand Down Expand Up @@ -149,21 +154,28 @@ class MLKitScannerPageState extends State<MLKitScannerPage> {

Future<void> _stopImageStream() async {
stoppingCamera = true;
_controller?.pausePreview();
if (mounted) {
setState(() {});
}
setState(() {});
await _controller?.dispose();
_controller = null;
}

// Convert the [CameraImage] to a [InputImage] and checking this for barcodes
// with help from ML Kit
Future<void> _processCameraImage(CameraImage image) async {
//Only scanning every xth image, but not resetting until the current one
//is done, so that we don't have idle time when the scanning takes longer
// TODO(M123): Can probably be merged with isBusy + checking if we should
// Count when ML Kit is busy
if (frameCounter < _SKIPPED_FRAMES) {
frameCounter++;
return;
}

if (isBusy || barcodeScanner == null) {
return;
}
isBusy = true;
frameCounter = 0;

final WriteBuffer allBytes = WriteBuffer();
for (final Plane plane in image.planes) {
Expand Down

0 comments on commit 7d8e9c2

Please sign in to comment.