2 * Copyright 2009 ZXing authors
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package com.google.zxing.pdf417;
\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.Reader;
\r
23 import com.google.zxing.ReaderException;
\r
24 import com.google.zxing.Result;
\r
25 import com.google.zxing.ResultPoint;
\r
26 import com.google.zxing.common.BitMatrix;
\r
27 import com.google.zxing.common.DecoderResult;
\r
28 import com.google.zxing.common.DetectorResult;
\r
29 import com.google.zxing.pdf417.decoder.Decoder;
\r
30 import com.google.zxing.pdf417.detector.Detector;
\r
32 import java.util.Hashtable;
\r
35 * This implementation can detect and decode PDF417 codes in an image.
\r
37 * @author SITA Lab (kevin.osullivan@sita.aero)
\r
39 public final class PDF417Reader implements Reader {
\r
41 private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
\r
43 private final Decoder decoder = new Decoder();
\r
46 * Locates and decodes a PDF417 code in an image.
\r
48 * @return a String representing the content encoded by the PDF417 code
\r
49 * @throws ReaderException if a PDF417 code cannot be found, or cannot be decoded
\r
51 public Result decode(BinaryBitmap image) throws ReaderException {
\r
52 return decode(image, null);
\r
55 public Result decode(BinaryBitmap image, Hashtable hints)
\r
56 throws ReaderException {
\r
57 DecoderResult decoderResult;
\r
58 ResultPoint[] points;
\r
59 if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
\r
60 BitMatrix bits = extractPureBits(image);
\r
61 decoderResult = decoder.decode(bits);
\r
64 DetectorResult detectorResult = new Detector(image).detect();
\r
65 decoderResult = decoder.decode(detectorResult.getBits());
\r
66 points = detectorResult.getPoints();
\r
68 return new Result(decoderResult.getText(), decoderResult.getRawBytes(), points,
\r
69 BarcodeFormat.PDF417);
\r
73 * This method detects a barcode in a "pure" image -- that is, pure monochrome image
\r
74 * which contains only an unrotated, unskewed, image of a barcode, with some white border
\r
75 * around it. This is a specialized method that works exceptionally fast in this special
\r
78 private static BitMatrix extractPureBits(BinaryBitmap image) throws ReaderException {
\r
79 // Now need to determine module size in pixels
\r
81 int height = image.getHeight();
\r
82 int width = image.getWidth();
\r
83 int minDimension = Math.min(height, width);
\r
85 // First, skip white border by tracking diagonally from the top left down and to the right:
\r
86 int borderWidth = 0;
\r
87 while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {
\r
90 if (borderWidth == minDimension) {
\r
91 throw ReaderException.getInstance();
\r
94 // And then keep tracking across the top-left black module to determine module size
\r
95 int moduleEnd = borderWidth;
\r
96 while (moduleEnd < minDimension && image.isBlack(moduleEnd, moduleEnd)) {
\r
99 if (moduleEnd == minDimension) {
\r
100 throw ReaderException.getInstance();
\r
103 int moduleSize = moduleEnd - borderWidth;
\r
105 // And now find where the rightmost black module on the first row ends
\r
106 int rowEndOfSymbol = width - 1;
\r
107 while (rowEndOfSymbol >= 0 && !image.isBlack(rowEndOfSymbol, borderWidth)) {
\r
110 if (rowEndOfSymbol < 0) {
\r
111 throw ReaderException.getInstance();
\r
115 // Make sure width of barcode is a multiple of module size
\r
116 if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) {
\r
117 throw ReaderException.getInstance();
\r
119 int dimension = (rowEndOfSymbol - borderWidth) / moduleSize;
\r
121 // Push in the "border" by half the module width so that we start
\r
122 // sampling in the middle of the module. Just in case the image is a
\r
123 // little off, this will help recover.
\r
124 borderWidth += moduleSize >> 1;
\r
126 int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
\r
127 if (sampleDimension >= width || sampleDimension >= height) {
\r
128 throw ReaderException.getInstance();
\r
131 // Now just read off the bits
\r
132 BitMatrix bits = new BitMatrix(dimension);
\r
133 for (int i = 0; i < dimension; i++) {
\r
134 int iOffset = borderWidth + i * moduleSize;
\r
135 for (int j = 0; j < dimension; j++) {
\r
136 if (image.isBlack(borderWidth + j * moduleSize, iOffset)) {
\r