55import android .content .Intent ;
66import android .content .pm .PackageManager ;
77import android .os .Bundle ;
8- import android .text .TextUtils ;
8+ import android .os .Handler ;
9+ import android .os .Message ;
910import android .util .Log ;
1011import android .view .View ;
1112import android .widget .FrameLayout ;
1213import android .widget .ImageView ;
1314import android .widget .LinearLayout ;
1415import android .widget .TextView ;
1516
17+ import me .devilsen .czxing .code .BarcodeFormat ;
1618import me .devilsen .czxing .compat .ActivityCompat ;
1719import me .devilsen .czxing .compat .ContextCompat ;
1820import me .devilsen .czxing .util .BarCodeUtil ;
3335public 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 ) {
0 commit comments