| /* |
| * Copyright 2007 ZXing authors |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.google.zxing.datamatrix; |
| |
| import com.google.zxing.BarcodeFormat; |
| import com.google.zxing.BinaryBitmap; |
| import com.google.zxing.ChecksumException; |
| import com.google.zxing.DecodeHintType; |
| import com.google.zxing.FormatException; |
| import com.google.zxing.NotFoundException; |
| import com.google.zxing.Reader; |
| import com.google.zxing.Result; |
| import com.google.zxing.ResultMetadataType; |
| import com.google.zxing.ResultPoint; |
| import com.google.zxing.common.BitMatrix; |
| import com.google.zxing.common.DecoderResult; |
| import com.google.zxing.common.DetectorResult; |
| import com.google.zxing.datamatrix.decoder.Decoder; |
| import com.google.zxing.datamatrix.detector.Detector; |
| |
| import java.util.Hashtable; |
| |
| /** |
| * This implementation can detect and decode Data Matrix codes in an image. |
| * |
| * @author bbrown@google.com (Brian Brown) |
| */ |
| public final class DataMatrixReader implements Reader { |
| |
| private static final ResultPoint[] NO_POINTS = new ResultPoint[0]; |
| |
| private final Decoder decoder = new Decoder(); |
| |
| /** |
| * Locates and decodes a Data Matrix code in an image. |
| * |
| * @return a String representing the content encoded by the Data Matrix code |
| * @throws NotFoundException if a Data Matrix code cannot be found |
| * @throws FormatException if a Data Matrix code cannot be decoded |
| * @throws ChecksumException if error correction fails |
| */ |
| public Result decode(BinaryBitmap image) throws NotFoundException, ChecksumException, FormatException { |
| return decode(image, null); |
| } |
| |
| public Result decode(BinaryBitmap image, Hashtable hints) |
| throws NotFoundException, ChecksumException, FormatException { |
| DecoderResult decoderResult; |
| ResultPoint[] points; |
| if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) { |
| BitMatrix bits = extractPureBits(image.getBlackMatrix()); |
| decoderResult = decoder.decode(bits); |
| points = NO_POINTS; |
| } else { |
| DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect(); |
| decoderResult = decoder.decode(detectorResult.getBits()); |
| points = detectorResult.getPoints(); |
| } |
| Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, |
| BarcodeFormat.DATA_MATRIX); |
| if (decoderResult.getByteSegments() != null) { |
| result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments()); |
| } |
| if (decoderResult.getECLevel() != null) { |
| result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel().toString()); |
| } |
| return result; |
| } |
| |
| public void reset() { |
| // do nothing |
| } |
| |
| /** |
| * This method detects a Data Matrix code in a "pure" image -- that is, pure monochrome image |
| * which contains only an unrotated, unskewed, image of a Data Matrix code, with some white border |
| * around it. This is a specialized method that works exceptionally fast in this special |
| * case. |
| * |
| * @see com.google.zxing.qrcode.QRCodeReader#extractPureBits(BitMatrix) |
| */ |
| private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException { |
| |
| int height = image.getHeight(); |
| int width = image.getWidth(); |
| int minDimension = Math.min(height, width); |
| |
| // And then keep tracking across the top-left black module to determine module size |
| //int moduleEnd = borderWidth; |
| int[] leftTopBlack = image.getTopLeftOnBit(); |
| if (leftTopBlack == null) { |
| throw NotFoundException.getNotFoundInstance(); |
| } |
| int x = leftTopBlack[0]; |
| int y = leftTopBlack[1]; |
| while (x < minDimension && y < minDimension && image.get(x, y)) { |
| x++; |
| } |
| if (x == minDimension) { |
| throw NotFoundException.getNotFoundInstance(); |
| } |
| |
| int moduleSize = x - leftTopBlack[0]; |
| |
| // And now find where the rightmost black module on the first row ends |
| int rowEndOfSymbol = width - 1; |
| while (rowEndOfSymbol >= 0 && !image.get(rowEndOfSymbol, y)) { |
| rowEndOfSymbol--; |
| } |
| if (rowEndOfSymbol < 0) { |
| throw NotFoundException.getNotFoundInstance(); |
| } |
| rowEndOfSymbol++; |
| |
| // Make sure width of barcode is a multiple of module size |
| if ((rowEndOfSymbol - x) % moduleSize != 0) { |
| throw NotFoundException.getNotFoundInstance(); |
| } |
| int dimension = 2 + ((rowEndOfSymbol - x) / moduleSize); |
| |
| y += moduleSize; |
| |
| // Push in the "border" by half the module width so that we start |
| // sampling in the middle of the module. Just in case the image is a |
| // little off, this will help recover. |
| x -= moduleSize >> 1; |
| y -= moduleSize >> 1; |
| |
| if ((x + (dimension - 1) * moduleSize) >= width || |
| (y + (dimension - 1) * moduleSize) >= height) { |
| throw NotFoundException.getNotFoundInstance(); |
| } |
| |
| // Now just read off the bits |
| BitMatrix bits = new BitMatrix(dimension); |
| for (int i = 0; i < dimension; i++) { |
| int iOffset = y + i * moduleSize; |
| for (int j = 0; j < dimension; j++) { |
| if (image.get(x + j * moduleSize, iOffset)) { |
| bits.set(j, i); |
| } |
| } |
| } |
| return bits; |
| } |
| |
| } |