Skip to content

Commit 99fa153

Browse files
committed
尝试加入zbar作为补充
1 parent c5ac095 commit 99fa153

28 files changed

Lines changed: 4094 additions & 16 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ buildscript {
2727

2828
}
2929
dependencies {
30-
classpath 'com.android.tools.build:gradle:3.5.0'
30+
classpath 'com.android.tools.build:gradle:3.5.1'
3131
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
3232
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
3333
// NOTE: Do not place your application dependencies here; they belong

czxing/libs/arm64-v8a/libiconv.so

918 KB
Binary file not shown.
154 KB
Binary file not shown.
878 KB
Binary file not shown.
102 KB
Binary file not shown.

czxing/src/main/cpp/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ set_target_properties(tegra_hal PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}
3333
#add_library(opencv_java4 SHARED IMPORTED)
3434
#set_target_properties(opencv_java4 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libopencv_java4.so)
3535

36+
# 导入zbar
37+
add_library(iconv SHARED IMPORTED)
38+
add_library(zbar_croe SHARED IMPORTED)
39+
set_target_properties(iconv PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libiconv.so)
40+
set_target_properties(zbar_croe PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libzbarjni.so)
41+
3642
find_library(
3743
log-lib
3844
log)
@@ -41,6 +47,9 @@ target_link_libraries(
4147
czxing
4248
ZXingCore
4349

50+
iconv
51+
zbar_croe
52+
4453
opencv_imgproc
4554
opencv_core
4655

czxing/src/main/cpp/ImageScheduler.cpp

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,42 @@ void ImageScheduler::preTreatMat(const FrameData &frameData) {
125125
if (cameraLight < 40) {
126126
return;
127127
}
128-
decodeGrayPixels(gray);
128+
// decodeGrayPixels(gray);
129+
decodeZBar(gray);
129130
} catch (const std::exception &e) {
130131
LOGE("preTreatMat error...");
131132
}
132133
}
133134

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+
134164
void ImageScheduler::decodeGrayPixels(const Mat &gray) {
135165
LOGE("start GrayPixels...");
136166

@@ -245,22 +275,22 @@ Result ImageScheduler::decodePixels(Mat mat) {
245275
int width = mat.cols;
246276
int height = mat.rows;
247277

248-
auto *pixels = new unsigned char[height * width];
249-
250-
int index = 0;
251-
for (int i = 0; i < height; ++i) {
252-
for (int j = 0; j < width; ++j) {
253-
pixels[index++] = mat.at<unsigned char>(i, j);
254-
}
255-
}
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+
// }
256286

257287
// Mat resultMat(height, width, CV_8UC1, pixels);
258288
// imwrite("/storage/emulated/0/scan/result.jpg", resultMat);
259289

260-
auto binImage = BinaryBitmapFromBytesC1(pixels, 0, 0, width, height);
290+
auto binImage = BinaryBitmapFromBytesC1(mat.data, 0, 0, width, height);
261291
Result result = reader->read(*binImage);
262292

263-
delete[]pixels;
293+
// delete[]pixels;
264294

265295
return result;
266296
} catch (const std::exception &e) {
@@ -302,3 +332,4 @@ Result ImageScheduler::readBitmap(jobject bitmap, int left, int top, int width,
302332
return reader->read(*binImage);
303333
}
304334

335+

czxing/src/main/cpp/ImageScheduler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
#include "JavaCallHelper.h"
1515
#include "QRCodeRecognizer.h"
1616
#include "safe_queue.h"
17+
#include "zbar/zbar.h"
1718

1819
using namespace cv;
1920
using namespace ZXing;
21+
using namespace zbar;
2022

2123
typedef struct FrameData {
2224
jbyte *bytes;
@@ -48,6 +50,8 @@ class ImageScheduler {
4850

4951
void decodeGrayPixels(const Mat& gray);
5052

53+
void decodeZBar(const Mat& gray);
54+
5155
void decodeThresholdPixels(const Mat& gray);
5256

5357
void decodeAdaptivePixels(const Mat& gray);

czxing/src/main/cpp/JNIUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <opencv2/core/types.hpp>
2525

2626
#define ZX_LOG_TAG "ZXing"
27-
//#define DEBUG
27+
#define DEBUG
2828

2929
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, ZX_LOG_TAG, __VA_ARGS__)
3030
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, ZX_LOG_TAG, __VA_ARGS__)
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
//------------------------------------------------------------------------
2+
// Copyright 2007-2010 (c) Jeff Brown <spadix@users.sourceforge.net>
3+
//
4+
// This file is part of the ZBar Bar Code Reader.
5+
//
6+
// The ZBar Bar Code Reader is free software; you can redistribute it
7+
// and/or modify it under the terms of the GNU Lesser Public License as
8+
// published by the Free Software Foundation; either version 2.1 of
9+
// the License, or (at your option) any later version.
10+
//
11+
// The ZBar Bar Code Reader is distributed in the hope that it will be
12+
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13+
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU Lesser Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Lesser Public License
17+
// along with the ZBar Bar Code Reader; if not, write to the Free
18+
// Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19+
// Boston, MA 02110-1301 USA
20+
//
21+
// http://sourceforge.net/projects/zbar
22+
//------------------------------------------------------------------------
23+
#ifndef _ZBAR_DECODER_H_
24+
#define _ZBAR_DECODER_H_
25+
26+
/// @file
27+
/// Decoder C++ wrapper
28+
29+
#ifndef _ZBAR_H_
30+
# error "include zbar.h in your application, **not** zbar/Decoder.h"
31+
#endif
32+
33+
#include <string>
34+
35+
namespace zbar {
36+
37+
/// low-level bar width stream decoder interface.
38+
/// identifies symbols and extracts encoded data
39+
40+
class Decoder {
41+
public:
42+
43+
/// Decoder result handler.
44+
/// applications should subtype this and pass an instance to
45+
/// set_handler() to implement result processing
46+
class Handler {
47+
public:
48+
virtual ~Handler() { }
49+
50+
/// invoked by the Decoder as decode results become available.
51+
virtual void decode_callback(Decoder &decoder) = 0;
52+
};
53+
54+
/// constructor.
55+
Decoder ()
56+
: _handler(NULL)
57+
{
58+
_decoder = zbar_decoder_create();
59+
}
60+
61+
~Decoder ()
62+
{
63+
zbar_decoder_destroy(_decoder);
64+
}
65+
66+
/// clear all decoder state.
67+
/// see zbar_decoder_reset()
68+
void reset ()
69+
{
70+
zbar_decoder_reset(_decoder);
71+
}
72+
73+
/// mark start of a new scan pass.
74+
/// see zbar_decoder_new_scan()
75+
void new_scan ()
76+
{
77+
zbar_decoder_new_scan(_decoder);
78+
}
79+
80+
/// process next bar/space width from input stream.
81+
/// see zbar_decode_width()
82+
zbar_symbol_type_t decode_width (unsigned width)
83+
{
84+
return(zbar_decode_width(_decoder, width));
85+
}
86+
87+
/// process next bar/space width from input stream.
88+
/// see zbar_decode_width()
89+
Decoder& operator<< (unsigned width)
90+
{
91+
zbar_decode_width(_decoder, width);
92+
return(*this);
93+
}
94+
95+
/// retrieve color of @em next element passed to Decoder.
96+
/// see zbar_decoder_get_color()
97+
zbar_color_t get_color () const
98+
{
99+
return(zbar_decoder_get_color(_decoder));
100+
}
101+
102+
/// retrieve last decoded symbol type.
103+
/// see zbar_decoder_get_type()
104+
zbar_symbol_type_t get_type () const
105+
{
106+
return(zbar_decoder_get_type(_decoder));
107+
}
108+
109+
/// retrieve string name of last decoded symbol type.
110+
/// see zbar_get_symbol_name()
111+
const char *get_symbol_name () const
112+
{
113+
return(zbar_get_symbol_name(zbar_decoder_get_type(_decoder)));
114+
}
115+
116+
/// retrieve string name for last decode addon.
117+
/// see zbar_get_addon_name()
118+
/// @deprecated in 0.11
119+
const char *get_addon_name () const
120+
{
121+
return(zbar_get_addon_name(zbar_decoder_get_type(_decoder)));
122+
}
123+
124+
/// retrieve last decoded data in ASCII format as a char array.
125+
/// see zbar_decoder_get_data()
126+
const char *get_data_chars() const
127+
{
128+
return(zbar_decoder_get_data(_decoder));
129+
}
130+
131+
/// retrieve last decoded data as a std::string.
132+
/// see zbar_decoder_get_data()
133+
const std::string get_data_string() const
134+
{
135+
return(std::string(zbar_decoder_get_data(_decoder),
136+
zbar_decoder_get_data_length(_decoder)));
137+
}
138+
139+
/// retrieve last decoded data as a std::string.
140+
/// see zbar_decoder_get_data()
141+
const std::string get_data() const
142+
{
143+
return(get_data_string());
144+
}
145+
146+
/// retrieve length of decoded binary data.
147+
/// see zbar_decoder_get_data_length()
148+
int get_data_length() const
149+
{
150+
return(zbar_decoder_get_data_length(_decoder));
151+
}
152+
153+
/// retrieve last decode direction.
154+
/// see zbar_decoder_get_direction()
155+
/// @since 0.11
156+
int get_direction() const
157+
{
158+
return(zbar_decoder_get_direction(_decoder));
159+
}
160+
161+
/// setup callback to handle result data.
162+
void set_handler (Handler &handler)
163+
{
164+
_handler = &handler;
165+
zbar_decoder_set_handler(_decoder, _cb);
166+
zbar_decoder_set_userdata(_decoder, this);
167+
}
168+
169+
/// set config for indicated symbology (0 for all) to specified value.
170+
/// @see zbar_decoder_set_config()
171+
/// @since 0.4
172+
int set_config (zbar_symbol_type_t symbology,
173+
zbar_config_t config,
174+
int value)
175+
{
176+
return(zbar_decoder_set_config(_decoder, symbology, config, value));
177+
}
178+
179+
/// set config parsed from configuration string.
180+
/// @see zbar_decoder_parse_config()
181+
/// @since 0.4
182+
int set_config (std::string cfgstr)
183+
{
184+
return(zbar_decoder_parse_config(_decoder, cfgstr.c_str()));
185+
}
186+
187+
private:
188+
friend class Scanner;
189+
zbar_decoder_t *_decoder;
190+
Handler *_handler;
191+
192+
static void _cb (zbar_decoder_t *cdcode)
193+
{
194+
Decoder *dcode = (Decoder*)zbar_decoder_get_userdata(cdcode);
195+
if(dcode && dcode->_handler)
196+
dcode->_handler->decode_callback(*dcode);
197+
}
198+
};
199+
200+
}
201+
202+
#endif

0 commit comments

Comments
 (0)