Skip to content

Commit 4d4ddb6

Browse files
committed
Merge branch 'master' into feature_one_thread
2 parents d4752aa + bed5d20 commit 4d4ddb6

11 files changed

Lines changed: 116 additions & 57 deletions

File tree

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ C++ port of ZXing for Android
99
### 使用
1010
在gradle中:
1111
``` groovy
12-
implementation 'me.devilsen:CZXing:0.8.8'
12+
implementation 'me.devilsen:CZXing:0.8.9'
1313
```
1414
建议加入abiFilters
1515
```gradle
@@ -37,6 +37,7 @@ Scanner.with(this)
3737
.setScanMode(ScanView.SCAN_MODE_TINY) // 扫描区域
3838
.setTitle("My Scan View") // 扫码界面标题
3939
.showAlbum(true) // 显示相册(默认为true)
40+
.continuousScan() // 连续扫码,不关闭扫码界面
4041
.setOnClickAlbumDelegate(new ScanActivityDelegate.OnClickAlbumDelegate() {
4142
@Override
4243
public void onClickAlbum(Activity activity) { // 点击右上角的相册按钮
@@ -53,7 +54,7 @@ Scanner.with(this)
5354
})
5455
.setOnScanResultDelegate(new ScanActivityDelegate.OnScanDelegate() { // 接管扫码成功的数据
5556
@Override
56-
public void onScanResult(String result) {
57+
public void onScanResult(String result, BarcodeFormat format) {
5758
Intent intent = new Intent(MainActivity.this, DelegateActivity.class);
5859
intent.putExtra("result", result);
5960
startActivity(intent);
@@ -75,7 +76,7 @@ Scanner.with(this)
7576
```java
7677
mScanView.setScanListener(new ScanListener() {
7778
@Override
78-
public void onScanSuccess(String result) {
79+
public void onScanSuccess(String result, BarcodeFormat format) {
7980
// 扫码成功
8081
}
8182

@@ -101,3 +102,19 @@ Bitmap bitmap = writer.write("Hello World", BarCodeUtil.dp2px(this, 200), BarCod
101102

102103
[设计思路](https://www.jianshu.com/p/e2866af44236)
103104

105+
## License
106+
107+
Copyright (C) 2012 The Android Open Source Project
108+
Copyright 2019 Devilsen
109+
110+
Licensed under the Apache License, Version 2.0 (the "License");
111+
you may not use this file except in compliance with the License.
112+
You may obtain a copy of the License at
113+
114+
http://www.apache.org/licenses/LICENSE-2.0
115+
116+
Unless required by applicable law or agreed to in writing, software
117+
distributed under the License is distributed on an "AS IS" BASIS,
118+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
119+
See the License for the specific language governing permissions and
120+
limitations under the License.

czxing/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77
minSdkVersion rootProject.ext.minSdkVersion
88
targetSdkVersion rootProject.ext.targetSdkVersion
99
versionCode 14
10-
versionName "0.8.8"
10+
versionName "0.8.9"
1111

1212
externalNativeBuild {
1313
cmake {

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

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
import android.content.Intent;
66
import android.content.pm.PackageManager;
77
import android.os.Bundle;
8-
import android.text.TextUtils;
8+
import android.os.Handler;
9+
import android.os.Message;
910
import android.util.Log;
1011
import android.view.View;
1112
import android.widget.FrameLayout;
1213
import android.widget.ImageView;
1314
import android.widget.LinearLayout;
1415
import android.widget.TextView;
1516

17+
import me.devilsen.czxing.code.BarcodeFormat;
1618
import me.devilsen.czxing.compat.ActivityCompat;
1719
import me.devilsen.czxing.compat.ContextCompat;
1820
import me.devilsen.czxing.util.BarCodeUtil;
@@ -33,12 +35,18 @@
3335
public class ScanActivity extends Activity implements ScanListener, View.OnClickListener {
3436

3537
private static final int PERMISSIONS_REQUEST_CAMERA = 1;
38+
private static final int MESSAGE_WHAT_START_SCAN = 10;
39+
private static final long DELAY_TIME = 800;
40+
41+
private TextView titleTxt;
42+
private TextView albumTxt;
3643
private ScanView mScanView;
44+
private SoundPoolUtil mSoundPoolUtil;
45+
46+
private boolean isContinuousScan;
47+
3748
private ScanActivityDelegate.OnScanDelegate scanDelegate;
3849
private ScanActivityDelegate.OnClickAlbumDelegate clickAlbumDelegate;
39-
private SoundPoolUtil mSoundPoolUtil;
40-
private TextView titleTxt;
41-
private TextView albumTxt;
4250

4351
@Override
4452
protected void onCreate(Bundle savedInstanceState) {
@@ -94,13 +102,8 @@ private void initData() {
94102
albumTxt.setVisibility(View.INVISIBLE);
95103
albumTxt.setOnClickListener(null);
96104
}
97-
}
98-
99-
@Override
100-
protected void onStart() {
101-
super.onStart();
102-
// mScanView.openCamera(); // 打开后置摄像头开始预览,但是并未开始识别
103-
// mScanView.startScan(); // 显示扫描框,并开始识别
105+
// 连续扫描
106+
isContinuousScan = option.isContinuousScan();
104107
}
105108

106109
@Override
@@ -109,23 +112,16 @@ protected void onResume() {
109112
mScanView.openCamera(); // 打开后置摄像头开始预览,但是并未开始识别
110113
mScanView.startScan(); // 显示扫描框,并开始识别
111114

112-
// mScanView.startScan(); // 显示扫描框,并开始识别
113-
// mScanView.resetZoom(); // 重置相机扩大倍数
115+
if (isContinuousScan) {
116+
mScanView.resetZoom(); // 重置相机扩大倍数
117+
}
114118
}
115119

116120
@Override
117121
protected void onPause() {
118122
super.onPause();
119123
mScanView.stopScan();
120124
mScanView.closeCamera(); // 关闭摄像头预览,并且隐藏扫描框
121-
122-
}
123-
124-
@Override
125-
protected void onStop() {
126-
// mScanView.stopScan();
127-
// mScanView.closeCamera(); // 关闭摄像头预览,并且隐藏扫描框
128-
super.onStop();
129125
}
130126

131127
@Override
@@ -148,16 +144,21 @@ public void onClick(View v) {
148144
}
149145

150146
@Override
151-
public void onScanSuccess(String result) {
147+
public void onScanSuccess(String result, BarcodeFormat format) {
152148
mSoundPoolUtil.play();
153149

154150
if (scanDelegate != null) {
155-
scanDelegate.onScanResult(result);
151+
scanDelegate.onScanResult(result, format);
156152
} else {
157153
Intent intent = new Intent(this, ResultActivity.class);
158154
intent.putExtra("result", result);
159155
startActivity(intent);
160156
}
157+
// 连续扫码,不关闭界面
158+
if (isContinuousScan) {
159+
handler.sendEmptyMessageDelayed(MESSAGE_WHAT_START_SCAN, DELAY_TIME);
160+
return;
161+
}
161162
finish();
162163
}
163164

@@ -166,6 +167,14 @@ public void onOpenCameraError() {
166167
Log.e("onOpenCameraError", "onOpenCameraError");
167168
}
168169

170+
private final Handler handler = new Handler(new Handler.Callback() {
171+
@Override
172+
public boolean handleMessage(Message msg) {
173+
mScanView.startScan();
174+
return true;
175+
}
176+
});
177+
169178
private void requestPermission() {
170179
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
171180
!= PackageManager.PERMISSION_GRANTED) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public ScannerManager showAlbum(boolean showAlbum) {
5151
return this;
5252
}
5353

54+
public ScannerManager continuousScan() {
55+
scanOption.continuousScan = true;
56+
return this;
57+
}
58+
5459
public ScannerManager setScanLineColors(List<Integer> scanLineColors) {
5560
scanOption.scanLineColors = scanLineColors;
5661
return this;
@@ -79,6 +84,7 @@ public static class ScanOption implements Parcelable {
7984
private int scanMode;
8085
private String title;
8186
private boolean showAlbum = true;
87+
private boolean continuousScan;
8288
private List<Integer> scanLineColors;
8389

8490
public int getCornerColor() {
@@ -101,6 +107,10 @@ public boolean isShowAlbum() {
101107
return showAlbum;
102108
}
103109

110+
public boolean isContinuousScan(){
111+
return continuousScan;
112+
}
113+
104114
public List<Integer> getScanLineColors() {
105115
return scanLineColors;
106116
}
@@ -117,6 +127,7 @@ public void writeToParcel(Parcel dest, int flags) {
117127
dest.writeInt(this.scanMode);
118128
dest.writeString(this.title);
119129
dest.writeByte(this.showAlbum ? (byte) 1 : (byte) 0);
130+
dest.writeByte(this.continuousScan ? (byte) 1 : (byte) 0);
120131
dest.writeList(this.scanLineColors);
121132
}
122133

@@ -129,6 +140,7 @@ protected ScanOption(Parcel in) {
129140
this.scanMode = in.readInt();
130141
this.title = in.readString();
131142
this.showAlbum = in.readByte() != 0;
143+
this.continuousScan = in.readByte() != 0;
132144
this.scanLineColors = new ArrayList<>();
133145
in.readList(this.scanLineColors, Integer.class.getClassLoader());
134146
}

czxing/src/main/java/me/devilsen/czxing/view/BarCoderView.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,12 @@ public void startScan() {
156156
mSpotAble = true;
157157
openCamera();
158158
setPreviewCallback();
159+
mScanBoxView.startAnim();
159160
}
160161

161162
public void stopScan() {
162163
mSpotAble = false;
164+
mScanBoxView.stopAnim();
163165

164166
if (mCamera == null) {
165167
return;
@@ -217,6 +219,7 @@ private void startCameraById(int cameraId) {
217219
mCameraSurface.setCamera(mCamera);
218220
} catch (Exception e) {
219221
e.printStackTrace();
222+
mSpotAble = false;
220223
if (mScanListener != null) {
221224
mScanListener.onOpenCameraError();
222225
}
@@ -301,7 +304,6 @@ void tryZoom(CodeResult result) {
301304
}
302305
}
303306

304-
BarCodeUtil.d("len " + len);
305307
handleAutoZoom(len);
306308
}
307309

czxing/src/main/java/me/devilsen/czxing/view/ScanActivityDelegate.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import android.app.Activity;
44
import android.content.Intent;
55

6+
import me.devilsen.czxing.code.BarcodeFormat;
7+
68
/**
79
* @author : dongSen
810
* date : 2019/07/21
@@ -38,7 +40,7 @@ public void setOnClickAlbumDelegate(OnClickAlbumDelegate onClickAlbumDelegate) {
3840
}
3941

4042
public interface OnScanDelegate {
41-
void onScanResult(String result);
43+
void onScanResult(String result, BarcodeFormat format);
4244
}
4345

4446
public interface OnClickAlbumDelegate {

czxing/src/main/java/me/devilsen/czxing/view/ScanBoxView.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ private void moveScanLine() {
357357
@Override
358358
public void onAnimationUpdate(ValueAnimator animation) {
359359
mScanLinePosition = (float) animation.getAnimatedValue();
360-
// postInvalidate();
361-
postInvalidate(mBoxLeft,
360+
// 这里如果用postInvalidate会导致所在Activity的onStop和onDestroy方法阻塞,感谢lhhseraph的反馈
361+
postInvalidateOnAnimation(mBoxLeft,
362362
((int) (mBoxTop + mScanLinePosition - 10)),
363363
mBoxLeft + mBoxSize,
364364
((int) (mBoxTop + mScanLinePosition + SCAN_LINE_HEIGHT + 10)));
@@ -449,6 +449,18 @@ public void hideCardText() {
449449
this.mDrawCardText = false;
450450
}
451451

452+
public void startAnim() {
453+
if (mScanLineAnimator != null && !mScanLineAnimator.isRunning()) {
454+
mScanLineAnimator.start();
455+
}
456+
}
457+
458+
public void stopAnim() {
459+
if (mScanLineAnimator != null && mScanLineAnimator.isRunning()) {
460+
mScanLineAnimator.cancel();
461+
}
462+
}
463+
452464
public interface ScanBoxClickListener {
453465
void onFlashLightClick();
454466
}

czxing/src/main/java/me/devilsen/czxing/view/ScanListener.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.devilsen.czxing.view;
22

3+
import me.devilsen.czxing.code.BarcodeFormat;
4+
35
/**
46
* @author : dongSen
57
* date : 2019-06-29 16:16
@@ -12,7 +14,7 @@ public interface ScanListener {
1214
*
1315
* @param result 摄像头扫码时只要回调了该方法 result 就一定有值,不会为 null
1416
*/
15-
void onScanSuccess(String result);
17+
void onScanSuccess(String result, BarcodeFormat format);
1618

1719
/**
1820
* 处理打开相机出错

0 commit comments

Comments
 (0)