fd597abf9ddd6d8210df09f7dd85d98feb10c88a
[zxing.git] / core / src / com / google / zxing / datamatrix / DataMatrixReader.java
1 /*
2  * Copyright 2007 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.datamatrix;
18
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.DecodeHintType;
21 import com.google.zxing.MonochromeBitmapSource;
22 import com.google.zxing.Reader;
23 import com.google.zxing.ReaderException;
24 import com.google.zxing.Result;
25 import com.google.zxing.ResultPoint;
26 import com.google.zxing.common.BitMatrix;
27 import com.google.zxing.common.DecoderResult;
28 import com.google.zxing.common.DetectorResult;
29 import com.google.zxing.datamatrix.decoder.Decoder;
30 import com.google.zxing.datamatrix.detector.Detector;
31
32 import java.util.Hashtable;
33
34 /**
35  * This implementation can detect and decode Data Matrix codes in an image.
36  *
37  * @author bbrown@google.com (Brian Brown)
38  */
39 public final class DataMatrixReader implements Reader {
40
41   private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
42   
43   private final Decoder decoder = new Decoder();
44
45   /**
46    * Locates and decodes a Data Matrix code in an image.
47    *
48    * @return a String representing the content encoded by the Data Matrix code
49    * @throws ReaderException if a Data Matrix code cannot be found, or cannot be decoded
50    */
51   public Result decode(MonochromeBitmapSource image) throws ReaderException {
52     return decode(image, null);
53   }
54
55   public Result decode(MonochromeBitmapSource image, Hashtable hints)
56       throws ReaderException {
57     DecoderResult decoderResult;
58     ResultPoint[] points;
59     if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
60       BitMatrix bits = extractPureBits(image);
61       decoderResult = decoder.decode(bits);
62       points = NO_POINTS;
63     } else {
64       DetectorResult result = new Detector(image).detect();
65       decoderResult = decoder.decode(result.getBits());
66       points = result.getPoints();
67     }
68     return new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.DATAMATRIX);
69   }
70
71   /**
72    * This method detects a Data Matrix code in a "pure" image -- that is, pure monochrome image
73    * which contains only an unrotated, unskewed, image of a Data Matrix code, with some white border
74    * around it. This is a specialized method that works exceptionally fast in this special
75    * case.
76    */
77   private static BitMatrix extractPureBits(MonochromeBitmapSource image) throws ReaderException {
78     // Now need to determine module size in pixels
79
80     int height = image.getHeight();
81     int width = image.getWidth();
82     int minDimension = Math.min(height, width);
83
84     // First, skip white border by tracking diagonally from the top left down and to the right:
85     int borderWidth = 0;
86     while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {
87       borderWidth++;
88     }
89     if (borderWidth == minDimension) {
90       throw new ReaderException("No black pixels found along diagonal");
91     }
92
93     // And then keep tracking across the top-left black module to determine module size
94     int moduleEnd = borderWidth + 1;
95     while (moduleEnd < width && image.isBlack(moduleEnd, borderWidth)) {
96       moduleEnd++;
97     }
98     if (moduleEnd == width) {
99       throw new ReaderException("No end to black pixels found along row");
100     }
101
102     int moduleSize = moduleEnd - borderWidth;
103
104     // And now find where the bottommost black module on the first column ends
105     int columnEndOfSymbol = height - 1;
106     while (columnEndOfSymbol >= 0 && !image.isBlack(borderWidth, columnEndOfSymbol)) {
107         columnEndOfSymbol--;
108     }
109     if (columnEndOfSymbol < 0) {
110       throw new ReaderException("Can't find end of bottommost black module");
111     }
112     columnEndOfSymbol++;
113
114     // Make sure width of barcode is a multiple of module size
115     if ((columnEndOfSymbol - borderWidth) % moduleSize != 0) {
116       throw new ReaderException("Bad module size / width: " + moduleSize +
117           " / " + (columnEndOfSymbol - borderWidth));
118     }
119     int dimension = (columnEndOfSymbol - borderWidth) / moduleSize;
120
121     // Push in the "border" by half the module width so that we start
122     // sampling in the middle of the module. Just in case the image is a
123     // little off, this will help recover.
124     borderWidth += moduleSize >> 1;
125
126     int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
127     if (sampleDimension >= width || sampleDimension >= height) {
128       throw new ReaderException("Estimated pure image size is beyond image boundaries");
129     }
130
131     // Now just read off the bits
132     BitMatrix bits = new BitMatrix(dimension);
133     for (int i = 0; i < dimension; i++) {
134       int iOffset = borderWidth + i * moduleSize;
135       for (int j = 0; j < dimension; j++) {
136         if (image.isBlack(borderWidth + j * moduleSize, iOffset)) {
137           bits.set(i, j);
138         }
139       }
140     }
141     return bits;
142   }
143
144 }