Add to result the raw, but parsed, bytes of byte segments in 2D barcodes
[zxing.git] / core / src / com / google / zxing / datamatrix / DataMatrixReader.java
index e5bff73..1e602cd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2007 Google Inc.
+ * 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.
 package com.google.zxing.datamatrix;
 
 import com.google.zxing.BarcodeFormat;
+import com.google.zxing.DecodeHintType;
 import com.google.zxing.MonochromeBitmapSource;
 import com.google.zxing.Reader;
 import com.google.zxing.ReaderException;
 import com.google.zxing.Result;
 import com.google.zxing.ResultPoint;
+import com.google.zxing.ResultMetadataType;
 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;
 
@@ -50,18 +55,22 @@ public final class DataMatrixReader implements Reader {
 
   public Result decode(MonochromeBitmapSource image, Hashtable hints)
       throws ReaderException {
-    String text;
+    DecoderResult decoderResult;
     ResultPoint[] points;
-    //if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
+    if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
       BitMatrix bits = extractPureBits(image);
-      text = decoder.decode(bits);
+      decoderResult = decoder.decode(bits);
       points = NO_POINTS;
-    //} else {
-    //  DetectorResult result = new Detector(image).detect();
-    //  text = decoder.decode(result.getBits());
-    //  points = result.getPoints();
-    //}
-    return new Result(text, points, BarcodeFormat.DATAMATRIX);
+    } else {
+      DetectorResult detectorResult = new Detector(image).detect();
+      decoderResult = decoder.decode(detectorResult.getBits());
+      points = detectorResult.getPoints();
+    }
+    Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.DATAMATRIX);
+    if (decoderResult.getByteSegments() != null) {
+      result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
+    }
+    return result;
   }
 
   /**
@@ -70,27 +79,41 @@ public final class DataMatrixReader implements Reader {
    * around it. This is a specialized method that works exceptionally fast in this special
    * case.
    */
-  private static BitMatrix extractPureBits(MonochromeBitmapSource image)
-      throws ReaderException {
+  private static BitMatrix extractPureBits(MonochromeBitmapSource image) throws ReaderException {
     // Now need to determine module size in pixels
 
-       // First, skip white border by tracking diagonally from the top left down and to the right:
+    int height = image.getHeight();
+    int width = image.getWidth();
+    int minDimension = Math.min(height, width);
+
+    // First, skip white border by tracking diagonally from the top left down and to the right:
     int borderWidth = 0;
-    while (!image.isBlack(borderWidth, borderWidth)) {
+    while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {
       borderWidth++;
     }
+    if (borderWidth == minDimension) {
+      throw new ReaderException("No black pixels found along diagonal");
+    }
+
     // And then keep tracking across the top-left black module to determine module size
     int moduleEnd = borderWidth + 1;
-    while (image.isBlack(moduleEnd, borderWidth)) {
+    while (moduleEnd < width && image.isBlack(moduleEnd, borderWidth)) {
       moduleEnd++;
     }
+    if (moduleEnd == width) {
+      throw new ReaderException("No end to black pixels found along row");
+    }
+
     int moduleSize = moduleEnd - borderWidth;
 
     // And now find where the bottommost black module on the first column ends
-    int columnEndOfSymbol = image.getHeight() - 1;
-    while (!image.isBlack(borderWidth, columnEndOfSymbol)) {
+    int columnEndOfSymbol = height - 1;
+    while (columnEndOfSymbol >= 0 && !image.isBlack(borderWidth, columnEndOfSymbol)) {
        columnEndOfSymbol--;
     }
+    if (columnEndOfSymbol < 0) {
+      throw new ReaderException("Can't find end of bottommost black module");
+    }
     columnEndOfSymbol++;
 
     // Make sure width of barcode is a multiple of module size
@@ -105,6 +128,11 @@ public final class DataMatrixReader implements Reader {
     // little off, this will help recover.
     borderWidth += moduleSize >> 1;
 
+    int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
+    if (sampleDimension >= width || sampleDimension >= height) {
+      throw new ReaderException("Estimated pure image size is beyond image boundaries");
+    }
+
     // Now just read off the bits
     BitMatrix bits = new BitMatrix(dimension);
     for (int i = 0; i < dimension; i++) {