Add basic Code 128, Code 39, ITF writers, per Erik
[zxing.git] / core / src / com / google / zxing / oned / ITFReader.java
index b3613ab..b380972 100644 (file)
 package com.google.zxing.oned;\r
 \r
 import com.google.zxing.BarcodeFormat;\r
-import com.google.zxing.ReaderException;\r
+import com.google.zxing.DecodeHintType;\r
+import com.google.zxing.FormatException;\r
+import com.google.zxing.NotFoundException;\r
 import com.google.zxing.Result;\r
 import com.google.zxing.ResultPoint;\r
-import com.google.zxing.DecodeHintType;\r
 import com.google.zxing.common.BitArray;\r
 \r
 import java.util.Hashtable;\r
@@ -37,7 +38,7 @@ import java.util.Hashtable;
  *\r
  * @author kevin.osullivan@sita.aero, SITA Lab.\r
  */\r
-public final class ITFReader extends AbstractOneDReader {\r
+public final class ITFReader extends OneDReader {\r
 \r
   private static final int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.42f);\r
   private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.8f);\r
@@ -45,7 +46,7 @@ public final class ITFReader extends AbstractOneDReader {
   private static final int W = 3; // Pixel width of a wide line\r
   private static final int N = 1; // Pixed width of a narrow line\r
 \r
-  private static final int[] DEFAULT_ALLOWED_LENGTHS = { 6, 10, 14 };\r
+  private static final int[] DEFAULT_ALLOWED_LENGTHS = { 6, 10, 14, 44 };\r
 \r
   // Stores the actual narrow line width of the image being decoded.\r
   private int narrowLineWidth = -1;\r
@@ -62,7 +63,7 @@ public final class ITFReader extends AbstractOneDReader {
   /**\r
    * Patterns of Wide / Narrow lines to indicate each digit\r
    */\r
-  private static final int[][] PATTERNS = {\r
+  static final int[][] PATTERNS = {\r
       {N, N, W, W, N}, // 0\r
       {W, N, N, N, W}, // 1\r
       {N, W, N, N, W}, // 2\r
@@ -75,7 +76,7 @@ public final class ITFReader extends AbstractOneDReader {
       {N, W, N, W, N}  // 9\r
   };\r
 \r
-  public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {\r
+  public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws FormatException, NotFoundException {\r
 \r
     // Find out where the Middle section (payload) starts & ends\r
     int[] startRange = decodeStart(row);\r
@@ -106,7 +107,7 @@ public final class ITFReader extends AbstractOneDReader {
 \r
     }\r
     if (!lengthOK) {\r
-      throw ReaderException.getInstance();\r
+      throw FormatException.getFormatInstance();\r
     }\r
 \r
     return new Result(\r
@@ -121,10 +122,10 @@ public final class ITFReader extends AbstractOneDReader {
    * @param row          row of black/white values to search\r
    * @param payloadStart offset of start pattern\r
    * @param resultString {@link StringBuffer} to append decoded chars to\r
-   * @throws ReaderException if decoding could not complete successfully\r
+   * @throws NotFoundException if decoding could not complete successfully\r
    */\r
-  static void decodeMiddle(BitArray row, int payloadStart, int payloadEnd,\r
-      StringBuffer resultString) throws ReaderException {\r
+  private static void decodeMiddle(BitArray row, int payloadStart, int payloadEnd,\r
+      StringBuffer resultString) throws NotFoundException {\r
 \r
     // Digits are interleaved in pairs - 5 black lines for one digit, and the\r
     // 5\r
@@ -163,9 +164,9 @@ public final class ITFReader extends AbstractOneDReader {
    * @param row row of black/white values to search\r
    * @return Array, containing index of start of 'start block' and end of\r
    *         'start block'\r
-   * @throws ReaderException\r
+   * @throws NotFoundException\r
    */\r
-  int[] decodeStart(BitArray row) throws ReaderException {\r
+  int[] decodeStart(BitArray row) throws NotFoundException {\r
     int endStart = skipWhiteSpace(row);\r
     int[] startPattern = findGuardPattern(row, endStart, START_PATTERN);\r
 \r
@@ -192,9 +193,9 @@ public final class ITFReader extends AbstractOneDReader {
    *\r
    * @param row bit array representing the scanned barcode.\r
    * @param startPattern index into row of the start or end pattern.\r
-   * @throws ReaderException if the quiet zone cannot be found, a ReaderException is thrown.\r
+   * @throws NotFoundException if the quiet zone cannot be found, a ReaderException is thrown.\r
    */\r
-  private void validateQuietZone(BitArray row, int startPattern) throws ReaderException {\r
+  private void validateQuietZone(BitArray row, int startPattern) throws NotFoundException {\r
 \r
     int quietCount = this.narrowLineWidth * 10;  // expect to find this many pixels of quiet zone\r
 \r
@@ -206,7 +207,7 @@ public final class ITFReader extends AbstractOneDReader {
     }\r
     if (quietCount != 0) {\r
       // Unable to find the necessary number of quiet zone pixels.\r
-      throw ReaderException.getInstance();\r
+      throw NotFoundException.getNotFoundInstance();\r
     }\r
   }\r
 \r
@@ -215,9 +216,9 @@ public final class ITFReader extends AbstractOneDReader {
    *\r
    * @param row row of black/white values to search\r
    * @return index of the first black line.\r
-   * @throws ReaderException Throws exception if no black lines are found in the row\r
+   * @throws NotFoundException Throws exception if no black lines are found in the row\r
    */\r
-  private static int skipWhiteSpace(BitArray row) throws ReaderException {\r
+  private static int skipWhiteSpace(BitArray row) throws NotFoundException {\r
     int width = row.getSize();\r
     int endStart = 0;\r
     while (endStart < width) {\r
@@ -227,7 +228,7 @@ public final class ITFReader extends AbstractOneDReader {
       endStart++;\r
     }\r
     if (endStart == width) {\r
-      throw ReaderException.getInstance();\r
+      throw NotFoundException.getNotFoundInstance();\r
     }\r
 \r
     return endStart;\r
@@ -239,10 +240,10 @@ public final class ITFReader extends AbstractOneDReader {
    * @param row row of black/white values to search\r
    * @return Array, containing index of start of 'end block' and end of 'end\r
    *         block'\r
-   * @throws ReaderException\r
+   * @throws NotFoundException\r
    */\r
 \r
-  int[] decodeEnd(BitArray row) throws ReaderException {\r
+  int[] decodeEnd(BitArray row) throws NotFoundException {\r
 \r
     // For convenience, reverse the row and then\r
     // search from 'the start' for the end block\r
@@ -256,8 +257,8 @@ public final class ITFReader extends AbstractOneDReader {
       // ref: http://www.barcode-1.net/i25code.html\r
       validateQuietZone(row, endPattern[0]);\r
 \r
-      // Now recalc the indicies of where the 'endblock' starts & stops to\r
-      // accomodate\r
+      // Now recalculate the indices of where the 'endblock' starts & stops to\r
+      // accommodate\r
       // the reversed nature of the search\r
       int temp = endPattern[0];\r
       endPattern[0] = row.getSize() - endPattern[1];\r
@@ -265,7 +266,7 @@ public final class ITFReader extends AbstractOneDReader {
 \r
       return endPattern;\r
     } finally {\r
-      // Put the row back the righ way.\r
+      // Put the row back the right way.\r
       row.reverse();\r
     }\r
   }\r
@@ -277,11 +278,11 @@ public final class ITFReader extends AbstractOneDReader {
    *                  being searched for as a pattern\r
    * @return start/end horizontal offset of guard pattern, as an array of two\r
    *         ints\r
-   * @throws ReaderException if pattern is not found\r
+   * @throws NotFoundException if pattern is not found\r
    */\r
-  static int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) throws ReaderException {\r
+  private static int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) throws NotFoundException {\r
 \r
-    // TODO: This is very similar to implementation in AbstractUPCEANReader. Consider if they can be\r
+    // TODO: This is very similar to implementation in UPCEANReader. Consider if they can be\r
     // merged to a single method.\r
     int patternLength = pattern.length;\r
     int[] counters = new int[patternLength];\r
@@ -310,10 +311,10 @@ public final class ITFReader extends AbstractOneDReader {
           counterPosition++;\r
         }\r
         counters[counterPosition] = 1;\r
-        isWhite ^= true; // isWhite = !isWhite;\r
+        isWhite = !isWhite;\r
       }\r
     }\r
-    throw ReaderException.getInstance();\r
+    throw NotFoundException.getNotFoundInstance();\r
   }\r
 \r
   /**\r
@@ -322,9 +323,9 @@ public final class ITFReader extends AbstractOneDReader {
    *\r
    * @param counters the counts of runs of observed black/white/black/... values\r
    * @return The decoded digit\r
-   * @throws ReaderException if digit cannot be decoded\r
+   * @throws NotFoundException if digit cannot be decoded\r
    */\r
-  private static int decodeDigit(int[] counters) throws ReaderException {\r
+  private static int decodeDigit(int[] counters) throws NotFoundException {\r
 \r
     int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept\r
     int bestMatch = -1;\r
@@ -340,8 +341,8 @@ public final class ITFReader extends AbstractOneDReader {
     if (bestMatch >= 0) {\r
       return bestMatch;\r
                } else {\r
-                       throw ReaderException.getInstance();\r
+                       throw NotFoundException.getNotFoundInstance();\r
                }\r
        }\r
 \r
-}
\ No newline at end of file
+}\r