CaptureActivity.java
8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package com.yzq.zxinglibrary.android;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.AppCompatImageView;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import com.google.zxing.Result;
import com.huaheng.wms.R;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.camera.CameraManager;
import com.yzq.zxinglibrary.common.Constant;
import com.yzq.zxinglibrary.decode.DecodeImgCallback;
import com.yzq.zxinglibrary.decode.DecodeImgThread;
import com.yzq.zxinglibrary.decode.ImageUtil;
import com.yzq.zxinglibrary.view.ViewfinderView;
import java.io.IOException;
/**
* @author: yzq
* @date: 2017/10/26 15:22
* @declare :扫一扫
*/
public class CaptureActivity extends AppCompatActivity implements SurfaceHolder.Callback, View.OnClickListener {
private static final String TAG = CaptureActivity.class.getSimpleName();
public ZxingConfig config;
private SurfaceView previewView;
private ViewfinderView viewfinderView;
private AppCompatImageView backIv;
private boolean hasSurface;
private InactivityTimer inactivityTimer;
private BeepManager beepManager;
private CameraManager cameraManager;
private CaptureActivityHandler handler;
private SurfaceHolder surfaceHolder;
public ViewfinderView getViewfinderView() {
return viewfinderView;
}
public Handler getHandler() {
return handler;
}
public CameraManager getCameraManager() {
return cameraManager;
}
public void drawViewfinder() {
viewfinderView.drawViewfinder();
}
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 保持Activity处于唤醒状态
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(Color.BLACK);
}
/*先获取配置信息*/
try {
config = (ZxingConfig) getIntent().getExtras().get(Constant.INTENT_ZXING_CONFIG);
} catch (Exception e) {
Log.i("config", e.toString());
}
if (config == null) {
config = new ZxingConfig();
}
getSupportActionBar().hide();
setContentView(R.layout.activity_capture);
initView();
hasSurface = false;
inactivityTimer = new InactivityTimer(this);
beepManager = new BeepManager(this);
beepManager.setPlayBeep(config.isPlayBeep());
beepManager.setVibrate(config.isShake());
}
private void initView() {
previewView = findViewById(R.id.preview_view);
previewView.setOnClickListener(this);
viewfinderView = findViewById(R.id.viewfinder_view);
viewfinderView.setZxingConfig(config);
backIv = findViewById(R.id.backIv);
backIv.setOnClickListener(this);
}
/**
* @param pm
* @return 是否有闪光灯
*/
public static boolean isSupportCameraLedFlash(PackageManager pm) {
if (pm != null) {
FeatureInfo[] features = pm.getSystemAvailableFeatures();
if (features != null) {
for (FeatureInfo f : features) {
if (f != null && PackageManager.FEATURE_CAMERA_FLASH.equals(f.name)) {
return true;
}
}
}
}
return false;
}
/**
* @param rawResult 返回的扫描结果
*/
public void handleDecode(Result rawResult) {
inactivityTimer.onActivity();
beepManager.playBeepSoundAndVibrate();
Intent intent = getIntent();
intent.putExtra(Constant.CODED_CONTENT, rawResult.getText());
setResult(RESULT_OK, intent);
this.finish();
}
private void switchVisibility(View view, boolean b) {
if (b) {
view.setVisibility(View.VISIBLE);
} else {
view.setVisibility(View.GONE);
}
}
@Override
protected void onResume() {
super.onResume();
cameraManager = new CameraManager(getApplication(),config);
viewfinderView.setCameraManager(cameraManager);
handler = null;
surfaceHolder = previewView.getHolder();
if (hasSurface) {
initCamera(surfaceHolder);
} else {
// 重置callback,等待surfaceCreated()来初始化camera
surfaceHolder.addCallback(this);
}
beepManager.updatePrefs();
inactivityTimer.onResume();
}
private void initCamera(SurfaceHolder surfaceHolder) {
if (surfaceHolder == null) {
throw new IllegalStateException("No SurfaceHolder provided");
}
if (cameraManager.isOpen()) {
return;
}
try {
// 打开Camera硬件设备
cameraManager.openDriver(surfaceHolder);
// 创建一个handler来打开预览,并抛出一个运行时异常
if (handler == null) {
handler = new CaptureActivityHandler(this, cameraManager);
}
} catch (IOException ioe) {
Log.w(TAG, ioe);
displayFrameworkBugMessageAndExit();
} catch (RuntimeException e) {
Log.w(TAG, "Unexpected error initializing camera", e);
displayFrameworkBugMessageAndExit();
}
}
private void displayFrameworkBugMessageAndExit() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("扫一扫");
builder.setMessage(getString(R.string.msg_camera_framework_bug));
builder.setPositiveButton(R.string.button_ok, new FinishListener(this));
builder.setOnCancelListener(new FinishListener(this));
builder.show();
}
@Override
protected void onPause() {
if (handler != null) {
handler.quitSynchronously();
handler = null;
}
inactivityTimer.onPause();
beepManager.close();
cameraManager.closeDriver();
if (!hasSurface) {
surfaceHolder.removeCallback(this);
}
super.onPause();
}
@Override
protected void onDestroy() {
inactivityTimer.shutdown();
super.onDestroy();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (!hasSurface) {
hasSurface = true;
initCamera(holder);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
hasSurface = false;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.backIv) {
finish();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constant.REQUEST_IMAGE && resultCode == RESULT_OK) {
String path = ImageUtil.getImageAbsolutePath(this, data.getData());
new DecodeImgThread(path, new DecodeImgCallback() {
@Override
public void onImageDecodeSuccess(Result result) {
handleDecode(result);
}
@Override
public void onImageDecodeFailed() {
Toast.makeText(CaptureActivity.this, "抱歉,解析失败,换个图片试试.", Toast.LENGTH_SHORT).show();
}
}).run();
}
}
}