Pure-barcode mode can now deal with asymmetric borders instead of assuming it's pure...
[zxing.git] / core / src / com / google / zxing / pdf417 / PDF417Reader.java
1 /*\r
2  * Copyright 2009 ZXing authors\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package com.google.zxing.pdf417;\r
18 \r
19 import com.google.zxing.BarcodeFormat;\r
20 import com.google.zxing.BinaryBitmap;\r
21 import com.google.zxing.DecodeHintType;\r
22 import com.google.zxing.FormatException;\r
23 import com.google.zxing.NotFoundException;\r
24 import com.google.zxing.Reader;\r
25 import com.google.zxing.Result;\r
26 import com.google.zxing.ResultPoint;\r
27 import com.google.zxing.common.BitMatrix;\r
28 import com.google.zxing.common.DecoderResult;\r
29 import com.google.zxing.common.DetectorResult;\r
30 import com.google.zxing.pdf417.decoder.Decoder;\r
31 import com.google.zxing.pdf417.detector.Detector;\r
32 import com.google.zxing.qrcode.QRCodeReader;\r
33 \r
34 import java.util.Hashtable;\r
35 \r
36 /**\r
37  * This implementation can detect and decode PDF417 codes in an image.\r
38  *\r
39  * @author SITA Lab (kevin.osullivan@sita.aero)\r
40  */\r
41 public final class PDF417Reader implements Reader {\r
42 \r
43   private static final ResultPoint[] NO_POINTS = new ResultPoint[0];\r
44 \r
45   private final Decoder decoder = new Decoder();\r
46 \r
47   /**\r
48    * Locates and decodes a PDF417 code in an image.\r
49    *\r
50    * @return a String representing the content encoded by the PDF417 code\r
51    * @throws NotFoundException if a PDF417 code cannot be found,\r
52    * @throws FormatException if a PDF417 cannot be decoded\r
53    */\r
54   public Result decode(BinaryBitmap image) throws NotFoundException, FormatException {\r
55     return decode(image, null);\r
56   }\r
57 \r
58   public Result decode(BinaryBitmap image, Hashtable hints)\r
59       throws NotFoundException, FormatException {\r
60     DecoderResult decoderResult;\r
61     ResultPoint[] points;\r
62     if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {\r
63       BitMatrix bits = QRCodeReader.extractPureBits(image.getBlackMatrix());\r
64       decoderResult = decoder.decode(bits);\r
65       points = NO_POINTS;\r
66     } else {\r
67       DetectorResult detectorResult = new Detector(image).detect();\r
68       decoderResult = decoder.decode(detectorResult.getBits());\r
69       points = detectorResult.getPoints();\r
70     }\r
71     return new Result(decoderResult.getText(), decoderResult.getRawBytes(), points,\r
72         BarcodeFormat.PDF417);\r
73   }\r
74 \r
75   public void reset() {\r
76     // do nothing\r
77   }\r
78 \r
79 }\r