Skip to content

Commit c3bc48a

Browse files
committed
加入一些API
1 parent a4cd5cd commit c3bc48a

17 files changed

Lines changed: 178 additions & 62 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.1'
30+
classpath 'com.android.tools.build:gradle:3.5.2'
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/src/main/cpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ include_directories(zxing)
1414
include_directories(include)
1515
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}")
1616

17+
add_compile_options(czxing "-O1" "-fPIC")
18+
1719
# 静态方式导入库
1820
add_library(opencv_core STATIC IMPORTED)
1921
add_library(opencv_imgproc STATIC IMPORTED)

czxing/src/main/cpp/ImageScheduler.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ImageScheduler::~ImageScheduler() {
3434
delete &cameraLight;
3535
delete &prepareThread;
3636
scanIndex = 0;
37+
decodeQr = 0;
3738
}
3839

3940
void *prepareMethod(void *arg) {
@@ -133,6 +134,13 @@ void ImageScheduler::preTreatMat(const FrameData &frameData) {
133134
return;
134135
}
135136

137+
// 不需要解析二维码
138+
if (!decodeQr) {
139+
decodeGrayPixels(gray);
140+
return;
141+
}
142+
143+
// 正常解析策略 偶数次zxing解析,奇数次zbar解析
136144
if (scanIndex % 2 == 0) {
137145
decodeGrayPixels(gray);
138146
} else {
@@ -167,6 +175,7 @@ void ImageScheduler::decodeZBar(const Mat &gray) {
167175
Image image(width, height, "Y800", raw, width * height);
168176
int n = zbarScanner->scan(image);
169177

178+
// 检测到二维码
170179
if (n > 0) {
171180
Image::SymbolIterator symbol = image.symbol_begin();
172181
LOGE("zbar GrayPixels Success Data = %s scanIndex = %d", symbol->get_data().c_str(),
@@ -233,6 +242,11 @@ void ImageScheduler::decodeAdaptivePixels(const Mat &gray) {
233242
}
234243

235244
void ImageScheduler::recognizerQrCode(const Mat &mat) {
245+
// 不需要解析二维码
246+
if (!decodeQr) {
247+
return;
248+
}
249+
236250
if (scanIndex % 7 == 0) {
237251
javaCallHelper->onFocus();
238252
return;
@@ -245,7 +259,7 @@ void ImageScheduler::recognizerQrCode(const Mat &mat) {
245259

246260
cv::Rect rect;
247261
qrCodeRecognizer->processData(mat, &rect);
248-
if (rect.empty()) {
262+
if (rect.empty() || rect.height < 110) {
249263
return;
250264
}
251265

@@ -263,7 +277,8 @@ void ImageScheduler::recognizerQrCode(const Mat &mat) {
263277

264278
javaCallHelper->onResult(result);
265279

266-
LOGE("end recognizerQrCode..., scanIndex = %d", scanIndex);
280+
LOGE("end recognizerQrCode..., scanIndex = %d height = %d width = %d", scanIndex, rect.height,
281+
rect.width);
267282
}
268283

269284
Result ImageScheduler::decodePixels(const Mat &mat) {
@@ -304,6 +319,10 @@ Result *ImageScheduler::analyzeResult() {
304319
return result;
305320
}
306321

322+
void ImageScheduler::isDecodeQrCode(bool decodeQrCode) {
323+
this->decodeQr = decodeQrCode;
324+
}
325+
307326
Result ImageScheduler::readBitmap(jobject bitmap, int left, int top, int width, int height) {
308327

309328
Mat src;

czxing/src/main/cpp/ImageScheduler.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,24 @@ class ImageScheduler {
4646

4747
void stop();
4848

49-
void preTreatMat(const FrameData& frameData);
49+
void preTreatMat(const FrameData &frameData);
5050

51-
void decodeGrayPixels(const Mat& gray);
51+
void decodeGrayPixels(const Mat &gray);
5252

53-
void decodeZBar(const Mat& gray);
53+
void decodeZBar(const Mat &gray);
5454

55-
void decodeThresholdPixels(const Mat& gray);
55+
void decodeThresholdPixels(const Mat &gray);
5656

57-
void decodeAdaptivePixels(const Mat& gray);
57+
void decodeAdaptivePixels(const Mat &gray);
5858

59-
Result readBitmap(jobject bitmap, int left, int top, int width,int height);
59+
Result readBitmap(jobject bitmap, int left, int top, int width, int height);
60+
61+
void isDecodeQrCode(bool decodeQrCode);
62+
63+
MultiFormatReader *reader;
6064

6165
private:
6266
JNIEnv *env;
63-
MultiFormatReader *reader;
6467
JavaCallHelper *javaCallHelper;
6568
std::atomic<bool> isProcessing{};
6669
std::atomic<bool> stopProcessing{};
@@ -69,16 +72,17 @@ class ImageScheduler {
6972
QRCodeRecognizer *qrCodeRecognizer;
7073
SafeQueue<FrameData> frameQueue;
7174
int scanIndex;
75+
bool decodeQr;
7276

7377
pthread_t prepareThread{};
7478

75-
Result decodePixels(const Mat& mat);
79+
Result decodePixels(const Mat &mat);
7680

77-
void recognizerQrCode(const Mat& mat);
81+
void recognizerQrCode(const Mat &mat);
7882

7983
Result *analyzeResult();
8084

81-
bool analysisBrightness(const Mat& gray);
85+
bool analysisBrightness(const Mat &gray);
8286

8387
};
8488

czxing/src/main/cpp/JavaCallHelper.cpp

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,12 @@
11
//
2-
// Created by Administrator on 2019/08/10 0010.
2+
// Created by dongSen on 2019/08/10 0010.
33
//
44

55
#include "JavaCallHelper.h"
66
#include "JNIUtils.h"
77

88
JavaCallHelper::JavaCallHelper(JavaVM *_javaVM, JNIEnv *_env, jobject &_jobj) : javaVM(_javaVM),
99
env(_env) {
10-
// 获取Class
11-
// jclass jSdkClass = env->FindClass("me/devilsen/czxing/code/NativeSdk");
12-
// if (jSdkClass == nullptr) {
13-
// LOGE("Unable to find class");
14-
// return;
15-
// }
16-
//
17-
// // 获取构造函数
18-
// jmethodID constructor = env->GetMethodID(jSdkClass, "<init>", "()V");
19-
// if (constructor == nullptr) {
20-
// LOGE("can't constructor jClass");
21-
// return;
22-
// }
23-
24-
// 获取对应函数
25-
// jSdkObject = env->NewObject(jSdkClass, constructor);
26-
// if (jSdkObject == nullptr) {
27-
// LOGE("can't new jobject");
28-
// return;
29-
// }
3010
jSdkObject = env->NewGlobalRef(_jobj);
3111

3212
jclass jSdkClass = env->GetObjectClass(jSdkObject);
@@ -46,19 +26,11 @@ JavaCallHelper::JavaCallHelper(JavaVM *_javaVM, JNIEnv *_env, jobject &_jobj) :
4626

4727
JavaCallHelper::~JavaCallHelper() {
4828
env->DeleteGlobalRef(jSdkObject);
49-
DELETE(javaVM);
50-
DELETE(env);
29+
// DELETE(javaVM);
30+
// DELETE(env);
5131
}
5232

5333
void JavaCallHelper::onResult(const ZXing::Result &result) {
54-
// if (result.format() == ZXing::BarcodeFormat::QR_CODE) {
55-
// if (result.resultPoints().size() < 2) {
56-
// return;
57-
// }
58-
// } else if (!result.isValid()) {
59-
// return;
60-
// }
61-
6234
if (!result.isValid() && !result.isBlurry()) {
6335
return;
6436
}

czxing/src/main/cpp/native-lib.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ JNIEXPORT jlong JNICALL
4747
Java_me_devilsen_czxing_code_NativeSdk_createInstance(JNIEnv *env, jobject instance,
4848
jintArray formats_) {
4949
try {
50+
if (javaCallHelper) {
51+
DELETE(javaCallHelper);
52+
}
5053
javaCallHelper = new JavaCallHelper(javaVM, env, instance);
5154

5255
ZXing::DecodeHints hints;
@@ -65,6 +68,29 @@ Java_me_devilsen_czxing_code_NativeSdk_createInstance(JNIEnv *env, jobject insta
6568
return 0;
6669
}
6770

71+
extern "C"
72+
JNIEXPORT void JNICALL
73+
Java_me_devilsen_czxing_code_NativeSdk_setFormat(JNIEnv *env, jobject thiz, jlong objPtr,
74+
jintArray formats_) {
75+
if (objPtr == 0) {
76+
return;
77+
}
78+
auto imageScheduler = reinterpret_cast<ImageScheduler *>(objPtr);
79+
ZXing::DecodeHints hints;
80+
bool decodeQr = true;
81+
if (formats_ != nullptr) {
82+
std::vector<ZXing::BarcodeFormat> formats = GetFormats(env, formats_);
83+
hints.setPossibleFormats(formats);
84+
85+
if (std::find(formats.begin(), formats.end(), ZXing::BarcodeFormat::QR_CODE) ==
86+
formats.end()) {
87+
decodeQr = false;
88+
}
89+
}
90+
imageScheduler->isDecodeQrCode(decodeQr);
91+
imageScheduler->reader->setFormat(hints);
92+
}
93+
6894
extern "C"
6995
JNIEXPORT void JNICALL
7096
Java_me_devilsen_czxing_code_NativeSdk_destroyInstance(JNIEnv *env, jobject instance,

czxing/src/main/cpp/zxing/src/MultiFormatReader.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ MultiFormatReader::MultiFormatReader(const DecodeHints& hints)
7979
}
8080
_readers.emplace_back(new QRCode::Reader(hints));
8181
_readers.emplace_back(new DataMatrix::Reader(hints));
82-
_readers.emplace_back(new Aztec::Reader());
83-
_readers.emplace_back(new Pdf417::Reader());
84-
_readers.emplace_back(new MaxiCode::Reader());
82+
// _readers.emplace_back(new Aztec::Reader());
83+
// _readers.emplace_back(new Pdf417::Reader());
84+
// _readers.emplace_back(new MaxiCode::Reader());
8585
if (tryHarder) {
8686
_readers.emplace_back(new OneD::Reader(hints));
8787
}
@@ -92,6 +92,12 @@ MultiFormatReader::~MultiFormatReader()
9292
{
9393
}
9494

95+
void MultiFormatReader::setFormat(const DecodeHints &hints)
96+
{
97+
_readers.clear();
98+
_readers.emplace_back(new OneD::Reader(hints));
99+
}
100+
95101
Result
96102
MultiFormatReader::read(const BinaryBitmap& image) const
97103
{
@@ -108,4 +114,6 @@ MultiFormatReader::read(const BinaryBitmap& image) const
108114
return Result(DecodeStatus::NotFound);
109115
}
110116

117+
118+
111119
} // ZXing

czxing/src/main/cpp/zxing/src/MultiFormatReader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class MultiFormatReader
4040
explicit MultiFormatReader(const DecodeHints& hints);
4141
~MultiFormatReader();
4242

43+
void setFormat(const DecodeHints& hints);
44+
4345
Result read(const BinaryBitmap& image) const;
4446

4547
private:

czxing/src/main/java/me/devilsen/czxing/ScanActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ private void initData() {
8989
scanBox.setBorderColor(option.getBorderColor());
9090
mScanView.setScanMode(option.getScanMode());
9191
scanBox.setScanLineColor(option.getScanLineColors());
92+
scanBox.setFlashLightOnText(option.getFlashLightOnText());
93+
scanBox.setFlashLightOffText(option.getFlashLightOffText());
94+
scanBox.setScanNoticeText(option.getScanNoticeText());
95+
96+
// mScanView.setBarcodeFormat(BarcodeFormat.EAN_13);
9297

9398
// 标题栏
9499
String title = option.getTitle();

czxing/src/main/java/me/devilsen/czxing/ScannerManager.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ public ScannerManager continuousScan() {
5656
return this;
5757
}
5858

59+
public ScannerManager setFlashLightOnText(String lightOnText) {
60+
scanOption.flashLightOnText = lightOnText;
61+
return this;
62+
}
63+
64+
public ScannerManager setFlashLightOffText(String lightOffText) {
65+
scanOption.flashLightOffText = lightOffText;
66+
return this;
67+
}
68+
69+
public ScannerManager setScanNoticeText(String scanNoticeText) {
70+
scanOption.scanNoticeText = scanNoticeText;
71+
return this;
72+
}
73+
5974
public ScannerManager setScanLineColors(List<Integer> scanLineColors) {
6075
scanOption.scanLineColors = scanLineColors;
6176
return this;
@@ -85,6 +100,11 @@ public static class ScanOption implements Parcelable {
85100
private String title;
86101
private boolean showAlbum = true;
87102
private boolean continuousScan;
103+
104+
private String flashLightOnText;
105+
private String flashLightOffText;
106+
private String scanNoticeText;
107+
88108
private List<Integer> scanLineColors;
89109

90110
public int getCornerColor() {
@@ -95,7 +115,7 @@ public int getBorderColor() {
95115
return borderColor;
96116
}
97117

98-
public int getScanMode(){
118+
public int getScanMode() {
99119
return scanMode;
100120
}
101121

@@ -107,10 +127,22 @@ public boolean isShowAlbum() {
107127
return showAlbum;
108128
}
109129

110-
public boolean isContinuousScan(){
130+
public boolean isContinuousScan() {
111131
return continuousScan;
112132
}
113133

134+
public String getFlashLightOnText() {
135+
return flashLightOnText;
136+
}
137+
138+
public String getFlashLightOffText() {
139+
return flashLightOffText;
140+
}
141+
142+
public String getScanNoticeText() {
143+
return scanNoticeText;
144+
}
145+
114146
public List<Integer> getScanLineColors() {
115147
return scanLineColors;
116148
}
@@ -128,6 +160,9 @@ public void writeToParcel(Parcel dest, int flags) {
128160
dest.writeString(this.title);
129161
dest.writeByte(this.showAlbum ? (byte) 1 : (byte) 0);
130162
dest.writeByte(this.continuousScan ? (byte) 1 : (byte) 0);
163+
dest.writeString(this.flashLightOnText);
164+
dest.writeString(this.flashLightOffText);
165+
dest.writeString(this.scanNoticeText);
131166
dest.writeList(this.scanLineColors);
132167
}
133168

@@ -141,6 +176,9 @@ protected ScanOption(Parcel in) {
141176
this.title = in.readString();
142177
this.showAlbum = in.readByte() != 0;
143178
this.continuousScan = in.readByte() != 0;
179+
this.flashLightOnText = in.readString();
180+
this.flashLightOffText = in.readString();
181+
this.scanNoticeText = in.readString();
144182
this.scanLineColors = new ArrayList<>();
145183
in.readList(this.scanLineColors, Integer.class.getClassLoader());
146184
}

0 commit comments

Comments
 (0)