Interpret ECI character-set marker in scanned 2D barcodes (#907)

This commit is contained in:
Jaieden authored and GitHub committed 2026-11-03 15:45:26 +11:00
1 parent 197de6aa16
commit 085dad4c2d
3 files changed
+142 -1

No files matched your search

+25 -1
View File
@@ -16,6 +16,7 @@ import "package:inventree/l10.dart";
import "package:inventree/barcode/handler.dart";
import "package:inventree/barcode/controller.dart";
import "package:inventree/barcode/eci.dart";
/*
* Barcode controller which uses the device's camera to scan barcodes.
@@ -128,7 +129,7 @@ class _CameraBarcodeControllerState extends InvenTreeBarcodeControllerState {
});
}
String barcode = result.barcodes.first.rawValue ?? "";
String barcode = scannedValue(result.barcodes.first);
if (barcode.isEmpty) {
// TODO: Error message "empty barcode"
@@ -157,6 +158,29 @@ class _CameraBarcodeControllerState extends InvenTreeBarcodeControllerState {
}
}
/*
* Text value of a scanned barcode.
* For 2D codes, an ECI character-set marker that the scanner left in the
* data is interpreted rather than passed on (see barcode/eci.dart).
*/
String scannedValue(Barcode code) {
final decoded = switch (code.rawDecodedBytes) {
DecodedBarcodeBytes(:final bytes) => bytes,
DecodedVisionBarcodeBytes(:final bytes) => bytes,
null => null,
};
switch (code.format) {
case BarcodeFormat.dataMatrix:
case BarcodeFormat.qrCode:
case BarcodeFormat.aztec:
case BarcodeFormat.pdf417:
return decodeEciPrefixed(code.rawValue, decoded);
default:
return code.rawValue ?? "";
}
}
void onControllerCreated(CameraController? controller, Exception? error) {
if (error != null) {
sentryReportError(
+46
View File
@@ -0,0 +1,46 @@
import "dart:convert";
import "dart:typed_data";
/*
* Interpret an ECI character-set marker reported inside scanned barcode data.
*
* A 2D barcode (DataMatrix, QR, Aztec, PDF417) can declare its character set
* with an ECI designator. Some scanner backends do not apply the declaration;
* instead they report the ECI number as a single leading byte of the data.
* ML Kit on Android (used through mobile_scanner) does this: a DataMatrix with
* ECI 26 (UTF-8) holding "JG-SI1" is reported as 0x1A followed by "JG-SI1",
* in rawValue, displayValue and the decoded bytes alike.
*
* When the decoded bytes begin with a known ECI number, and the reported
* string begins with the same character (showing the marker leaked into the
* value), the rest of the bytes are decoded with the declared character set.
* In every other case the reported string is returned unchanged.
*/
/// ECI assignments understood here: ECI number -> character set.
const Map<int, Encoding> eciCharsets = {
3: latin1, // ISO-8859-1
26: utf8, // UTF-8
27: ascii, // US-ASCII
};
String decodeEciPrefixed(String? value, Uint8List? bytes) {
final String reported = value ?? "";
if (bytes == null || bytes.length < 2 || reported.isEmpty) {
return reported;
}
final int marker = bytes.first;
final Encoding? charset = eciCharsets[marker];
if (charset == null || reported.codeUnitAt(0) != marker) {
return reported;
}
try {
return charset.decode(bytes.sublist(1));
} on FormatException {
return reported;
}
}