(Немного о DevFest)

(Камера и Flutter)

<uses-feature

   android:name="android.hardware.camera"

   android:required="true" />

<key>NSCameraUsageDescription</key>

<string>Need access to scan barcodes</string>

#camera

camera: ^0.10.5+4

google_mlkit_barcode_scanning: ^0.9.0

// Note: request() will check permission without request popup if already granted

 isPermissionGranted = await Permission.camera.request().isGranted;



 if (isPermissionGranted == true) {

   Future.delayed(const Duration(milliseconds: 200), () async {

     cameraController = await CameraUtils.getCameraController(

         ResolutionPreset.high, CameraLensDirection.back);

     await cameraController?.initialize(); // will throw CameraException if already disposed

     if (cameraController?.value.isInitialized == true) {

       if (mounted) {

         await cameraController?.lockCaptureOrientation(DeviceOrientation.portraitUp);

       }

       await _startImageStream();

       _stopped = false;

       if (mounted) {

         setState(() {});

       }

     }

   });

 } else {

   if (mounted) {

     setState(() {});

   }

   _stoppedByLifeCycle = true;

 }

});

Future<void> _startImageStream() async {

 await guarded(() async {

   if (cameraController?.value.isStreamingImages == false) {

     await cameraController?.startImageStream((image) {

  if (!_isDetecting && DateTime.now().difference(_lastScan).inMilliseconds >= 200) {

         _toggleDetectionLock();

         handleCameraImage(image);

       }

     });

   }

 });

}

final barcodes = await ScannerUtils().detect(

 image: image,

 detectInImage: _barcodeDetector.processImage,

 imageRotation: controller.description.sensorOrientation,

);

// Удалим из результатов штрихкоды за прицелом сканера (с периферии экрана)

if (barcodes.isNotEmpty) {

 if (controller.description.sensorOrientation == 90 ||

     controller.description.sensorOrientation == 270) {

   _removePeripherialObjects(barcodes, image.height.toDouble(), image.width.toDouble());

 } else {

   _removePeripherialObjects(barcodes, image.width.toDouble(), image.height.toDouble());

 }

Future<List<Barcode>> detect({

 required CameraImage image,

 required Future<List<Barcode>> Function(InputImage image) detectInImage,

 required int imageRotation,

}) async {

 final results = <Barcode>[];

 final bytes = _concatenatePlanes(image.planes);



 results.addAll(await detectInImage(

   InputImage.fromBytes(

     bytes: bytes,

     metadata: buildMetaData(image, rotationIntToImageRotation(imageRotation)),

   ),

 ));



 if (results.isNotEmpty) {

   return results;

 }



 final img_lib.Image? img = convertCameraImageToImageColor(image, true, pngFormat: false);

 final List<Barcode> resultsWhite = await detectInImage(

   InputImage.fromBytes(

     bytes: img?.getBytes() ?? Uint8List.fromList([]),

     metadata: buildMetaData(image, rotationIntToImageRotation(imageRotation)),

   ),

 );

 results.addAll(resultsWhite);



 return results;

}

void _removePeripherialObjects(List<Barcode> barcodes, double width, double height) {

 final currentWidth = width * 0.76;

 final squareRect = Rect.fromCenter(

     center: Offset(width / 2, height / 2), width: currentWidth, height: currentWidth);

 for (int i = 0; i < barcodes.length; i++) {

   final barcode = barcodes[i];

   final boundingBox = barcode.boundingBox;

   final intersect = squareRect.intersect(boundingBox);

   if (boundingBox == null ||

       intersect.width < boundingBox.width * 0.5 ||

       intersect.height < boundingBox.height * 0.5) {

     barcodes.removeAt(i);

     i--;

   }

 }

}

Хотите связаться с владельцем
компании напрямую?
Дмитрий Тарасов
Дмитрий Тарасов
СЕО

НАПИСАТЬ