Fix small display problem when extension starts with 9
[zxing.git] / core / src / com / google / zxing / oned / UPCAReader.java
1 /*
2  * Copyright 2008 ZXing authors
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.BarcodeFormat;
20 import com.google.zxing.BinaryBitmap;
21 import com.google.zxing.ChecksumException;
22 import com.google.zxing.FormatException;
23 import com.google.zxing.NotFoundException;
24 import com.google.zxing.Result;
25 import com.google.zxing.common.BitArray;
26
27 import java.util.Hashtable;
28
29 /**
30  * <p>Implements decoding of the UPC-A format.</p>
31  *
32  * @author dswitkin@google.com (Daniel Switkin)
33  * @author Sean Owen
34  */
35 public final class UPCAReader extends UPCEANReader {
36
37   private final UPCEANReader ean13Reader = new EAN13Reader();
38
39   public Result decodeRow(int rowNumber, BitArray row, int[] startGuardRange, Hashtable hints)
40       throws NotFoundException, FormatException, ChecksumException {
41     return maybeReturnResult(ean13Reader.decodeRow(rowNumber, row, startGuardRange, hints));
42   }
43
44   public Result decodeRow(int rowNumber, BitArray row, Hashtable hints)
45       throws NotFoundException, FormatException, ChecksumException {
46     return maybeReturnResult(ean13Reader.decodeRow(rowNumber, row, hints));
47   }
48
49   public Result decode(BinaryBitmap image) throws NotFoundException, FormatException {
50     return maybeReturnResult(ean13Reader.decode(image));
51   }
52
53   public Result decode(BinaryBitmap image, Hashtable hints) throws NotFoundException, FormatException {
54     return maybeReturnResult(ean13Reader.decode(image, hints));
55   }
56
57   BarcodeFormat getBarcodeFormat() {
58     return BarcodeFormat.UPC_A;
59   }
60
61   protected int decodeMiddle(BitArray row, int[] startRange, StringBuffer resultString)
62       throws NotFoundException {
63     return ean13Reader.decodeMiddle(row, startRange, resultString);
64   }
65
66   private static Result maybeReturnResult(Result result) throws FormatException {
67     String text = result.getText();
68     if (text.charAt(0) == '0') {
69       return new Result(text.substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
70     } else {
71       throw FormatException.getFormatInstance();
72     }
73   }
74
75 }