mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
C++ changes for r2294
git-svn-id: https://zxing.googlecode.com/svn/trunk@2332 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
7890d40026
commit
afb81b1625
|
@ -1,3 +1,4 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* ResultPoint.cpp
|
||||
* zxing
|
||||
|
@ -19,7 +20,9 @@
|
|||
*/
|
||||
|
||||
#include <zxing/ResultPoint.h>
|
||||
#include <math.h>
|
||||
#include <zxing/common/detector/math_utils.h>
|
||||
|
||||
namespace math_utils = zxing::common::detector::math_utils;
|
||||
|
||||
namespace zxing {
|
||||
|
||||
|
@ -82,8 +85,11 @@ void ResultPoint::orderBestPatterns(std::vector<Ref<ResultPoint> > &patterns) {
|
|||
patterns[2] = pointC;
|
||||
}
|
||||
|
||||
float ResultPoint::distance(Ref<ResultPoint> point1, Ref<ResultPoint> point2) {
|
||||
return distance(point1->getX(), point1->getY(), point2->getX(), point2->getY());
|
||||
float ResultPoint::distance(Ref<ResultPoint> pattern1, Ref<ResultPoint> pattern2) {
|
||||
return math_utils::distance(pattern1->posX_,
|
||||
pattern1->posY_,
|
||||
pattern2->posX_,
|
||||
pattern2->posY_);
|
||||
}
|
||||
|
||||
float ResultPoint::distance(float x1, float x2, float y1, float y2) {
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
#include <zxing/common/reedsolomon/ReedSolomonException.h>
|
||||
#include <zxing/common/reedsolomon/GenericGF.h>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <zxing/common/detector/math_utils.h>
|
||||
#include <zxing/NotFoundException.h>
|
||||
|
||||
using zxing::aztec::Detector;
|
||||
|
@ -36,6 +36,7 @@ using zxing::Ref;
|
|||
using zxing::ResultPoint;
|
||||
using zxing::BitArray;
|
||||
using zxing::BitMatrix;
|
||||
namespace math_utils = zxing::common::detector::math_utils;
|
||||
|
||||
Detector::Detector(Ref<BitMatrix> image):
|
||||
image_(image),
|
||||
|
@ -136,21 +137,21 @@ std::vector<Ref<ResultPoint> > Detector::getMatrixCornerPoints(std::vector<Ref<P
|
|||
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 = math_utils::round(bullEyeCornerPoints[2]->x - ratio * dx);
|
||||
int targetcy = math_utils::round(bullEyeCornerPoints[2]->y - ratio * dy);
|
||||
|
||||
int targetax = ROUND(bullEyeCornerPoints[0]->x + ratio * dx);
|
||||
int targetay = ROUND(bullEyeCornerPoints[0]->y + ratio * dy);
|
||||
int targetax = math_utils::round(bullEyeCornerPoints[0]->x + ratio * dx);
|
||||
int targetay = math_utils::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 = math_utils::round(bullEyeCornerPoints[3]->x - ratio * dx);
|
||||
int targetdy = math_utils::round(bullEyeCornerPoints[3]->y - ratio * dy);
|
||||
int targetbx = math_utils::round(bullEyeCornerPoints[1]->x + ratio * dx);
|
||||
int targetby = math_utils::round(bullEyeCornerPoints[1]->y + ratio * dy);
|
||||
|
||||
if (!isValid(targetax, targetay) ||
|
||||
!isValid(targetbx, targetby) ||
|
||||
|
@ -262,18 +263,18 @@ std::vector<Ref<Point> > Detector::getBullEyeCornerPoints(Ref<zxing::aztec::Poin
|
|||
int dx = pina->x - pind->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 = math_utils::round(pinc->x - ratio * dx);
|
||||
int targetcy = math_utils::round(pinc->y - ratio * dy);
|
||||
int targetax = math_utils::round(pina->x + ratio * dx);
|
||||
int targetay = math_utils::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 = math_utils::round(pind->x - ratio * dx);
|
||||
int targetdy = math_utils::round(pind->y - ratio * dy);
|
||||
int targetbx = math_utils::round(pinb->x + ratio * dx);
|
||||
int targetby = math_utils::round(pinb->y + ratio * dy);
|
||||
|
||||
if (!isValid(targetax, targetay) ||
|
||||
!isValid(targetbx, targetby) ||
|
||||
|
@ -314,8 +315,8 @@ Ref<Point> Detector::getMatrixCenter() {
|
|||
|
||||
}
|
||||
|
||||
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 = math_utils::round((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4);
|
||||
int cy = math_utils::round((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4);
|
||||
|
||||
try {
|
||||
|
||||
|
@ -334,8 +335,8 @@ Ref<Point> Detector::getMatrixCenter() {
|
|||
|
||||
}
|
||||
|
||||
cx = ROUND((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4);
|
||||
cy = ROUND((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4);
|
||||
cx = math_utils::round((pointA->getX() + pointD->getX() + pointB->getX() + pointC->getX()) / 4);
|
||||
cy = math_utils::round((pointA->getY() + pointD->getY() + pointB->getY() + pointC->getY()) / 4);
|
||||
|
||||
return Ref<Point>(new Point(cx, cy));
|
||||
|
||||
|
@ -424,7 +425,7 @@ Ref<BitArray> Detector::sampleLine(Ref<zxing::aztec::Point> p1, Ref<zxing::aztec
|
|||
float py = p1->y;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (image_->get(ROUND(px), ROUND(py))) res->set(i);
|
||||
if (image_->get(math_utils::round(px), math_utils::round(py))) res->set(i);
|
||||
px += dx;
|
||||
py += dy;
|
||||
}
|
||||
|
@ -486,7 +487,7 @@ int Detector::getColor(Ref<zxing::aztec::Point> p1, Ref<zxing::aztec::Point> p2)
|
|||
for (int i = 0; i < d; i++) {
|
||||
px += dx;
|
||||
py += dy;
|
||||
if (image_->get(ROUND(px), ROUND(py)) != colorModel) {
|
||||
if (image_->get(math_utils::round(px), math_utils::round(py)) != colorModel) {
|
||||
error ++;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,8 +28,6 @@
|
|||
#include <zxing/DecodeHints.h>
|
||||
#include <zxing/aztec/AztecDetectorResult.h>
|
||||
|
||||
#define ROUND(a) ((int)(a + 0.5f))
|
||||
|
||||
namespace zxing {
|
||||
namespace aztec {
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
/*
|
||||
* WhiteRectangleDetector.cpp
|
||||
* y_wmk
|
||||
|
@ -20,9 +21,11 @@
|
|||
|
||||
#include <zxing/NotFoundException.h>
|
||||
#include <zxing/common/detector/WhiteRectangleDetector.h>
|
||||
#include <math.h>
|
||||
#include <zxing/common/detector/math_utils.h>
|
||||
#include <sstream>
|
||||
|
||||
namespace math_utils = zxing::common::detector::math_utils;
|
||||
|
||||
namespace zxing {
|
||||
using namespace std;
|
||||
|
||||
|
@ -222,21 +225,14 @@ std::vector<Ref<ResultPoint> > WhiteRectangleDetector::detect() {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends up being a bit faster than Math.round(). This merely rounds its
|
||||
* argument to the nearest int, where x.5 rounds up.
|
||||
*/
|
||||
int WhiteRectangleDetector::round(float d) {
|
||||
return (int) (d + 0.5f);
|
||||
}
|
||||
|
||||
Ref<ResultPoint> WhiteRectangleDetector::getBlackPointOnSegment(float aX, float aY, float bX, float bY) {
|
||||
int dist = distanceL2(aX, aY, bX, bY);
|
||||
int dist = math_utils::round(math_utils::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 = math_utils::round(aX + i * xStep);
|
||||
int y = math_utils::round(aY + i * yStep);
|
||||
if (image_->get(x, y)) {
|
||||
Ref<ResultPoint> point(new ResultPoint(x, y));
|
||||
return point;
|
||||
|
@ -246,12 +242,6 @@ Ref<ResultPoint> WhiteRectangleDetector::getBlackPointOnSegment(float aX, float
|
|||
return point;
|
||||
}
|
||||
|
||||
int WhiteRectangleDetector::distanceL2(float aX, float aY, float bX, float bY) {
|
||||
float xDiff = aX - bX;
|
||||
float yDiff = aY - bY;
|
||||
return round((float)sqrt(xDiff * xDiff + yDiff * yDiff));
|
||||
}
|
||||
|
||||
/**
|
||||
* recenters the points of a constant distance towards the center
|
||||
*
|
||||
|
|
|
@ -49,9 +49,7 @@ class WhiteRectangleDetector : public Counted {
|
|||
std::vector<Ref<ResultPoint> > detect();
|
||||
|
||||
private:
|
||||
int round(float a);
|
||||
Ref<ResultPoint> getBlackPointOnSegment(float aX, float aY, float bX, float bY);
|
||||
int distanceL2(float aX, float aY, float bX, float bY);
|
||||
std::vector<Ref<ResultPoint> > centerEdges(Ref<ResultPoint> y, Ref<ResultPoint> z,
|
||||
Ref<ResultPoint> x, Ref<ResultPoint> t);
|
||||
bool containsBlackPoint(int a, int b, int fixed, bool horizontal);
|
||||
|
|
46
cpp/core/src/zxing/common/detector/math_utils.h
Normal file
46
cpp/core/src/zxing/common/detector/math_utils.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
||||
#ifndef __ZXING_COMMON_DETECTOR_MATHUTILS_H__
|
||||
#define __ZXING_COMMON_DETECTOR_MATHUTILS_H__
|
||||
/*
|
||||
* Copyright 2012 ZXing authors All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
namespace zxing { namespace common { namespace detector { namespace math_utils {
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
inline int round(float d) {
|
||||
return (int) (d + 0.5f);
|
||||
}
|
||||
|
||||
inline float distance(float aX, float aY, float bX, float bY) {
|
||||
float xDiff = aX - bX;
|
||||
float yDiff = aY - bY;
|
||||
return (float) sqrt(xDiff * xDiff + yDiff * yDiff);
|
||||
}
|
||||
|
||||
inline float distance(int aX, int aY, int bX, int bY) {
|
||||
int xDiff = aX - bX;
|
||||
int yDiff = aY - bY;
|
||||
return (float) sqrt(xDiff * xDiff + yDiff * yDiff);
|
||||
}
|
||||
|
||||
}}}}
|
||||
|
||||
#endif
|
|
@ -22,10 +22,12 @@
|
|||
#include <zxing/ResultPoint.h>
|
||||
#include <zxing/common/GridSampler.h>
|
||||
#include <zxing/datamatrix/detector/Detector.h>
|
||||
#include <cmath>
|
||||
#include <zxing/common/detector/math_utils.h>
|
||||
#include <sstream>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace math_utils = zxing::common::detector::math_utils;
|
||||
|
||||
namespace zxing {
|
||||
namespace datamatrix {
|
||||
|
||||
|
@ -327,12 +329,8 @@ bool Detector::isValid(Ref<ResultPoint> p) {
|
|||
&& p->getY() < image_->getHeight();
|
||||
}
|
||||
|
||||
// L2 distance
|
||||
int Detector::distance(Ref<ResultPoint> a, Ref<ResultPoint> b) {
|
||||
return round(
|
||||
(float) sqrt(
|
||||
(double) (a->getX() - b->getX()) * (a->getX() - b->getX())
|
||||
+ (a->getY() - b->getY()) * (a->getY() - b->getY())));
|
||||
return math_utils::round(ResultPoint::distance(a, b));
|
||||
}
|
||||
|
||||
Ref<ResultPointsAndTransitions> Detector::transitionsBetween(Ref<ResultPoint> from,
|
||||
|
|
|
@ -27,10 +27,12 @@
|
|||
#include <zxing/qrcode/Version.h>
|
||||
#include <zxing/common/GridSampler.h>
|
||||
#include <zxing/DecodeHints.h>
|
||||
#include <cmath>
|
||||
#include <zxing/common/detector/math_utils.h>
|
||||
#include <sstream>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace math_utils = zxing::common::detector::math_utils;
|
||||
|
||||
namespace zxing {
|
||||
namespace qrcode {
|
||||
|
||||
|
@ -145,8 +147,10 @@ Ref<BitMatrix> Detector::sampleGrid(Ref<BitMatrix> image, int dimension, Ref<Per
|
|||
|
||||
int Detector::computeDimension(Ref<ResultPoint> topLeft, Ref<ResultPoint> topRight, Ref<ResultPoint> bottomLeft,
|
||||
float moduleSize) {
|
||||
int tltrCentersDimension = int(FinderPatternFinder::distance(topLeft, topRight) / moduleSize + 0.5f);
|
||||
int tlblCentersDimension = int(FinderPatternFinder::distance(topLeft, bottomLeft) / moduleSize + 0.5f);
|
||||
int tltrCentersDimension =
|
||||
math_utils::round(ResultPoint::distance(topLeft, topRight) / moduleSize);
|
||||
int tlblCentersDimension =
|
||||
math_utils::round(ResultPoint::distance(topLeft, bottomLeft) / moduleSize);
|
||||
int dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7;
|
||||
switch (dimension & 0x03) { // mod 4
|
||||
case 0:
|
||||
|
@ -247,9 +251,7 @@ float Detector::sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY)
|
|||
// Does current pixel mean we have moved white to black or vice versa?
|
||||
if (!((state == 1) ^ image_->get(realX, realY))) {
|
||||
if (state == 2) {
|
||||
int diffX = x - fromX;
|
||||
int diffY = y - fromY;
|
||||
return (float) sqrt((double) (diffX * diffX + diffY * diffY));
|
||||
return math_utils::distance(x, y, fromX, fromY);
|
||||
}
|
||||
state++;
|
||||
}
|
||||
|
@ -267,9 +269,7 @@ float Detector::sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY)
|
|||
// 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) sqrt((double) (diffX * diffX + diffY * diffY));
|
||||
return math_utils::distance(toX + xstep, toY, fromX, fromY);
|
||||
}
|
||||
// else we didn't find even black-white-black; no estimate is really possible
|
||||
return NAN;
|
||||
|
|
Loading…
Reference in a new issue