Skip to content

Commit 806e35b

Browse files
committed
完善zbar的识别流程
1 parent a9ca2a6 commit 806e35b

8 files changed

Lines changed: 83 additions & 93 deletions

File tree

17.5 MB
Binary file not shown.
9.41 MB
Binary file not shown.

czxing/src/main/cpp/CMakeLists.txt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ include_directories(include)
1515
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}")
1616

1717
# 静态方式导入库
18-
add_library(opencv_core STATIC IMPORTED)
19-
add_library(opencv_imgproc STATIC IMPORTED)
20-
21-
add_library(cpufeatures STATIC IMPORTED)
22-
add_library(tbb STATIC IMPORTED)
23-
add_library(tegra_hal STATIC IMPORTED)
18+
#add_library(opencv_core STATIC IMPORTED)
19+
#add_library(opencv_imgproc STATIC IMPORTED)
20+
#
21+
#add_library(cpufeatures STATIC IMPORTED)
22+
#add_library(tbb STATIC IMPORTED)
23+
#add_library(tegra_hal STATIC IMPORTED)
2424
#
2525
## 设置库路径
26-
set_target_properties(opencv_core PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../opencv/libs/${ANDROID_ABI}/libopencv_core.a)
27-
set_target_properties(opencv_imgproc PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../opencv/libs/${ANDROID_ABI}/libopencv_imgproc.a)
28-
29-
set_target_properties(cpufeatures PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../opencv/3rdparty/libs/${ANDROID_ABI}/libcpufeatures.a)
30-
set_target_properties(tbb PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../opencv/3rdparty/libs/${ANDROID_ABI}/libtbb.a)
31-
set_target_properties(tegra_hal PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../opencv/3rdparty/libs/${ANDROID_ABI}/libtegra_hal.a)
26+
#set_target_properties(opencv_core PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../opencv/libs/${ANDROID_ABI}/libopencv_core.a)
27+
#set_target_properties(opencv_imgproc PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../opencv/libs/${ANDROID_ABI}/libopencv_imgproc.a)
28+
#
29+
#set_target_properties(cpufeatures PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../opencv/3rdparty/libs/${ANDROID_ABI}/libcpufeatures.a)
30+
#set_target_properties(tbb PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../opencv/3rdparty/libs/${ANDROID_ABI}/libtbb.a)
31+
#set_target_properties(tegra_hal PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../opencv/3rdparty/libs/${ANDROID_ABI}/libtegra_hal.a)
3232

33-
#add_library(opencv_java4 SHARED IMPORTED)
34-
#set_target_properties(opencv_java4 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libopencv_java4.so)
33+
add_library(opencv_java4 SHARED IMPORTED)
34+
set_target_properties(opencv_java4 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libopencv_java4.so)
3535

3636
# 导入zbar
3737
#add_library(iconv SHARED IMPORTED)
@@ -50,13 +50,13 @@ target_link_libraries(
5050
# iconv
5151
zbar_croe
5252

53-
opencv_imgproc
54-
opencv_core
53+
# opencv_imgproc
54+
# opencv_core
55+
# cpufeatures
56+
# tbb
57+
# tegra_hal
5558

56-
cpufeatures
57-
tbb
58-
tegra_hal
59-
# opencv_java4
59+
opencv_java4
6060

6161
jnigraphics
6262
android

czxing/src/main/cpp/ImageScheduler.cpp

Lines changed: 25 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -144,32 +144,7 @@ void ImageScheduler::decodeGrayPixels(const Mat &gray) {
144144

145145
if (result.isValid()) {
146146
javaCallHelper->onResult(result);
147-
}
148-
// else if (result.isNeedScale()) {
149-
// LOGE("is need scale image...");
150-
// std::vector<ResultPoint> points = result.resultPoints();
151-
// ResultPoint topLeft = points[1];
152-
// ResultPoint topRight = points[2];
153-
// ResultPoint bottomLeft = points[0];
154-
//
155-
// int left = static_cast<int>(topLeft.x()) - 3 * 20;
156-
// int top = static_cast<int>(topLeft.y()) - 3 * 20;
157-
// int width = static_cast<int>(topRight.x() - topLeft.x()) + 3 * 25;
158-
// int height = static_cast<int>(bottomLeft.y() - topLeft.y()) + 3 * 25;
159-
//
160-
// LOGE("left = %d, top = %d, width = %d, height = %d", left, top, width, height);
161-
//
162-
// mat = mat(Rect(left, top, width, height));
163-
// imwrite("/storage/emulated/0/scan/scale.jpg", mat);
164-
// Result result1 = decodePixels(mat);
165-
//
166-
// if (result.isValid()) {
167-
// javaCallHelper->onResult(result);
168-
// } else {
169-
// decodeThresholdPixels(gray);
170-
// }
171-
// }
172-
else {
147+
}else {
173148
decodeThresholdPixels(gray);
174149
}
175150
}
@@ -225,7 +200,6 @@ void ImageScheduler::decodeZBar(const Mat &gray) {
225200
Image image(width, height, "Y800", raw, width * height);
226201
int n = zbarScanner->scan(image);
227202

228-
// extract results
229203
if (n > 0) {
230204
Image::SymbolIterator symbol = image.symbol_begin();
231205
LOGE("zbar Code Data = %s", symbol->get_data().c_str());
@@ -267,7 +241,6 @@ void ImageScheduler::recognizerQrCode(const Mat &mat) {
267241
javaCallHelper->onResult(result);
268242

269243
LOGE("end recognizerQrCode...");
270-
271244
}
272245

273246
Result ImageScheduler::decodePixels(const Mat &mat) {
@@ -308,47 +281,36 @@ Result *ImageScheduler::analyzeResult() {
308281
return result;
309282
}
310283

311-
void ImageScheduler::decodeZBar(const jobject &gray) {
312-
// auto width = static_cast<unsigned int>(gray.cols);
313-
// auto height = static_cast<unsigned int>(gray.rows);
314-
//
315-
// const void *raw = gray.data;
316-
// Image image(width, height, "Y800", raw, width * height);
317-
// ImageScanner scanner;
318-
// int n = scanner.scan(image);
319-
//
320-
// // extract results
321-
// if (image.symbol_begin() == image.symbol_end()) {
322-
// image.set_data(nullptr, 0);
323-
// recognizerQrCode(gray);
324-
// return;
325-
// }
326-
//
327-
// Image::SymbolIterator symbol = image.symbol_begin();
328-
// for (; symbol != image.symbol_end(); ++symbol) {
329-
// LOGE("zbar Code Type = %s , Data = %s", symbol->get_type_name().c_str(),
330-
// symbol->get_data().c_str());
331-
//
332-
// if (symbol->get_type() == zbar_symbol_type_e::ZBAR_QRCODE){
333-
// Result result(DecodeStatus::NoError);
334-
// result.setFormat(BarcodeFormat::QR_CODE);
335-
// result.setText(ANSIToUnicode(symbol->get_data()));
336-
// javaCallHelper->onResult(result);
337-
// }
338-
// }
339-
// image.set_data(nullptr, 0);
340-
}
341-
342284
Result ImageScheduler::readBitmap(jobject bitmap, int left, int top, int width, int height) {
285+
286+
Mat src;
287+
BitmapToMat(env, bitmap, src);
288+
289+
Mat gray;
290+
cvtColor(src, gray, COLOR_RGB2GRAY);
291+
292+
const void *raw = gray.data;
293+
Image image(gray.cols, gray.rows, "Y800", raw, gray.cols * gray.rows);
294+
if (zbarScanner->scan(image) > 0) {
295+
Image::SymbolIterator symbol = image.symbol_begin();
296+
LOGE("zbar Code Data = %s", symbol->get_data().c_str());
297+
if (symbol->get_type() == zbar_symbol_type_e::ZBAR_QRCODE) {
298+
Result resultBar(DecodeStatus::NoError);
299+
resultBar.setFormat(BarcodeFormat::QR_CODE);
300+
resultBar.setText(ANSIToUnicode(symbol->get_data()));
301+
image.set_data(nullptr, 0);
302+
return resultBar;
303+
}
304+
} else {
305+
image.set_data(nullptr, 0);
306+
}
307+
343308
auto binImage = BinaryBitmapFromJavaBitmap(env, bitmap, left, top, width, height);
344309
if (!binImage) {
345310
LOGE("create binary bitmap fail");
346311
return Result(DecodeStatus::NotFound);
347312
}
348-
// Result result = reader->read(*binImage);
349-
// if (!result.isValid()){
350-
//
351-
// }
313+
352314
return reader->read(*binImage);
353315
}
354316

czxing/src/main/cpp/ImageScheduler.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ class ImageScheduler {
5252

5353
void decodeZBar(const Mat& gray);
5454

55-
void decodeZBar(const jobject& bitmap);
56-
5755
void decodeThresholdPixels(const Mat& gray);
5856

5957
void decodeAdaptivePixels(const Mat& gray);

czxing/src/main/cpp/JNIUtils.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <stdexcept>
2222
#include <vector>
2323
#include <opencv2/core/types.hpp>
24+
#include <opencv2/core/mat.hpp>
25+
#include <opencv2/imgproc.hpp>
2426

2527
namespace {
2628

@@ -83,9 +85,8 @@ std::shared_ptr<ZXing::BinaryBitmap>
8385
BinaryBitmapFromBytesC4(JNIEnv *env, void *pixels, int cropLeft, int cropTop, int cropWidth,
8486
int cropHeight) {
8587
using namespace ZXing;
86-
87-
// LOGE("cropLeft %d , cropTop %d cropWidth %d cropHeight %d", cropLeft, cropTop, cropWidth,
88-
// cropHeight);
88+
LOGE("cropLeft %d , cropTop %d cropWidth %d cropHeight %d", cropLeft, cropTop, cropWidth,
89+
cropHeight);
8990

9091
std::shared_ptr<GenericLuminanceSource> luminance = std::make_shared<GenericLuminanceSource>(
9192
cropLeft, cropTop, cropWidth,
@@ -98,9 +99,8 @@ BinaryBitmapFromBytesC4(JNIEnv *env, void *pixels, int cropLeft, int cropTop, in
9899
std::shared_ptr<ZXing::BinaryBitmap>
99100
BinaryBitmapFromBytesC1(void *pixels, int left, int top, int width, int height) {
100101
using namespace ZXing;
101-
102-
// LOGE("cropLeft %d , cropTop %d cropWidth %d cropHeight %d", left, top, width,
103-
// height);
102+
LOGE("cropLeft %d , cropTop %d cropWidth %d cropHeight %d", left, top, width,
103+
height);
104104

105105
std::shared_ptr<GenericLuminanceSource> luminance = std::make_shared<GenericLuminanceSource>(
106106
left, top, width, height,
@@ -109,6 +109,30 @@ BinaryBitmapFromBytesC1(void *pixels, int left, int top, int width, int height)
109109
return std::make_shared<HybridBinarizer>(luminance);
110110
}
111111

112+
void
113+
BitmapToMat(JNIEnv *env, jobject bitmap, cv::Mat &mat) {
114+
AndroidBitmapInfo bmInfo;
115+
AndroidBitmap_getInfo(env, bitmap, &bmInfo);
116+
cv::Mat &dst = mat;
117+
118+
void *pixels = nullptr;
119+
if (AndroidBitmap_lockPixels(env, bitmap, &pixels) == ANDROID_BITMAP_RESUT_SUCCESS) {
120+
AutoUnlockPixels autounlock(env, bitmap);
121+
122+
if (bmInfo.format == ANDROID_BITMAP_FORMAT_RGBA_8888) {
123+
LOGE("nBitmapToMat: RGB_8888 -> CV_8UC4");
124+
cv::Mat tmp(bmInfo.height, bmInfo.width, CV_8UC4, pixels);
125+
tmp.copyTo(dst);
126+
} else {
127+
// info.format == ANDROID_BITMAP_FORMAT_RGB_565
128+
LOGE("nBitmapToMat: RGB_565 -> CV_8UC4");
129+
cv::Mat tmp(bmInfo.height, bmInfo.width, CV_8UC2, pixels);
130+
}
131+
} else {
132+
throw std::runtime_error("Failed to read bitmap's data");
133+
}
134+
}
135+
112136
/**
113137
* string转wstring
114138
*/
@@ -199,4 +223,5 @@ ToJavaArray(JNIEnv *env, const std::vector<ZXing::ResultPoint> &input) {
199223
}
200224

201225
return array;
202-
}
226+
}
227+

czxing/src/main/cpp/JNIUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <string>
2323
#include <src/ResultPoint.h>
2424
#include <opencv2/core/types.hpp>
25+
#include "zbar/zbar.h"
2526

2627
#define ZX_LOG_TAG "ZXing"
2728
#define DEBUG
@@ -56,6 +57,9 @@ BinaryBitmapFromBytesC4(JNIEnv *env, void *rgbScale, int cropLeft, int cropTop,
5657
std::shared_ptr<ZXing::BinaryBitmap>
5758
BinaryBitmapFromBytesC1(void *grayScale, int cropLeft, int cropTop, int cropWidth, int cropHeight);
5859

60+
void
61+
BitmapToMat(JNIEnv *env, jobject bitmap, cv::Mat &mat);
62+
5963
std::string UnicodeToANSI(const std::wstring &wstr);
6064

6165
std::wstring ANSIToUnicode(const std::string &src);

sample/src/main/java/me/sam/czxing/MainActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import me.devilsen.czxing.code.BarcodeFormat;
2929
import me.devilsen.czxing.code.BarcodeReader;
3030
import me.devilsen.czxing.code.CodeResult;
31+
import me.devilsen.czxing.util.SaveImageUtil;
3132
import me.devilsen.czxing.view.ScanActivityDelegate;
3233
import me.devilsen.czxing.view.ScanView;
3334

@@ -48,7 +49,7 @@ protected void onCreate(Bundle savedInstanceState) {
4849
}
4950

5051
public void scan(View view) {
51-
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test_boder_complex_7);
52+
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test_black_boder);
5253
CodeResult result = reader.read(bitmap);
5354

5455
if (result == null) {

0 commit comments

Comments
 (0)