package com.google.zxing.datamatrix.detector;
-import com.google.zxing.ReaderException;
+import com.google.zxing.NotFoundException;
import com.google.zxing.ResultPoint;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.Collections;
import com.google.zxing.common.Comparator;
import com.google.zxing.common.DetectorResult;
import com.google.zxing.common.GridSampler;
-import com.google.zxing.common.detector.MonochromeRectangleDetector;
+import com.google.zxing.common.detector.WhiteRectangleDetector;
import java.util.Enumeration;
import java.util.Hashtable;
*/
public final class Detector {
- //private static final int MAX_MODULES = 32;
-
// Trick to avoid creating new Integer objects below -- a sort of crude copy of
// the Integer.valueOf(int) optimization added in Java 5, not in J2ME
private static final Integer[] INTEGERS =
// No, can't use valueOf()
private final BitMatrix image;
- private final MonochromeRectangleDetector rectangleDetector;
+ private final WhiteRectangleDetector rectangleDetector;
public Detector(BitMatrix image) {
this.image = image;
- rectangleDetector = new MonochromeRectangleDetector(image);
+ rectangleDetector = new WhiteRectangleDetector(image);
}
/**
* <p>Detects a Data Matrix Code in an image.</p>
*
- * @return {@link DetectorResult} encapsulating results of detecting a QR Code
- * @throws ReaderException if no Data Matrix Code can be found
+ * @return {@link DetectorResult} encapsulating results of detecting a Data Matrix Code
+ * @throws NotFoundException if no Data Matrix Code can be found
*/
- public DetectorResult detect() throws ReaderException {
+ public DetectorResult detect() throws NotFoundException {
ResultPoint[] cornerPoints = rectangleDetector.detect();
ResultPoint pointA = cornerPoints[0];
}
if (maybeTopLeft == null || bottomLeft == null || maybeBottomRight == null) {
- throw ReaderException.getInstance();
+ throw NotFoundException.getNotFoundInstance();
}
// Bottom left is correct but top left and bottom right might be switched
// The top right point is actually the corner of a module, which is one of the two black modules
// adjacent to the white module at the top right. Tracing to that corner from either the top left
- // or bottom right should work here. The number of transitions could be higher than it should be
- // due to noise. So we try both and take the min.
-
- int dimension = Math.min(transitionsBetween(topLeft, topRight).getTransitions(),
- transitionsBetween(bottomRight, topRight).getTransitions());
- if ((dimension & 0x01) == 1) {
+ // or bottom right should work here.
+
+
+ int dimensionTop = transitionsBetween(topLeft, topRight).getTransitions();
+ int dimensionRight = transitionsBetween(bottomRight, topRight).getTransitions();
+
+ if ((dimensionTop & 0x01) == 1) {
// it can't be odd, so, round... up?
- dimension++;
+ dimensionTop++;
}
- dimension += 2;
+ dimensionTop += 2;
+
+ if ((dimensionRight & 0x01) == 1) {
+ // it can't be odd, so, round... up?
+ dimensionRight++;
+ }
+ dimensionRight += 2;
+
+ BitMatrix bits = null;
+ ResultPoint correctedTopRight = null;
+
+ if (dimensionTop >= dimensionRight * 2 || dimensionRight >= dimensionTop * 2){
+ //The matrix is rectangular
+
+ correctedTopRight = correctTopRightRectangular(bottomLeft, bottomRight, topLeft, topRight, dimensionTop, dimensionRight);
+ if (correctedTopRight == null){
+ correctedTopRight = topRight;
+ }
+
+ dimensionTop = transitionsBetween(topLeft, correctedTopRight).getTransitions();
+ dimensionRight = transitionsBetween(bottomRight, correctedTopRight).getTransitions();
+
+ if ((dimensionTop & 0x01) == 1) {
+ // it can't be odd, so, round... up?
+ dimensionTop++;
+ }
+
+ if ((dimensionRight & 0x01) == 1) {
+ // it can't be odd, so, round... up?
+ dimensionRight++;
+ }
+
+ bits = sampleGrid(image, topLeft, bottomLeft, bottomRight, correctedTopRight, dimensionTop, dimensionRight);
+
+ } else {
+ //The matrix is square
+
+ int dimension = Math.min(dimensionRight, dimensionTop);
+ //correct top right point to match the white module
+ correctedTopRight = correctTopRight(bottomLeft, bottomRight, topLeft, topRight, dimension);
+ if (correctedTopRight == null){
+ correctedTopRight = topRight;
+ }
+
+ //We redetermine the dimension using the corrected top right point
+ int dimensionCorrected = Math.max(transitionsBetween(topLeft, correctedTopRight).getTransitions(),
+ transitionsBetween(bottomRight, correctedTopRight).getTransitions());
+ dimensionCorrected++;
+ if ((dimensionCorrected & 0x01) == 1) {
+ dimensionCorrected++;
+ }
- BitMatrix bits = sampleGrid(image, topLeft, bottomLeft, bottomRight, dimension);
- return new DetectorResult(bits, new ResultPoint[] {pointA, pointB, pointC, pointD});
+ bits = sampleGrid(image, topLeft, bottomLeft, bottomRight, correctedTopRight, dimensionCorrected, dimensionCorrected);
+ }
+
+
+ return new DetectorResult(bits, new ResultPoint[]{topLeft, bottomLeft, bottomRight, correctedTopRight});
+ }
+
+ /**
+ * Calculates the position of the white top right module using the output of the rectangle detector for a rectangular matrix
+ */
+ private ResultPoint correctTopRightRectangular(ResultPoint bottomLeft,
+ ResultPoint bottomRight, ResultPoint topLeft, ResultPoint topRight,
+ int dimensionTop, int dimensionRight) {
+
+ float corr = distance(bottomLeft, bottomRight) / (float)dimensionTop;
+ int norm = distance(topLeft, topRight);
+ float cos = (topRight.getX() - topLeft.getX()) / norm;
+ float sin = (topRight.getY() - topLeft.getY()) / norm;
+
+ ResultPoint c1 = new ResultPoint(topRight.getX()+corr*cos, topRight.getY()+corr*sin);
+
+ corr = distance(bottomLeft, topLeft) / (float)dimensionRight;
+ norm = distance(bottomRight, topRight);
+ cos = (topRight.getX() - bottomRight.getX()) / norm;
+ sin = (topRight.getY() - bottomRight.getY()) / norm;
+
+ ResultPoint c2 = new ResultPoint(topRight.getX()+corr*cos, topRight.getY()+corr*sin);
+
+ if (!isValid(c1)){
+ if (isValid(c2)){
+ return c2;
+ }
+ return null;
+ } else if (!isValid(c2)){
+ return c1;
+ }
+
+ int l1 = Math.abs(dimensionTop - transitionsBetween(topLeft, c1).getTransitions()) +
+ Math.abs(dimensionRight - transitionsBetween(bottomRight, c1).getTransitions());
+ int l2 = Math.abs(dimensionTop - transitionsBetween(topLeft, c2).getTransitions()) +
+ Math.abs(dimensionRight - transitionsBetween(bottomRight, c2).getTransitions());
+
+ if (l1 <= l2){
+ return c1;
+ }
+
+ return c2;
+}
+
+/**
+ * Calculates the position of the white top right module using the output of the rectangle detector for a square matrix
+ */
+ private ResultPoint correctTopRight(ResultPoint bottomLeft,
+ ResultPoint bottomRight,
+ ResultPoint topLeft,
+ ResultPoint topRight,
+ int dimension) {
+
+ float corr = distance(bottomLeft, bottomRight) / (float) dimension;
+ int norm = distance(topLeft, topRight);
+ float cos = (topRight.getX() - topLeft.getX()) / norm;
+ float sin = (topRight.getY() - topLeft.getY()) / norm;
+
+ ResultPoint c1 = new ResultPoint(topRight.getX() + corr * cos, topRight.getY() + corr * sin);
+
+ corr = distance(bottomLeft, bottomRight) / (float) dimension;
+ norm = distance(bottomRight, topRight);
+ cos = (topRight.getX() - bottomRight.getX()) / norm;
+ sin = (topRight.getY() - bottomRight.getY()) / norm;
+
+ ResultPoint c2 = new ResultPoint(topRight.getX() + corr * cos, topRight.getY() + corr * sin);
+
+ if (!isValid(c1)) {
+ if (isValid(c2)) {
+ return c2;
+ }
+ return null;
+ } else if (!isValid(c2)) {
+ return c1;
+ }
+
+ int l1 = Math.abs(transitionsBetween(topLeft, c1).getTransitions() -
+ transitionsBetween(bottomRight, c1).getTransitions());
+ int l2 = Math.abs(transitionsBetween(topLeft, c2).getTransitions() -
+ transitionsBetween(bottomRight, c2).getTransitions());
+
+ return l1 <= l2 ? c1 : c2;
+ }
+
+ private boolean isValid(ResultPoint p) {
+ return (p.getX() >= 0 && p.getX() < image.width && p.getY() > 0 && p.getY() < image.height);
+ }
+
+ /**
+ * Ends up being a bit faster than Math.round(). This merely rounds its
+ * argument to the nearest int, where x.5 rounds up.
+ */
+ private static int round(float d) {
+ return (int) (d + 0.5f);
+ }
+
+// L2 distance
+ private static int distance(ResultPoint a, ResultPoint b) {
+ return round((float) Math.sqrt((a.getX() - b.getX())
+ * (a.getX() - b.getX()) + (a.getY() - b.getY())
+ * (a.getY() - b.getY())));
}
/**
ResultPoint topLeft,
ResultPoint bottomLeft,
ResultPoint bottomRight,
- int dimension) throws ReaderException {
-
- // We make up the top right point for now, based on the others.
- // TODO: we actually found a fourth corner above and figured out which of two modules
- // it was the corner of. We could use that here and adjust for perspective distortion.
- float topRightX = (bottomRight.getX() - bottomLeft.getX()) + topLeft.getX();
- float topRightY = (bottomRight.getY() - bottomLeft.getY()) + topLeft.getY();
+ ResultPoint topRight,
+ int dimensionX,
+ int dimensionY) throws NotFoundException {
- // Note that unlike in the QR Code sampler, we didn't find the center of modules, but the
- // very corners. So there is no 0.5f here; 0.0f is right.
GridSampler sampler = GridSampler.getInstance();
- return sampler.sampleGrid(
- image,
- dimension,
- 0.0f,
- 0.0f,
- dimension,
- 0.0f,
- dimension,
- dimension,
- 0.0f,
- dimension,
- topLeft.getX(),
- topLeft.getY(),
- topRightX,
- topRightY,
- bottomRight.getX(),
- bottomRight.getY(),
- bottomLeft.getX(),
- bottomLeft.getY());
+
+ return sampler.sampleGrid(image,
+ dimensionX,
+ dimensionY,
+ 0.5f,
+ 0.5f,
+ dimensionX - 0.5f,
+ 0.5f,
+ dimensionX - 0.5f,
+ dimensionY - 0.5f,
+ 0.5f,
+ dimensionY - 0.5f,
+ topLeft.getX(),
+ topLeft.getY(),
+ topRight.getX(),
+ topRight.getY(),
+ bottomRight.getX(),
+ bottomRight.getY(),
+ bottomLeft.getX(),
+ bottomLeft.getY());
}
/**
}
error += dy;
if (error > 0) {
+ if (y == toY) {
+ break;
+ }
y += ystep;
error -= dx;
}