Miscellaneous changes from FindBugs analysis
[zxing.git] / core / src / com / google / zxing / datamatrix / DataMatrixReader.java
index 5d7b4a3..18e88b6 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.
@@ -18,15 +18,17 @@ package com.google.zxing.datamatrix;
 
 import com.google.zxing.BarcodeFormat;
 import com.google.zxing.DecodeHintType;
-import com.google.zxing.MonochromeBitmapSource;
+import com.google.zxing.BinaryBitmap;
 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 com.google.zxing.datamatrix.detector.DetectorResult;
 
 import java.util.Hashtable;
 
@@ -47,24 +49,31 @@ public final class DataMatrixReader implements Reader {
    * @return a String representing the content encoded by the Data Matrix code
    * @throws ReaderException if a Data Matrix code cannot be found, or cannot be decoded
    */
-  public Result decode(MonochromeBitmapSource image) throws ReaderException {
+  public Result decode(BinaryBitmap image) throws ReaderException {
     return decode(image, null);
   }
 
-  public Result decode(MonochromeBitmapSource image, Hashtable hints)
+  public Result decode(BinaryBitmap image, Hashtable hints)
       throws ReaderException {
-    String text;
+    DecoderResult decoderResult;
     ResultPoint[] points;
-    //if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
-      BitMatrix bits = extractPureBits(image);
-      text = decoder.decode(bits);
+    if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
+      BitMatrix bits = extractPureBits(image.getBlackMatrix());
+      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.getBlackMatrix()).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());
+    }
+    if (decoderResult.getECLevel() != null) {
+      result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel().toString());
+    }
+    return result;
   }
 
   /**
@@ -73,33 +82,46 @@ 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(BitMatrix 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.get(borderWidth, borderWidth)) {
       borderWidth++;
     }
+    if (borderWidth == minDimension) {
+      throw ReaderException.getInstance();
+    }
+
     // 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.get(moduleEnd, borderWidth)) {
       moduleEnd++;
     }
+    if (moduleEnd == width) {
+      throw ReaderException.getInstance();
+    }
+
     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.get(borderWidth, columnEndOfSymbol)) {
        columnEndOfSymbol--;
     }
+    if (columnEndOfSymbol < 0) {
+      throw ReaderException.getInstance();
+    }
     columnEndOfSymbol++;
 
     // Make sure width of barcode is a multiple of module size
     if ((columnEndOfSymbol - borderWidth) % moduleSize != 0) {
-      throw new ReaderException("Bad module size / width: " + moduleSize +
-          " / " + (columnEndOfSymbol - borderWidth));
+      throw ReaderException.getInstance();
     }
     int dimension = (columnEndOfSymbol - borderWidth) / moduleSize;
 
@@ -108,13 +130,18 @@ 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 ReaderException.getInstance();
+    }
+
     // Now just read off the bits
     BitMatrix bits = new BitMatrix(dimension);
     for (int i = 0; i < dimension; i++) {
       int iOffset = borderWidth + i * moduleSize;
       for (int j = 0; j < dimension; j++) {
-        if (image.isBlack(borderWidth + j * moduleSize, iOffset)) {
-          bits.set(i, j);
+        if (image.get(borderWidth + j * moduleSize, iOffset)) {
+          bits.set(j, i);
         }
       }
     }