2 * Copyright 2007 Google Inc.
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.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.datamatrix.decoder.Decoder;
28 import java.util.Hashtable;
31 * This implementation can detect and decode Data Matrix codes in an image.
33 * @author bbrown@google.com (Brian Brown)
35 public final class DataMatrixReader implements Reader {
37 private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
39 private final Decoder decoder = new Decoder();
42 * Locates and decodes a Data Matrix code in an image.
44 * @return a String representing the content encoded by the Data Matrix code
45 * @throws ReaderException if a Data Matrix code cannot be found, or cannot be decoded
47 public Result decode(MonochromeBitmapSource image) throws ReaderException {
48 return decode(image, null);
51 public Result decode(MonochromeBitmapSource image, Hashtable hints)
52 throws ReaderException {
55 //if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
56 BitMatrix bits = extractPureBits(image);
57 text = decoder.decode(bits);
60 // DetectorResult result = new Detector(image).detect();
61 // text = decoder.decode(result.getBits());
62 // points = result.getPoints();
64 return new Result(text, points, BarcodeFormat.DATAMATRIX);
68 * This method detects a Data Matrix code in a "pure" image -- that is, pure monochrome image
69 * which contains only an unrotated, unskewed, image of a Data Matrix code, with some white border
70 * around it. This is a specialized method that works exceptionally fast in this special
73 private static BitMatrix extractPureBits(MonochromeBitmapSource image) throws ReaderException {
74 // Now need to determine module size in pixels
76 int height = image.getHeight();
77 int width = image.getWidth();
78 int minDimension = Math.min(height, width);
80 // First, skip white border by tracking diagonally from the top left down and to the right:
82 while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {
85 if (borderWidth == minDimension) {
86 throw new ReaderException("No black pixels found along diagonal");
89 // And then keep tracking across the top-left black module to determine module size
90 int moduleEnd = borderWidth + 1;
91 while (moduleEnd < width && image.isBlack(moduleEnd, borderWidth)) {
94 if (moduleEnd == width) {
95 throw new ReaderException("No end to black pixels found along row");
98 int moduleSize = moduleEnd - borderWidth;
100 // And now find where the bottommost black module on the first column ends
101 int columnEndOfSymbol = height - 1;
102 while (columnEndOfSymbol >= 0 && !image.isBlack(borderWidth, columnEndOfSymbol)) {
105 if (columnEndOfSymbol < 0) {
106 throw new ReaderException("Can't find end of bottommost black module");
110 // Make sure width of barcode is a multiple of module size
111 if ((columnEndOfSymbol - borderWidth) % moduleSize != 0) {
112 throw new ReaderException("Bad module size / width: " + moduleSize +
113 " / " + (columnEndOfSymbol - borderWidth));
115 int dimension = (columnEndOfSymbol - borderWidth) / moduleSize;
117 // Push in the "border" by half the module width so that we start
118 // sampling in the middle of the module. Just in case the image is a
119 // little off, this will help recover.
120 borderWidth += moduleSize >> 1;
122 int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
123 if (sampleDimension >= width || sampleDimension >= height) {
124 throw new ReaderException("Estimated pure image size is beyond image boundaries");
127 // Now just read off the bits
128 BitMatrix bits = new BitMatrix(dimension);
129 for (int i = 0; i < dimension; i++) {
130 int iOffset = borderWidth + i * moduleSize;
131 for (int j = 0; j < dimension; j++) {
132 if (image.isBlack(borderWidth + j * moduleSize, iOffset)) {