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