Skip to content

Commit 11b19e5

Browse files
committed
将zbar加入到扫码流程
1 parent 99fa153 commit 11b19e5

2 files changed

Lines changed: 82 additions & 55 deletions

File tree

czxing/src/main/cpp/ImageScheduler.cpp

Lines changed: 79 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -125,42 +125,12 @@ void ImageScheduler::preTreatMat(const FrameData &frameData) {
125125
if (cameraLight < 40) {
126126
return;
127127
}
128-
// decodeGrayPixels(gray);
129-
decodeZBar(gray);
128+
decodeGrayPixels(gray);
130129
} catch (const std::exception &e) {
131130
LOGE("preTreatMat error...");
132131
}
133132
}
134133

135-
void ImageScheduler::decodeZBar(const Mat &gray) {
136-
int width = gray.cols;
137-
int height = gray.rows;
138-
139-
ImageScanner scanner;
140-
const void *raw = (&gray)->data;
141-
Image image(width, height, "Y800", raw, gray.cols * gray.rows);
142-
int n = scanner.scan(image);
143-
144-
// extract results
145-
if (image.symbol_begin() == image.symbol_end()) {
146-
LOGE("not find code");
147-
image.set_data(NULL, 0);
148-
return;
149-
}
150-
151-
Image::SymbolIterator symbol = image.symbol_begin();
152-
for (; symbol != image.symbol_end(); ++symbol) {
153-
LOGE("zbar Code Type = %s , Data = %s", symbol->get_type_name().c_str(),
154-
symbol->get_data().c_str());
155-
156-
Result result(DecodeStatus::NoError);
157-
result.setFormat(BarcodeFormat::QR_CODE);
158-
result.setText(ANSIToUnicode(symbol->get_data()));
159-
javaCallHelper->onResult(result);
160-
}
161-
image.set_data(NULL, 0);
162-
}
163-
164134
void ImageScheduler::decodeGrayPixels(const Mat &gray) {
165135
LOGE("start GrayPixels...");
166136

@@ -239,8 +209,42 @@ void ImageScheduler::decodeAdaptivePixels(const Mat &gray) {
239209
if (result.isValid()) {
240210
javaCallHelper->onResult(result);
241211
} else {
212+
// decodeZBar(gray);
213+
recognizerQrCode(gray);
214+
}
215+
}
216+
217+
void ImageScheduler::decodeZBar(const Mat &gray) {
218+
auto width = static_cast<unsigned int>(gray.cols);
219+
auto height = static_cast<unsigned int>(gray.rows);
220+
221+
const void *raw = gray.data;
222+
ImageScanner zbarScanner;
223+
zbarScanner.set_config(ZBAR_QRCODE, ZBAR_CFG_ENABLE, 1);
224+
225+
Image image(width, height, "Y800", raw, width * height);
226+
int n = zbarScanner.scan(image);
227+
228+
// extract results
229+
if (image.symbol_begin() == image.symbol_end()) {
230+
image.set_data(nullptr, 0);
242231
recognizerQrCode(gray);
232+
return;
243233
}
234+
235+
Image::SymbolIterator symbol = image.symbol_begin();
236+
for (; symbol != image.symbol_end(); ++symbol) {
237+
LOGE("zbar Code Type = %s , Data = %s", symbol->get_type_name().c_str(),
238+
symbol->get_data().c_str());
239+
240+
if (symbol->get_type() == zbar_symbol_type_e::ZBAR_QRCODE){
241+
Result result(DecodeStatus::NoError);
242+
result.setFormat(BarcodeFormat::QR_CODE);
243+
result.setText(ANSIToUnicode(symbol->get_data()));
244+
javaCallHelper->onResult(result);
245+
}
246+
}
247+
image.set_data(nullptr, 0);
244248
}
245249

246250
void ImageScheduler::recognizerQrCode(const Mat &mat) {
@@ -270,33 +274,18 @@ void ImageScheduler::recognizerQrCode(const Mat &mat) {
270274

271275
}
272276

273-
Result ImageScheduler::decodePixels(Mat mat) {
277+
Result ImageScheduler::decodePixels(const Mat& mat) {
274278
try {
275-
int width = mat.cols;
276-
int height = mat.rows;
277-
278-
// auto *pixels = new unsigned char[height * width];
279-
//
280-
// int index = 0;
281-
// for (int i = 0; i < height; ++i) {
282-
// for (int j = 0; j < width; ++j) {
283-
// pixels[index++] = mat.at<unsigned char>(i, j);
284-
// }
285-
// }
286-
287-
// Mat resultMat(height, width, CV_8UC1, pixels);
288-
// imwrite("/storage/emulated/0/scan/result.jpg", resultMat);
289-
290-
auto binImage = BinaryBitmapFromBytesC1(mat.data, 0, 0, width, height);
291-
Result result = reader->read(*binImage);
292-
293-
// delete[]pixels;
279+
// int width = mat.cols;
280+
// int height = mat.rows;
281+
// Mat resultMat(height, width, CV_8UC1, pixels);
282+
// imwrite("/storage/emulated/0/scan/result.jpg", resultMat);
294283

295-
return result;
284+
auto binImage = BinaryBitmapFromBytesC1(mat.data, 0, 0, mat.cols, mat.rows);
285+
return reader->read(*binImage);
296286
} catch (const std::exception &e) {
297287
ThrowJavaException(env, e.what());
298-
}
299-
catch (...) {
288+
} catch (...) {
300289
ThrowJavaException(env, "Unknown exception");
301290
}
302291

@@ -323,13 +312,49 @@ Result *ImageScheduler::analyzeResult() {
323312
return result;
324313
}
325314

315+
void ImageScheduler::decodeZBar(const jobject &gray) {
316+
// auto width = static_cast<unsigned int>(gray.cols);
317+
// auto height = static_cast<unsigned int>(gray.rows);
318+
//
319+
// const void *raw = gray.data;
320+
// Image image(width, height, "Y800", raw, width * height);
321+
// ImageScanner scanner;
322+
// int n = scanner.scan(image);
323+
//
324+
// // extract results
325+
// if (image.symbol_begin() == image.symbol_end()) {
326+
// image.set_data(nullptr, 0);
327+
// recognizerQrCode(gray);
328+
// return;
329+
// }
330+
//
331+
// Image::SymbolIterator symbol = image.symbol_begin();
332+
// for (; symbol != image.symbol_end(); ++symbol) {
333+
// LOGE("zbar Code Type = %s , Data = %s", symbol->get_type_name().c_str(),
334+
// symbol->get_data().c_str());
335+
//
336+
// if (symbol->get_type() == zbar_symbol_type_e::ZBAR_QRCODE){
337+
// Result result(DecodeStatus::NoError);
338+
// result.setFormat(BarcodeFormat::QR_CODE);
339+
// result.setText(ANSIToUnicode(symbol->get_data()));
340+
// javaCallHelper->onResult(result);
341+
// }
342+
// }
343+
// image.set_data(nullptr, 0);
344+
}
345+
326346
Result ImageScheduler::readBitmap(jobject bitmap, int left, int top, int width, int height) {
327347
auto binImage = BinaryBitmapFromJavaBitmap(env, bitmap, left, top, width, height);
328348
if (!binImage) {
329349
LOGE("create binary bitmap fail");
330350
return Result(DecodeStatus::NotFound);
331351
}
352+
// Result result = reader->read(*binImage);
353+
// if (!result.isValid()){
354+
//
355+
// }
332356
return reader->read(*binImage);
333357
}
334358

335359

360+

czxing/src/main/cpp/ImageScheduler.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class ImageScheduler {
5252

5353
void decodeZBar(const Mat& gray);
5454

55+
void decodeZBar(const jobject& bitmap);
56+
5557
void decodeThresholdPixels(const Mat& gray);
5658

5759
void decodeAdaptivePixels(const Mat& gray);
@@ -70,7 +72,7 @@ class ImageScheduler {
7072

7173
pthread_t prepareThread{};
7274

73-
Result decodePixels(Mat mat);
75+
Result decodePixels(const Mat& mat);
7476

7577
void recognizerQrCode(const Mat& mat);
7678

0 commit comments

Comments
 (0)