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