X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=core%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Foned%2FMultiFormatUPCEANReader.java;h=502b086f14ae9ccb91561c8667b68b22bb33fedb;hb=7ca7b8f6a94aae2c7e65d7179c2576c35d153bc2;hp=835504be8ce45d068ad0e2e0163746695cece293;hpb=2c46678a95114dac1c8d32fdfea694dc4ca02236;p=zxing.git diff --git a/core/src/com/google/zxing/oned/MultiFormatUPCEANReader.java b/core/src/com/google/zxing/oned/MultiFormatUPCEANReader.java index 835504be..502b086f 100644 --- a/core/src/com/google/zxing/oned/MultiFormatUPCEANReader.java +++ b/core/src/com/google/zxing/oned/MultiFormatUPCEANReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2008 Google Inc. + * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,21 +27,23 @@ import java.util.Vector; /** *

A reader that can read all available UPC/EAN formats. If a caller wants to try to - * read all such formats, it is most efficent to use this implementation rather than invoke + * read all such formats, it is most efficient to use this implementation rather than invoke * individual readers.

* - * @author srowen@google.com (Sean Owen) + * @author Sean Owen */ public final class MultiFormatUPCEANReader extends AbstractOneDReader { - public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException { - Vector possibleFormats = hints == null ? null : (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS); - Vector readers = new Vector(); + private final Vector readers; + + public MultiFormatUPCEANReader(Hashtable hints) { + Vector possibleFormats = hints == null ? null : + (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS); + readers = new Vector(); if (possibleFormats != null) { if (possibleFormats.contains(BarcodeFormat.EAN_13)) { readers.addElement(new EAN13Reader()); - } - if (possibleFormats.contains(BarcodeFormat.UPC_A)) { + } else if (possibleFormats.contains(BarcodeFormat.UPC_A)) { readers.addElement(new UPCAReader()); } if (possibleFormats.contains(BarcodeFormat.EAN_8)) { @@ -52,41 +54,44 @@ public final class MultiFormatUPCEANReader extends AbstractOneDReader { } } if (readers.isEmpty()) { - readers.addElement(new EAN13Reader()); - readers.addElement(new UPCAReader()); + readers.addElement(new EAN13Reader()); + // UPC-A is covered by EAN-13 readers.addElement(new EAN8Reader()); readers.addElement(new UPCEReader()); } + } + public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException { // Compute this location once and reuse it on multiple implementations int[] startGuardPattern = AbstractUPCEANReader.findStartGuardPattern(row); - for (int i = 0; i < readers.size(); i++) { + int size = readers.size(); + for (int i = 0; i < size; i++) { UPCEANReader reader = (UPCEANReader) readers.elementAt(i); Result result; try { - result = reader.decodeRow(rowNumber, row, startGuardPattern); + result = reader.decodeRow(rowNumber, row, startGuardPattern, hints); } catch (ReaderException re) { continue; } // Special case: a 12-digit code encoded in UPC-A is identical to a "0" // followed by those 12 digits encoded as EAN-13. Each will recognize such a code, // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0". - // Individually these are correct. - // - // These cases can't be distinguished, so we defer to the UPC-A decoder and - // treat this case as a UPC-A code. But if we let the UPC-A decoder look at - // symbols first, it will recognize any EAN-13 code as a 12-digit string, - // which is wrong. So EAN-13 has to try first. + // Individually these are correct and their readers will both read such a code + // and correctly call it EAN-13, or UPC-A, respectively. // - // Here is, therefore, where we implement this logic: + // In this case, if we've been looking for both types, we'd like to call it + // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read + // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A + // result if appropriate. if (result.getBarcodeFormat().equals(BarcodeFormat.EAN_13) && result.getText().charAt(0) == '0') { - return new Result(result.getText().substring(1), result.getResultPoints(), BarcodeFormat.UPC_A); + return new Result(result.getText().substring(1), null, result.getResultPoints(), + BarcodeFormat.UPC_A); } return result; } - throw new ReaderException("No barcode was detected in this image."); + throw ReaderException.getInstance(); } -} \ No newline at end of file +}