-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.cpp
60 lines (48 loc) · 1.91 KB
/
bridge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "bridge.h"
#include <cstring>
#include <zxing/ZXing.h>
#include <zxing/common/Array.h>
#include <zxing/common/Counted.h>
#include <zxing/common/GreyscaleLuminanceSource.h>
#include <zxing/common/HybridBinarizer.h>
#include <zxing/multi/GenericMultipleBarcodeReader.h>
#include <zxing/BinaryBitmap.h>
#include <zxing/DecodeHints.h>
#include <zxing/MultiFormatReader.h>
#include <zxing/ReaderException.h>
using namespace std;
using namespace zxing;
extern "C" {
int scan(bool tryhard, int stride, int npixels, char *pixels, int maxoutput, char *outputs[]) {
int height = npixels / stride;
// creates binary bitmap from our gray pixels
ArrayRef<char> pixelsRef(pixels, npixels);
Ref<GreyscaleLuminanceSource> lum(new GreyscaleLuminanceSource(pixelsRef, stride, height, 0, 0, stride, height));
Ref<HybridBinarizer> binarizer(new HybridBinarizer(lum));
Ref<BinaryBitmap> bmp(new BinaryBitmap(binarizer));
// actually try to scan for barcode.
MultiFormatReader reader;
Ref<multi::GenericMultipleBarcodeReader> multiReader(new multi::GenericMultipleBarcodeReader(reader));
try {
// if our image is at a good resolution, we do not need the TRYHARDER_HINT which can
// slowdown scanning by atleast an order of magnitude.
vector< Ref<Result> > results = multiReader->decodeMultiple(bmp,
tryhard ? DecodeHints::TRYHARDER_HINT : 0);
int idx = 0;
for (vector< Ref<Result> >::iterator it = results.begin();
it != results.end() && idx < maxoutput;
++it, ++idx) {
Ref<Result> result = *it;
const std::string stdStr = result->getText()->getText();
char *cstr = new char[stdStr.length()+1];
std::strcpy(cstr, stdStr.c_str());
outputs[idx] = cstr;
}
return idx;
} catch (const ReaderException& e) {
return 0;
} catch (const IllegalArgumentException e) {
return 0;
}
}
}