mirror of
https://github.com/inventree/inventree-app.git
synced 2026-11-19 20:28:38 +00:00
Interpret ECI character-set marker in scanned 2D barcodes (#907)
This commit is contained in:
1 parent
197de6aa16
commit
085dad4c2d
3 files changed
+142
-1
No files matched your search
@@ -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(
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<int> 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), "");
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user