I’m encountering an issue with barcode scanning using mobile_scanner While the scanner works perfectly with barcodes on paper, it struggles to read barcodes printed on boxes.
The problem occurs even in well-lit conditions, and I’ve tried adjusting the distance and angle, but the barcode on the box still isn’t being detected. Attached is an image of the box in question.
What could be causing this issue?
Here is the my code:
Widget oneTapedQuickScanBarCode(BuildContext context) {
barcode = null;
return LayoutBuilder(
builder: (context, constraints) {
final Size layoutSize = constraints.biggest;
final double scanWindowWidth = 250.w;
final double scanWindowHeight = 110.h;
final Rect scanWindow = Rect.fromCenter(
center: layoutSize.center(Offset.zero),
width: scanWindowWidth,
height: scanWindowHeight,
);
return MobileScanner(
scanWindow: scanWindow,
overlayBuilder: (context, constraints) {
return Stack(
children: [
Positioned.fill(
child: Container(
decoration: ShapeDecoration(
shape: QrScannerOverlayShape(
borderColor: Colors.white,
borderRadius: 10,
borderLength: 20,
borderWidth: 5,
cutOutWidth: scanWindowWidth,
cutOutHeight: scanWindowHeight,
),
),
),
),
],
);
},
onDetect: (capture) async {
if (capture.image != null) {
img.Image? fullImage = img.decodeImage(capture.image!);
if (fullImage != null) {
final int cropX = (fullImage.width - scanWindowWidth) ~/ 2;
final int cropY = (fullImage.height - scanWindowHeight) ~/ 2;
final int cropWidth = scanWindowWidth.toInt();
final int cropHeight = scanWindowHeight.toInt();
img.Image croppedImage =
img.copyCrop(fullImage, x: cropX, y: cropY, width: cropWidth, height: cropHeight);
final Uint8List croppedImageBytes = Uint8List.fromList(img.encodeJpg(croppedImage));
image = croppedImageBytes;
barcode = capture.barcodes.first.rawValue;
debugPrint('Barcode value: $barcode');
await _scannerController.stop();
_scannerController.dispose();
if (widget.passDataBack) {
Get.back(result: capture);
return;
}
setState(() {});
var bottomNavbarController = Get.find<BottomNavController>();
await bottomNavbarController.validateIMEI(barcode!, image);
}
}
},
controller: _scannerController,
);
},
);
}
void initController() {
_scannerController = MobileScannerController(
detectionSpeed: DetectionSpeed.normal,
returnImage: true,
useNewCameraSelector: true,
torchEnabled: _isFlashOn,
/* formats: [
// Add the barcode formats you want to scan here, excluding BarcodeFormat.qrCode
BarcodeFormat.aztec,
BarcodeFormat.codabar,
BarcodeFormat.code128,
BarcodeFormat.code39,
BarcodeFormat.code93,
BarcodeFormat.dataMatrix,
BarcodeFormat.ean13,
BarcodeFormat.ean8,
BarcodeFormat.itf,
BarcodeFormat.upcA,
BarcodeFormat.upcE,
],*/
);
}