mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Some updates to the C++ test binary:
- calls MultiFormatReader directly - code cleanup, removing parts of the QR-specific framework - associated change to blackboxtest git-svn-id: https://zxing.googlecode.com/svn/trunk@1482 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
31b5627fee
commit
5632ab619f
|
@ -22,7 +22,7 @@ for format in $formats; do
|
||||||
tmp="${pic%JPG}";
|
tmp="${pic%JPG}";
|
||||||
txt="${tmp%jpg}txt";
|
txt="${tmp%jpg}txt";
|
||||||
expected=`cat "$txt"`;
|
expected=`cat "$txt"`;
|
||||||
actual=`build/zxing . $pic`;
|
actual=`build/zxing $pic`;
|
||||||
if [ "$expected" == "$actual" ]; then
|
if [ "$expected" == "$actual" ]; then
|
||||||
echo "passed."
|
echo "passed."
|
||||||
passed=$((passed+1));
|
passed=$((passed+1));
|
||||||
|
|
|
@ -25,9 +25,9 @@
|
||||||
#include <Magick++.h>
|
#include <Magick++.h>
|
||||||
#include "MagickBitmapSource.h"
|
#include "MagickBitmapSource.h"
|
||||||
#include <zxing/common/Counted.h>
|
#include <zxing/common/Counted.h>
|
||||||
#include <zxing/qrcode/QRCodeReader.h>
|
//#include <zxing/qrcode/QRCodeReader.h>
|
||||||
#include <zxing/Binarizer.h>
|
#include <zxing/Binarizer.h>
|
||||||
#include <zxing/oned/MultiFormatUPCEANReader.h>
|
#include <zxing/MultiFormatReader.h>
|
||||||
#include <zxing/Result.h>
|
#include <zxing/Result.h>
|
||||||
#include <zxing/ReaderException.h>
|
#include <zxing/ReaderException.h>
|
||||||
#include <zxing/common/GlobalHistogramBinarizer.h>
|
#include <zxing/common/GlobalHistogramBinarizer.h>
|
||||||
|
@ -37,122 +37,24 @@
|
||||||
#include <zxing/common/IllegalArgumentException.h>
|
#include <zxing/common/IllegalArgumentException.h>
|
||||||
#include <zxing/BinaryBitmap.h>
|
#include <zxing/BinaryBitmap.h>
|
||||||
|
|
||||||
#include <zxing/qrcode/detector/Detector.h>
|
//#include <zxing/qrcode/detector/Detector.h>
|
||||||
#include <zxing/qrcode/detector/QREdgeDetector.h>
|
//#include <zxing/qrcode/detector/QREdgeDetector.h>
|
||||||
#include <zxing/qrcode/decoder/Decoder.h>
|
//#include <zxing/qrcode/decoder/Decoder.h>
|
||||||
|
|
||||||
using namespace Magick;
|
using namespace Magick;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace zxing;
|
using namespace zxing;
|
||||||
using namespace zxing::qrcode;
|
//using namespace zxing::qrcode;
|
||||||
|
|
||||||
void draw_matrix(Image& image, Ref<BitMatrix> matrix) {
|
Ref<Result> decode(Ref<BinaryBitmap> image) {
|
||||||
int width = matrix->getWidth();
|
Ref<Reader> reader(new MultiFormatReader);
|
||||||
int height = matrix->getHeight();
|
return Ref<Result> (new Result(*reader->decode(image)));
|
||||||
// image.modifyImage();
|
|
||||||
// image.type(TrueColorType);
|
|
||||||
|
|
||||||
PixelPacket* pixels = image.getPixels(0, 0, width, height);
|
|
||||||
|
|
||||||
PixelPacket* pixel = pixels;
|
|
||||||
ColorMono color;
|
|
||||||
for (int y = 0; y < height; y++) {
|
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
color.mono(!matrix->get(x, y));
|
|
||||||
*pixel = color;
|
|
||||||
pixel++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
image.syncPixels();
|
|
||||||
}
|
|
||||||
|
|
||||||
void save_matrix(Ref<BitMatrix> matrix, string filename, float scale = 1.0) {
|
|
||||||
Image image(Geometry(matrix->getWidth(), matrix->getHeight()), Color(MaxRGB, MaxRGB, MaxRGB, 0));
|
|
||||||
int width = matrix->getWidth();
|
|
||||||
int height = matrix->getHeight();
|
|
||||||
draw_matrix(image, matrix);
|
|
||||||
image.scale(Geometry(width*scale, height*scale));
|
|
||||||
image.write(filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
void save_grid(Ref<BitMatrix> matrix, string filename, Ref<PerspectiveTransform> transform, int dimension) {
|
|
||||||
Image image(Geometry(matrix->getWidth(), matrix->getHeight()), Color(MaxRGB, MaxRGB, MaxRGB, 0));
|
|
||||||
|
|
||||||
draw_matrix(image, matrix);
|
|
||||||
|
|
||||||
image.strokeColor(Color(MaxRGB, 0, 0, MaxRGB / 3));
|
|
||||||
image.fillColor(Color(0, 0, 0, MaxRGB));
|
|
||||||
image.strokeWidth(1);
|
|
||||||
|
|
||||||
for (int i = 0; i <= dimension; i++) {
|
|
||||||
vector<float> tpoints(4);
|
|
||||||
|
|
||||||
tpoints[0] = 0;
|
|
||||||
tpoints[1] = i;
|
|
||||||
tpoints[2] = dimension;
|
|
||||||
tpoints[3] = i;
|
|
||||||
transform->transformPoints(tpoints);
|
|
||||||
|
|
||||||
DrawableLine line1(tpoints[0], tpoints[1], tpoints[2], tpoints[3]);
|
|
||||||
image.draw(line1);
|
|
||||||
|
|
||||||
tpoints[0] = i;
|
|
||||||
tpoints[1] = 0;
|
|
||||||
tpoints[2] = i;
|
|
||||||
tpoints[3] = dimension;
|
|
||||||
transform->transformPoints(tpoints);
|
|
||||||
|
|
||||||
DrawableLine line2(tpoints[0], tpoints[1], tpoints[2], tpoints[3]);
|
|
||||||
image.draw(line2);
|
|
||||||
}
|
|
||||||
|
|
||||||
image.write(filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ref<Result> decode2D(string out_prefix, Ref<BinaryBitmap> image, string& cell_grid, string& cell_transformed) {
|
|
||||||
|
|
||||||
Decoder decoder;
|
|
||||||
|
|
||||||
QREdgeDetector detector = QREdgeDetector(image->getBlackMatrix());
|
|
||||||
|
|
||||||
Ref<DetectorResult> detectorResult(detector.detect());
|
|
||||||
|
|
||||||
vector<Ref<ResultPoint> > points(detectorResult->getPoints());
|
|
||||||
|
|
||||||
Ref<DecoderResult> decoderResult(decoder.decode(detectorResult->getBits()));
|
|
||||||
|
|
||||||
Ref<Result> result(new Result(decoderResult->getText(),
|
|
||||||
decoderResult->getRawBytes(),
|
|
||||||
points,
|
|
||||||
BarcodeFormat_QR_CODE));
|
|
||||||
return result;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Ref<Result> decode1D(string out_prefix, Ref<BinaryBitmap> image, string& cell_grid, string& cell_transformed) {
|
|
||||||
|
|
||||||
|
|
||||||
Ref<Reader> reader(new oned::MultiFormatUPCEANReader);
|
|
||||||
Ref<Result> result(new Result(*reader->decode(image)));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO(flyashi): Call MultiFormatReader directly
|
|
||||||
Ref<Result> decode(string out_prefix, Ref<BinaryBitmap> image, string& cell_grid, string& cell_transformed) {
|
|
||||||
try {
|
|
||||||
return decode1D(out_prefix,image,cell_grid,cell_transformed);
|
|
||||||
} catch (ReaderException re) {
|
|
||||||
return decode2D(out_prefix,image,cell_grid,cell_transformed);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int test_image(Image& image, string out_prefix, bool localized) {
|
int test_image(Image& image, bool localized) {
|
||||||
string cell_mono;
|
|
||||||
string cell_transformed;
|
|
||||||
string cell_result;
|
string cell_result;
|
||||||
string cell_grid;
|
|
||||||
string result_color = "red";
|
|
||||||
int res = -1;
|
int res = -1;
|
||||||
|
|
||||||
Ref<BitMatrix> matrix(NULL);
|
Ref<BitMatrix> matrix(NULL);
|
||||||
|
@ -168,17 +70,9 @@ int test_image(Image& image, string out_prefix, bool localized) {
|
||||||
binarizer = new GlobalHistogramBinarizer(source);
|
binarizer = new GlobalHistogramBinarizer(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (out_prefix.size()) {
|
|
||||||
string monofile = out_prefix + ".mono.png";
|
|
||||||
matrix = binarizer->getBlackMatrix();
|
|
||||||
//save_matrix(matrix, monofile);
|
|
||||||
cell_mono = "<img src=\"" + monofile + "\" />";
|
|
||||||
}
|
|
||||||
|
|
||||||
Ref<BinaryBitmap> binary(new BinaryBitmap(binarizer));
|
Ref<BinaryBitmap> binary(new BinaryBitmap(binarizer));
|
||||||
Ref<Result> result(decode(out_prefix, binary, cell_grid, cell_transformed));
|
Ref<Result> result(decode(binary));
|
||||||
cell_result = result->getText()->getText();
|
cell_result = result->getText()->getText();
|
||||||
result_color = "green";
|
|
||||||
res = 0;
|
res = 0;
|
||||||
} catch (ReaderException e) {
|
} catch (ReaderException e) {
|
||||||
cell_result = "zxing::ReaderException: " + string(e.what());
|
cell_result = "zxing::ReaderException: " + string(e.what());
|
||||||
|
@ -195,25 +89,22 @@ int test_image(Image& image, string out_prefix, bool localized) {
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << cell_result;
|
cout << cell_result;
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_image_local(Image& image, string out_prefix) {
|
int test_image_local(Image& image) {
|
||||||
return test_image(image, out_prefix, true);
|
return test_image(image, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_image_global(Image& image, string out_prefix) {
|
int test_image_global(Image& image) {
|
||||||
return test_image(image, out_prefix, false);
|
return test_image(image, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
if (argc <= 2) {
|
if (argc <= 1) {
|
||||||
cout << "Usage: " << argv[0] << " [<outfolder> | \"-\"] <filename1> [<filename2> ...]" << endl;
|
cout << "Usage: " << argv[0] << " <filename1> [<filename2> ...]" << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
string outfolder = argv[1];
|
|
||||||
|
|
||||||
// int total = argc - 2;
|
// int total = argc - 2;
|
||||||
int gonly = 0;
|
int gonly = 0;
|
||||||
|
@ -221,7 +112,7 @@ int main(int argc, char** argv) {
|
||||||
int both = 0;
|
int both = 0;
|
||||||
int neither = 0;
|
int neither = 0;
|
||||||
|
|
||||||
for (int i = 2; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
string infilename = argv[i];
|
string infilename = argv[i];
|
||||||
// cerr << "Processing: " << infilename << endl;
|
// cerr << "Processing: " << infilename << endl;
|
||||||
Image image;
|
Image image;
|
||||||
|
@ -232,19 +123,11 @@ int main(int argc, char** argv) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int gresult = 1;
|
int gresult = 1;
|
||||||
int lresult = 1;
|
int lresult = 1;
|
||||||
|
|
||||||
if (outfolder == string("-")) {
|
gresult = test_image_global(image);
|
||||||
gresult = test_image_global(image, "");
|
// lresult = test_image_local(image);
|
||||||
// lresult = test_image_local(image, "");
|
|
||||||
} else {
|
|
||||||
replace(infilename.begin(), infilename.end(), '/', '_');
|
|
||||||
string prefix = string(outfolder) + string("/") + infilename;
|
|
||||||
gresult = test_image_global(image, prefix + ".g");
|
|
||||||
// lresult = test_image_local(image, prefix + ".l");
|
|
||||||
}
|
|
||||||
|
|
||||||
gresult = gresult == 0;
|
gresult = gresult == 0;
|
||||||
// lresult = lresult == 0;
|
// lresult = lresult == 0;
|
||||||
|
|
Loading…
Reference in a new issue