Skip to content

Commit

Permalink
fix: Camera ignore turn flash off exception (openfoodfacts#2228)
Browse files Browse the repository at this point in the history
* Only accepts barcodes on half-top of the image

* Fix for "Unhandled Exception: CameraException(setFlashModeFailed, Could not set flash mode.)"

* Also try/catch from pausePreview
  • Loading branch information
g123k authored Jun 9, 2022
1 parent e335b9a commit 004f1aa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/smooth_app/lib/pages/scan/camera_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,18 @@ class SmoothCameraController extends CameraController {
}

if (_isInitialized) {
await _pauseFlash();
await super.pausePreview();
try {
await _pauseFlash();
} catch (exception) {
// Camera already disposed
}

try {
await super.pausePreview();
} catch (exception) {
// Camera already disposed
}

_isPaused = true;
}
}
Expand Down
5 changes: 5 additions & 0 deletions packages/smooth_app/lib/pages/scan/mkit_scan_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,17 @@ class _MLKitScanDecoderIsolate {

final CameraImage image = CameraImage.fromPlatformData(message);
final InputImage cropImage = _cropImage(image);
final double imageHeight =
cropImage.inputImageData?.size.longestSide ?? double.infinity;

final List<Barcode> barcodes =
await _barcodeScanner.processImage(cropImage);

port.send(
barcodes
// Only accepts barcodes on half-top of the image
.where((Barcode barcode) =>
(barcode.boundingBox?.top ?? 0.0) <= imageHeight * 0.5)
.map((Barcode barcode) => _changeBarcodeType(barcode))
.where((String? barcode) => barcode?.isNotEmpty == true)
.cast<String>()
Expand Down

0 comments on commit 004f1aa

Please sign in to comment.