From 1baa8f33116531b383868f17e0c5f348f23517ce Mon Sep 17 00:00:00 2001 From: Ralf Kistner Date: Fri, 31 Jan 2014 17:21:02 +0200 Subject: [PATCH] Specify allowed EAN extensions as a hint. --- .../java/com/google/zxing/DecodeHintType.java | 9 +++++++++ .../com/google/zxing/oned/UPCEANReader.java | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/core/src/main/java/com/google/zxing/DecodeHintType.java b/core/src/main/java/com/google/zxing/DecodeHintType.java index 54a1c8086..90d0a3e02 100644 --- a/core/src/main/java/com/google/zxing/DecodeHintType.java +++ b/core/src/main/java/com/google/zxing/DecodeHintType.java @@ -88,6 +88,15 @@ public enum DecodeHintType { */ NEED_RESULT_POINT_CALLBACK(ResultPointCallback.class), + + /** + * Allowed extension lengths for EAN or UPC barcodes. + * Other formats will ignore this. + * Maps to an {@code int[]} of the allowed extension lengths, for example [2], [5], or [2, 5]. + * If it is optional to have an extension, do not set this hint. + */ + ALLOWED_EAN_EXTENSIONS(int[].class); + // End of enumeration values. ; diff --git a/core/src/main/java/com/google/zxing/oned/UPCEANReader.java b/core/src/main/java/com/google/zxing/oned/UPCEANReader.java index 91afbe36d..349d4632d 100644 --- a/core/src/main/java/com/google/zxing/oned/UPCEANReader.java +++ b/core/src/main/java/com/google/zxing/oned/UPCEANReader.java @@ -194,15 +194,33 @@ public abstract class UPCEANReader extends OneDReader { new ResultPoint(right, (float) rowNumber)}, format); + int extensionLength = 0; + try { Result extensionResult = extensionReader.decodeRow(rowNumber, row, endRange[1]); decodeResult.putMetadata(ResultMetadataType.UPC_EAN_EXTENSION, extensionResult.getText()); decodeResult.putAllMetadata(extensionResult.getResultMetadata()); decodeResult.addResultPoints(extensionResult.getResultPoints()); + extensionLength = extensionResult.getText().length(); } catch (ReaderException re) { // continue } + int[] allowedExtensions = hints == null ? null : + (int[])hints.get(DecodeHintType.ALLOWED_EAN_EXTENSIONS); + if(allowedExtensions != null) { + boolean valid = false; + for (int length : allowedExtensions) { + if(extensionLength == length) { + valid = true; + break; + } + } + if(!valid) { + throw NotFoundException.getNotFoundInstance(); + } + } + if (format == BarcodeFormat.EAN_13 || format == BarcodeFormat.UPC_A) { String countryID = eanManSupport.lookupCountryIdentifier(resultString); if (countryID != null) {