mirror of
https://github.com/zxing/zxing.git
synced 2025-01-12 19:57:27 -08:00
Refactor some duplicated math methods
git-svn-id: https://zxing.googlecode.com/svn/trunk@2294 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
b0ba223c90
commit
f1358d6c52
|
@ -16,6 +16,8 @@
|
|||
|
||||
package com.google.zxing;
|
||||
|
||||
import com.google.zxing.common.detector.MathUtils;
|
||||
|
||||
/**
|
||||
* <p>Encapsulates a point of interest in an image containing a barcode. Typically, this
|
||||
* would be the location of a finder pattern or the corner of the barcode, for example.</p>
|
||||
|
@ -114,9 +116,7 @@ public class ResultPoint {
|
|||
* @return distance between two points
|
||||
*/
|
||||
public static float distance(ResultPoint pattern1, ResultPoint pattern2) {
|
||||
float xDiff = pattern1.x - pattern2.x;
|
||||
float yDiff = pattern1.y - pattern2.y;
|
||||
return (float) Math.sqrt((double) (xDiff * xDiff + yDiff * yDiff));
|
||||
return MathUtils.distance(pattern1.x, pattern1.y, pattern2.x, pattern2.y);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -21,6 +21,7 @@ import com.google.zxing.ResultPoint;
|
|||
import com.google.zxing.aztec.AztecDetectorResult;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.common.GridSampler;
|
||||
import com.google.zxing.common.detector.MathUtils;
|
||||
import com.google.zxing.common.detector.WhiteRectangleDetector;
|
||||
import com.google.zxing.common.reedsolomon.GenericGF;
|
||||
import com.google.zxing.common.reedsolomon.ReedSolomonDecoder;
|
||||
|
@ -168,21 +169,21 @@ public final class Detector {
|
|||
int dy = bullEyeCornerPoints[0].y-bullEyeCornerPoints[2].y;
|
||||
dy+=dy>0?1:-1;
|
||||
|
||||
int targetcx = round(bullEyeCornerPoints[2].x-ratio*dx);
|
||||
int targetcy = round(bullEyeCornerPoints[2].y-ratio*dy);
|
||||
int targetcx = MathUtils.round(bullEyeCornerPoints[2].x - ratio * dx);
|
||||
int targetcy = MathUtils.round(bullEyeCornerPoints[2].y - ratio * dy);
|
||||
|
||||
int targetax = round(bullEyeCornerPoints[0].x+ratio*dx);
|
||||
int targetay = round(bullEyeCornerPoints[0].y+ratio*dy);
|
||||
int targetax = MathUtils.round(bullEyeCornerPoints[0].x + ratio * dx);
|
||||
int targetay = MathUtils.round(bullEyeCornerPoints[0].y + ratio * dy);
|
||||
|
||||
dx = bullEyeCornerPoints[1].x-bullEyeCornerPoints[3].x;
|
||||
dx+=dx>0?1:-1;
|
||||
dy = bullEyeCornerPoints[1].y-bullEyeCornerPoints[3].y;
|
||||
dy+=dy>0?1:-1;
|
||||
|
||||
int targetdx = round(bullEyeCornerPoints[3].x-ratio*dx);
|
||||
int targetdy = round(bullEyeCornerPoints[3].y-ratio*dy);
|
||||
int targetbx = round(bullEyeCornerPoints[1].x+ratio*dx);
|
||||
int targetby = round(bullEyeCornerPoints[1].y+ratio*dy);
|
||||
int targetdx = MathUtils.round(bullEyeCornerPoints[3].x - ratio * dx);
|
||||
int targetdy = MathUtils.round(bullEyeCornerPoints[3].y - ratio * dy);
|
||||
int targetbx = MathUtils.round(bullEyeCornerPoints[1].x + ratio * dx);
|
||||
int targetby = MathUtils.round(bullEyeCornerPoints[1].y+ratio*dy);
|
||||
|
||||
if (!isValid(targetax, targetay) || !isValid(targetbx, targetby) || !isValid(targetcx, targetcy) || !isValid(targetdx, targetdy)) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
|
@ -294,18 +295,18 @@ public final class Detector {
|
|||
|
||||
int dx = pina.x-pinc.x;
|
||||
int dy = pina.y-pinc.y;
|
||||
int targetcx = round(pinc.x-ratio*dx);
|
||||
int targetcy = round(pinc.y-ratio*dy);
|
||||
int targetax = round(pina.x+ratio*dx);
|
||||
int targetay = round(pina.y+ratio*dy);
|
||||
int targetcx = MathUtils.round(pinc.x-ratio*dx);
|
||||
int targetcy = MathUtils.round(pinc.y-ratio*dy);
|
||||
int targetax = MathUtils.round(pina.x+ratio*dx);
|
||||
int targetay = MathUtils.round(pina.y+ratio*dy);
|
||||
|
||||
dx = pinb.x-pind.x;
|
||||
dy = pinb.y-pind.y;
|
||||
|
||||
int targetdx = round(pind.x-ratio*dx);
|
||||
int targetdy = round(pind.y-ratio*dy);
|
||||
int targetbx = round(pinb.x+ratio*dx);
|
||||
int targetby = round(pinb.y+ratio*dy);
|
||||
int targetdx = MathUtils.round(pind.x-ratio*dx);
|
||||
int targetdy = MathUtils.round(pind.y-ratio*dy);
|
||||
int targetbx = MathUtils.round(pinb.x+ratio*dx);
|
||||
int targetby = MathUtils.round(pinb.y+ratio*dy);
|
||||
|
||||
if (!isValid(targetax, targetay) || !isValid(targetbx, targetby)
|
||||
|| !isValid(targetcx, targetcy) || !isValid(targetdx, targetdy)) {
|
||||
|
@ -356,8 +357,8 @@ public final class Detector {
|
|||
}
|
||||
|
||||
//Compute the center of the rectangle
|
||||
int cx = round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX())/4);
|
||||
int cy = round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY())/4);
|
||||
int cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX())/4);
|
||||
int cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY())/4);
|
||||
|
||||
// Redetermine the white rectangle starting from previously computed center.
|
||||
// This will ensure that we end up with a white rectangle in center bull's eye
|
||||
|
@ -380,8 +381,8 @@ public final class Detector {
|
|||
}
|
||||
|
||||
// Recompute the center of the rectangle
|
||||
cx = round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX())/4);
|
||||
cy = round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY())/4);
|
||||
cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX())/4);
|
||||
cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY())/4);
|
||||
|
||||
return new Point(cx, cy);
|
||||
}
|
||||
|
@ -485,7 +486,7 @@ public final class Detector {
|
|||
float py = p1.y;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
res[i] = image.get(round(px), round(py));
|
||||
res[i] = image.get(MathUtils.round(px), MathUtils.round(py));
|
||||
px+=dx;
|
||||
py+=dy;
|
||||
}
|
||||
|
@ -552,7 +553,7 @@ public final class Detector {
|
|||
for (int i = 0; i < d; i++) {
|
||||
px+=dx;
|
||||
py+=dy;
|
||||
if (image.get(round(px), round(py)) != colorModel) {
|
||||
if (image.get(MathUtils.round(px), MathUtils.round(py)) != colorModel) {
|
||||
error++;
|
||||
}
|
||||
}
|
||||
|
@ -616,19 +617,8 @@ public final class Detector {
|
|||
return x >= 0 && x < image.getWidth() && y > 0 && y < image.getHeight();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 float distance(Point a, Point b) {
|
||||
return (float) Math.sqrt((a.x - b.x)
|
||||
* (a.x - b.x) + (a.y - b.y)
|
||||
* (a.y - b.y));
|
||||
return MathUtils.distance(a.x, a.y, b.x, b.y);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
44
core/src/com/google/zxing/common/detector/MathUtils.java
Normal file
44
core/src/com/google/zxing/common/detector/MathUtils.java
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright 2012 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.common.detector;
|
||||
|
||||
public final class MathUtils {
|
||||
|
||||
private MathUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends up being a bit faster than {@link Math#round(float)}. This merely rounds its
|
||||
* argument to the nearest int, where x.5 rounds up to x+1.
|
||||
*/
|
||||
public static int round(float d) {
|
||||
return (int) (d + 0.5f);
|
||||
}
|
||||
|
||||
public static float distance(float aX, float aY, float bX, float bY) {
|
||||
float xDiff = aX - bX;
|
||||
float yDiff = aY - bY;
|
||||
return (float) Math.sqrt(xDiff * xDiff + yDiff * yDiff);
|
||||
}
|
||||
|
||||
public static float distance(int aX, int aY, int bX, int bY) {
|
||||
int xDiff = aX - bX;
|
||||
int yDiff = aY - bY;
|
||||
return (float) Math.sqrt(xDiff * xDiff + yDiff * yDiff);
|
||||
}
|
||||
|
||||
}
|
|
@ -240,22 +240,14 @@ public final class WhiteRectangleDetector {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
private ResultPoint getBlackPointOnSegment(float aX, float aY, float bX, float bY) {
|
||||
int dist = distanceL2(aX, aY, bX, bY);
|
||||
int dist = MathUtils.round(MathUtils.distance(aX, aY, bX, bY));
|
||||
float xStep = (bX - aX) / dist;
|
||||
float yStep = (bY - aY) / dist;
|
||||
|
||||
for (int i = 0; i < dist; i++) {
|
||||
int x = round(aX + i * xStep);
|
||||
int y = round(aY + i * yStep);
|
||||
int x = MathUtils.round(aX + i * xStep);
|
||||
int y = MathUtils.round(aY + i * yStep);
|
||||
if (image.get(x, y)) {
|
||||
return new ResultPoint(x, y);
|
||||
}
|
||||
|
@ -263,12 +255,6 @@ public final class WhiteRectangleDetector {
|
|||
return null;
|
||||
}
|
||||
|
||||
private static int distanceL2(float aX, float aY, float bX, float bY) {
|
||||
float xDiff = aX - bX;
|
||||
float yDiff = aY - bY;
|
||||
return round((float) Math.sqrt(xDiff * xDiff + yDiff * yDiff));
|
||||
}
|
||||
|
||||
/**
|
||||
* recenters the points of a constant distance towards the center
|
||||
*
|
||||
|
|
|
@ -21,6 +21,7 @@ import com.google.zxing.ResultPoint;
|
|||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.common.DetectorResult;
|
||||
import com.google.zxing.common.GridSampler;
|
||||
import com.google.zxing.common.detector.MathUtils;
|
||||
import com.google.zxing.common.detector.WhiteRectangleDetector;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
@ -305,19 +306,8 @@ public final class Detector {
|
|||
return p.getX() >= 0 && p.getX() < image.getWidth() && p.getY() > 0 && p.getY() < image.getHeight();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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())));
|
||||
return MathUtils.round(ResultPoint.distance(a, b));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -23,6 +23,7 @@ import com.google.zxing.ResultPoint;
|
|||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.common.DetectorResult;
|
||||
import com.google.zxing.common.GridSampler;
|
||||
import com.google.zxing.common.detector.MathUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
@ -393,8 +394,8 @@ public final class Detector {
|
|||
ResultPoint bottomLeft,
|
||||
ResultPoint bottomRight,
|
||||
float moduleWidth) {
|
||||
int topRowDimension = round(ResultPoint.distance(topLeft, topRight) / moduleWidth);
|
||||
int bottomRowDimension = round(ResultPoint.distance(bottomLeft, bottomRight) / moduleWidth);
|
||||
int topRowDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleWidth);
|
||||
int bottomRowDimension = MathUtils.round(ResultPoint.distance(bottomLeft, bottomRight) / moduleWidth);
|
||||
return ((((topRowDimension + bottomRowDimension) >> 1) + 8) / 17) * 17;
|
||||
}
|
||||
|
||||
|
@ -430,14 +431,6 @@ public final class Detector {
|
|||
bottomLeft.getY()); // p4FromY
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param matrix row of black/white values to search
|
||||
* @param column x position to start search
|
||||
|
|
|
@ -25,6 +25,7 @@ import com.google.zxing.common.BitMatrix;
|
|||
import com.google.zxing.common.DetectorResult;
|
||||
import com.google.zxing.common.GridSampler;
|
||||
import com.google.zxing.common.PerspectiveTransform;
|
||||
import com.google.zxing.common.detector.MathUtils;
|
||||
import com.google.zxing.qrcode.decoder.Version;
|
||||
|
||||
import java.util.Map;
|
||||
|
@ -197,8 +198,8 @@ public class Detector {
|
|||
ResultPoint topRight,
|
||||
ResultPoint bottomLeft,
|
||||
float moduleSize) throws NotFoundException {
|
||||
int tltrCentersDimension = round(ResultPoint.distance(topLeft, topRight) / moduleSize);
|
||||
int tlblCentersDimension = round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
|
||||
int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
|
||||
int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
|
||||
int dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7;
|
||||
switch (dimension & 0x03) { // mod 4
|
||||
case 0:
|
||||
|
@ -328,9 +329,7 @@ public class Detector {
|
|||
// color, advance to next state or end if we are in state 2 already
|
||||
if ((state == 1) == image.get(realX, realY)) {
|
||||
if (state == 2) {
|
||||
int diffX = x - fromX;
|
||||
int diffY = y - fromY;
|
||||
return (float) Math.sqrt((double) (diffX * diffX + diffY * diffY));
|
||||
return MathUtils.distance(x, y, fromX, fromY);
|
||||
}
|
||||
state++;
|
||||
}
|
||||
|
@ -348,9 +347,7 @@ public class Detector {
|
|||
// is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a
|
||||
// small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this.
|
||||
if (state == 2) {
|
||||
int diffX = toX + xstep - fromX;
|
||||
int diffY = toY - fromY;
|
||||
return (float) Math.sqrt((double) (diffX * diffX + diffY * diffY));
|
||||
return MathUtils.distance(toX + xstep, toY, fromX, fromY);
|
||||
}
|
||||
// else we didn't find even black-white-black; no estimate is really possible
|
||||
return Float.NaN;
|
||||
|
@ -399,11 +396,4 @@ public class Detector {
|
|||
return alignmentFinder.find();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue