From 085dad4c2d60fc80edb16ab095846e82445e8215 Mon Sep 17 00:00:00 2001 From: Jaieden <5084731+Jaieden@users.noreply.github.com> Date: Tue, 3 Nov 2026 14:45:26 +1000 Subject: [PATCH] Interpret ECI character-set marker in scanned 2D barcodes (#907) --- lib/barcode/camera_controller.dart | 26 ++++++++++- lib/barcode/eci.dart | 46 +++++++++++++++++++ test/barcode_eci_test.dart | 71 ++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 lib/barcode/eci.dart create mode 100644 test/barcode_eci_test.dart diff --git a/lib/barcode/camera_controller.dart b/lib/barcode/camera_controller.dart index 94a00720..aee0e910 100644 --- a/lib/barcode/camera_controller.dart +++ b/lib/barcode/camera_controller.dart @@ -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( diff --git a/lib/barcode/eci.dart b/lib/barcode/eci.dart new file mode 100644 index 00000000..add91e27 --- /dev/null +++ b/lib/barcode/eci.dart @@ -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 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; + } +} diff --git a/test/barcode_eci_test.dart b/test/barcode_eci_test.dart new file mode 100644 index 00000000..ed62fd67 --- /dev/null +++ b/test/barcode_eci_test.dart @@ -0,0 +1,71 @@ +/* + * Unit tests for interpreting ECI markers in scanned barcode data + */ + +import "dart:convert"; +import "dart:typed_data"; + +import "package:flutter_test/flutter_test.dart"; + +import "package:inventree/barcode/eci.dart"; + +/// Bytes as the scanner reports them: ECI number, then the encoded data. +Uint8List withMarker(int eci, List data) => + Uint8List.fromList([eci, ...data]); + +void main() { + group("ECI marker in scanned data", () { + test("UTF-8 marker (ECI 26) is removed", () { + final bytes = withMarker(26, ascii.encode("INV-SI1")); + final reported = String.fromCharCodes(bytes); + + expect(decodeEciPrefixed(reported, bytes), "INV-SI1"); + }); + + test("UTF-8 data is decoded with the declared character set", () { + final bytes = withMarker(26, utf8.encode("Crème brûlée")); + final reported = String.fromCharCodes(bytes); + + expect(decodeEciPrefixed(reported, bytes), "Crème brûlée"); + }); + + test("ISO-8859-1 marker (ECI 3) is decoded as Latin-1", () { + final bytes = withMarker(3, latin1.encode("Crème")); + final reported = String.fromCharCodes(bytes); + + expect(decodeEciPrefixed(reported, bytes), "Crème"); + }); + + test("Data without a marker is unchanged", () { + final bytes = Uint8List.fromList(ascii.encode("INV-SI1")); + + expect(decodeEciPrefixed("INV-SI1", bytes), "INV-SI1"); + }); + + test("Unknown leading control byte is left alone", () { + // 0x1D (GS) is used by GS1 codes and is not an ECI marker here + final bytes = withMarker(0x1D, ascii.encode("01234")); + final reported = String.fromCharCodes(bytes); + + expect(decodeEciPrefixed(reported, bytes), reported); + }); + + test("Marker in bytes but not in the string is left alone", () { + final bytes = withMarker(26, ascii.encode("INV-SI1")); + + expect(decodeEciPrefixed("INV-SI1", bytes), "INV-SI1"); + }); + + test("Invalid UTF-8 falls back to the reported string", () { + final bytes = withMarker(26, [0xFF, 0xFE]); + final reported = String.fromCharCodes(bytes); + + expect(decodeEciPrefixed(reported, bytes), reported); + }); + + test("Missing bytes fall back to the reported string", () { + expect(decodeEciPrefixed("INV-SI1", null), "INV-SI1"); + expect(decodeEciPrefixed(null, null), ""); + }); + }); +}