2 * Copyright 2007 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.datamatrix;
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.DecodeHintType;
21 import com.google.zxing.BinaryBitmap;
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.BinaryBitmap;
28 import com.google.zxing.common.BitMatrix;
29 import com.google.zxing.common.DecoderResult;
30 import com.google.zxing.common.DetectorResult;
31 import com.google.zxing.datamatrix.decoder.Decoder;
32 import com.google.zxing.datamatrix.detector.Detector;
34 import java.util.Hashtable;
37 * This implementation can detect and decode Data Matrix codes in an image.
39 * @author bbrown@google.com (Brian Brown)
41 public final class DataMatrixReader implements Reader {
43 private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
45 private final Decoder decoder = new Decoder();
48 * Locates and decodes a Data Matrix code in an image.
50 * @return a String representing the content encoded by the Data Matrix code
51 * @throws ReaderException if a Data Matrix code cannot be found, or cannot be decoded
53 public Result decode(BinaryBitmap image) throws ReaderException {
54 return decode(image, null);
57 public Result decode(BinaryBitmap image, Hashtable hints)
58 throws ReaderException {
59 DecoderResult decoderResult;
61 if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
62 BitMatrix bits = extractPureBits(image);
63 decoderResult = decoder.decode(bits);
66 DetectorResult detectorResult = new Detector(image).detect();
67 decoderResult = decoder.decode(detectorResult.getBits());
68 points = detectorResult.getPoints();
70 Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.DATAMATRIX);
71 if (decoderResult.getByteSegments() != null) {
72 result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
78 * This method detects a Data Matrix code in a "pure" image -- that is, pure monochrome image
79 * which contains only an unrotated, unskewed, image of a Data Matrix code, with some white border
80 * around it. This is a specialized method that works exceptionally fast in this special
83 private static BitMatrix extractPureBits(BinaryBitmap image) throws ReaderException {
84 // Now need to determine module size in pixels
86 int height = image.getHeight();
87 int width = image.getWidth();
88 int minDimension = Math.min(height, width);
90 // First, skip white border by tracking diagonally from the top left down and to the right:
92 while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {
95 if (borderWidth == minDimension) {
96 throw ReaderException.getInstance();
99 // And then keep tracking across the top-left black module to determine module size
100 int moduleEnd = borderWidth + 1;
101 while (moduleEnd < width && image.isBlack(moduleEnd, borderWidth)) {
104 if (moduleEnd == width) {
105 throw ReaderException.getInstance();
108 int moduleSize = moduleEnd - borderWidth;
110 // And now find where the bottommost black module on the first column ends
111 int columnEndOfSymbol = height - 1;
112 while (columnEndOfSymbol >= 0 && !image.isBlack(borderWidth, columnEndOfSymbol)) {
115 if (columnEndOfSymbol < 0) {
116 throw ReaderException.getInstance();
120 // Make sure width of barcode is a multiple of module size
121 if ((columnEndOfSymbol - borderWidth) % moduleSize != 0) {
122 throw ReaderException.getInstance();
124 int dimension = (columnEndOfSymbol - borderWidth) / moduleSize;
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;
131 int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
132 if (sampleDimension >= width || sampleDimension >= height) {
133 throw ReaderException.getInstance();
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)) {