Specify allowed EAN extensions as a hint.

This commit is contained in:
Ralf Kistner 2014-01-31 17:21:02 +02:00
parent 9d4b8b9565
commit 1baa8f3311
2 changed files with 27 additions and 0 deletions

View file

@ -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.
;

View file

@ -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) {