Fix small display problem when extension starts with 9
[zxing.git] / core / src / com / google / zxing / oned / UPCAReader.java
index f4dfdc6..b90c1b8 100644 (file)
@@ -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.
 
 package com.google.zxing.oned;
 
-import com.google.zxing.ReaderException;
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.BinaryBitmap;
+import com.google.zxing.ChecksumException;
+import com.google.zxing.FormatException;
+import com.google.zxing.NotFoundException;
+import com.google.zxing.Result;
 import com.google.zxing.common.BitArray;
 
+import java.util.Hashtable;
+
 /**
  * <p>Implements decoding of the UPC-A format.</p>
  *
  * @author dswitkin@google.com (Daniel Switkin)
- * @author srowen@google.com (Sean Owen)
+ * @author Sean Owen
  */
-public final class UPCAReader extends AbstractUPCEANReader {
+public final class UPCAReader extends UPCEANReader {
+
+  private final UPCEANReader ean13Reader = new EAN13Reader();
+
+  public Result decodeRow(int rowNumber, BitArray row, int[] startGuardRange, Hashtable hints)
+      throws NotFoundException, FormatException, ChecksumException {
+    return maybeReturnResult(ean13Reader.decodeRow(rowNumber, row, startGuardRange, hints));
+  }
+
+  public Result decodeRow(int rowNumber, BitArray row, Hashtable hints)
+      throws NotFoundException, FormatException, ChecksumException {
+    return maybeReturnResult(ean13Reader.decodeRow(rowNumber, row, hints));
+  }
+
+  public Result decode(BinaryBitmap image) throws NotFoundException, FormatException {
+    return maybeReturnResult(ean13Reader.decode(image));
+  }
+
+  public Result decode(BinaryBitmap image, Hashtable hints) throws NotFoundException, FormatException {
+    return maybeReturnResult(ean13Reader.decode(image, hints));
+  }
+
+  BarcodeFormat getBarcodeFormat() {
+    return BarcodeFormat.UPC_A;
+  }
 
-  protected int decodeMiddle(BitArray row, int[] startRange, StringBuffer resultString) throws ReaderException {
-    int middleStart = decodeDigits(row, startRange[1], resultString);
-    int[] middleRange = findGuardPattern(row, middleStart, true, MIDDLE_PATTERN);
-    return decodeDigits(row, middleRange[1], resultString);
+  protected int decodeMiddle(BitArray row, int[] startRange, StringBuffer resultString)
+      throws NotFoundException {
+    return ean13Reader.decodeMiddle(row, startRange, resultString);
   }
 
-  /**
-   * @param row row of black/white values to decode
-   * @param start horizontal offset from which decoding starts
-   * @param result {@link StringBuffer} to append decoded digits to
-   * @return horizontal offset of first pixel after the six decoded digits
-   * @throws ReaderException if six digits could not be decoded from the row
-   */
-  private static int decodeDigits(BitArray row, int start, StringBuffer result) throws ReaderException {
-    int[] counters = new int[4];
-    int end = row.getSize();
-    int rowOffset = start;
-    for (int x = 0; x < 6 && rowOffset < end; x++) {
-      int bestMatch = decodeDigit(row, counters, rowOffset, L_PATTERNS);
-      result.append((char) ('0' + bestMatch));
-      for (int i = 0; i < counters.length; i++) {
-        rowOffset += counters[i];
-      }
+  private static Result maybeReturnResult(Result result) throws FormatException {
+    String text = result.getText();
+    if (text.charAt(0) == '0') {
+      return new Result(text.substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
+    } else {
+      throw FormatException.getFormatInstance();
     }
-    return rowOffset;
   }
 
-}
\ No newline at end of file
+}