MultiFinderPatternFinder.cpp Example File
appdemos/qtws/QZXing/zxing/zxing/multi/qrcode/detector/MultiFinderPatternFinder.cpp
#include <cmath>
#include <algorithm>
#include <zxing/multi/qrcode/detector/MultiFinderPatternFinder.h>
#include <zxing/DecodeHints.h>
#include <zxing/ReaderException.h>
using std::abs;
using std::min;
using std::sort;
using std::vector;
using zxing::Ref;
using zxing::BitMatrix;
using zxing::ReaderException;
using zxing::qrcode::FinderPattern;
using zxing::qrcode::FinderPatternInfo;
using zxing::multi::MultiFinderPatternFinder;
using zxing::BitMatrix;
using zxing::ResultPointCallback;
using zxing::DecodeHints;
const float MultiFinderPatternFinder::MAX_MODULE_COUNT_PER_EDGE = 180;
const float MultiFinderPatternFinder::MIN_MODULE_COUNT_PER_EDGE = 9;
const float MultiFinderPatternFinder::DIFF_MODSIZE_CUTOFF_PERCENT = 0.05f;
const float MultiFinderPatternFinder::DIFF_MODSIZE_CUTOFF = 0.5f;
namespace {
bool compareModuleSize(Ref<FinderPattern> a, Ref<FinderPattern> b){
float value = a->getEstimatedModuleSize() - b->getEstimatedModuleSize();
return value < 0.0;
}
}
MultiFinderPatternFinder::MultiFinderPatternFinder(Ref<BitMatrix> image,
Ref<ResultPointCallback> resultPointCallback)
: FinderPatternFinder(image, resultPointCallback)
{
}
MultiFinderPatternFinder::~MultiFinderPatternFinder(){}
vector<Ref<FinderPatternInfo> > MultiFinderPatternFinder::findMulti(DecodeHints const& hints){
bool tryHarder = hints.getTryHarder();
Ref<BitMatrix> image = image_;
int maxI = image->getHeight();
int maxJ = image->getWidth();
int iSkip = (int) (maxI / (MAX_MODULES * 4.0f) * 3);
if (iSkip < MIN_SKIP || tryHarder) {
iSkip = MIN_SKIP;
}
int stateCount[5];
for (int i = iSkip - 1; i < maxI; i += iSkip) {
stateCount[0] = 0;
stateCount[1] = 0;
stateCount[2] = 0;
stateCount[3] = 0;
stateCount[4] = 0;
int currentState = 0;
for (int j = 0; j < maxJ; j++) {
if (image->get(j, i)) {
if ((currentState & 1) == 1) {
currentState++;
}
stateCount[currentState]++;
} else {
if ((currentState & 1) == 0) {
if (currentState == 4) {
if (foundPatternCross(stateCount) && handlePossibleCenter(stateCount, i, j)) {
currentState = 0;
stateCount[0] = 0;
stateCount[1] = 0;
stateCount[2] = 0;
stateCount[3] = 0;
stateCount[4] = 0;
} else {
stateCount[0] = stateCount[2];
stateCount[1] = stateCount[3];
stateCount[2] = stateCount[4];
stateCount[3] = 1;
stateCount[4] = 0;
currentState = 3;
}
} else {
stateCount[++currentState]++;
}
} else {
stateCount[currentState]++;
}
}
}
if (foundPatternCross(stateCount)) {
handlePossibleCenter(stateCount, i, maxJ);
}
}
vector<vector<Ref<FinderPattern> > > patternInfo = selectBestPatterns();
vector<Ref<FinderPatternInfo> > result;
for (unsigned int i = 0; i < patternInfo.size(); i++) {
vector<Ref<FinderPattern> > pattern = patternInfo[i];
pattern = FinderPatternFinder::orderBestPatterns(pattern);
result.push_back(Ref<FinderPatternInfo>(new FinderPatternInfo(pattern)));
}
return result;
}
vector<vector<Ref<FinderPattern> > > MultiFinderPatternFinder::selectBestPatterns(){
vector<Ref<FinderPattern> > possibleCenters = possibleCenters_;
int size = possibleCenters.size();
if (size < 3) {
throw ReaderException("No code detected");
}
vector<vector<Ref<FinderPattern> > > results;
if (size == 3) {
results.push_back(possibleCenters_);
return results;
}
sort(possibleCenters.begin(), possibleCenters.end(), compareModuleSize);
for (int i1 = 0; i1 < (size - 2); i1++) {
Ref<FinderPattern> p1 = possibleCenters[i1];
for (int i2 = i1 + 1; i2 < (size - 1); i2++) {
Ref<FinderPattern> p2 = possibleCenters[i2];
float vModSize12 = (p1->getEstimatedModuleSize() - p2->getEstimatedModuleSize()) / min(p1->getEstimatedModuleSize(), p2->getEstimatedModuleSize());
float vModSize12A = abs(p1->getEstimatedModuleSize() - p2->getEstimatedModuleSize());
if (vModSize12A > DIFF_MODSIZE_CUTOFF && vModSize12 >= DIFF_MODSIZE_CUTOFF_PERCENT) {
break;
}
for (int i3 = i2 + 1; i3 < size; i3++) {
Ref<FinderPattern> p3 = possibleCenters[i3];
float vModSize23 = (p2->getEstimatedModuleSize() - p3->getEstimatedModuleSize()) / min(p2->getEstimatedModuleSize(), p3->getEstimatedModuleSize());
float vModSize23A = abs(p2->getEstimatedModuleSize() - p3->getEstimatedModuleSize());
if (vModSize23A > DIFF_MODSIZE_CUTOFF && vModSize23 >= DIFF_MODSIZE_CUTOFF_PERCENT) {
break;
}
vector<Ref<FinderPattern> > test;
test.push_back(p1);
test.push_back(p2);
test.push_back(p3);
test = FinderPatternFinder::orderBestPatterns(test);
Ref<FinderPatternInfo> info = Ref<FinderPatternInfo>(new FinderPatternInfo(test));
float dA = FinderPatternFinder::distance(info->getTopLeft(), info->getBottomLeft());
float dC = FinderPatternFinder::distance(info->getTopRight(), info->getBottomLeft());
float dB = FinderPatternFinder::distance(info->getTopLeft(), info->getTopRight());
float estimatedModuleCount = (dA + dB) / (p1->getEstimatedModuleSize() * 2.0f);
if (estimatedModuleCount > MAX_MODULE_COUNT_PER_EDGE || estimatedModuleCount < MIN_MODULE_COUNT_PER_EDGE) {
continue;
}
float vABBC = abs((dA - dB) / min(dA, dB));
if (vABBC >= 0.1f) {
continue;
}
float dCpy = (float) sqrt(dA * dA + dB * dB);
float vPyC = abs((dC - dCpy) / min(dC, dCpy));
if (vPyC >= 0.1f) {
continue;
}
results.push_back(test);
}
}
}
if (results.empty()){
throw ReaderException("No code detected");
}
return results;
}