Added BarcodeFormat to Result, indicating what type of barcode was detected. Added...
[zxing.git] / core / src / com / google / zxing / oned / UPCAReader.java
1 /*
2  * Copyright 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.oned;
18
19 import com.google.zxing.ReaderException;
20 import com.google.zxing.BarcodeFormat;
21 import com.google.zxing.common.BitArray;
22
23 /**
24  * <p>Implements decoding of the UPC-A format.</p>
25  *
26  * @author dswitkin@google.com (Daniel Switkin)
27  * @author srowen@google.com (Sean Owen)
28  */
29 public final class UPCAReader extends AbstractUPCEANReader {
30
31   protected int decodeMiddle(BitArray row, int[] startRange, StringBuffer resultString) throws ReaderException {
32     int middleStart = decodeDigits(row, startRange[1], resultString);
33     int[] middleRange = findGuardPattern(row, middleStart, true, MIDDLE_PATTERN);
34     return decodeDigits(row, middleRange[1], resultString);
35   }
36
37   /**
38    * @param row row of black/white values to decode
39    * @param start horizontal offset from which decoding starts
40    * @param result {@link StringBuffer} to append decoded digits to
41    * @return horizontal offset of first pixel after the six decoded digits
42    * @throws ReaderException if six digits could not be decoded from the row
43    */
44   private static int decodeDigits(BitArray row, int start, StringBuffer result) throws ReaderException {
45     int[] counters = new int[4];
46     int end = row.getSize();
47     int rowOffset = start;
48     for (int x = 0; x < 6 && rowOffset < end; x++) {
49       int bestMatch = decodeDigit(row, counters, rowOffset, L_PATTERNS);
50       result.append((char) ('0' + bestMatch));
51       for (int i = 0; i < counters.length; i++) {
52         rowOffset += counters[i];
53       }
54     }
55     return rowOffset;
56   }
57
58   BarcodeFormat getBarcodeFormat() {
59     return BarcodeFormat.UPC_A;  
60   }
61
62 }