/
CameraCaptureActivity.java
156 lines (123 loc) · 4.06 KB
/
CameraCaptureActivity.java
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
package com.example.merchant.activity;
import java.io.IOException;
import java.util.Vector;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import com.example.merchant.FinishListener;
import com.example.merchant.R;
import com.example.merchant.ViewfinderView;
import com.example.merchant.camera.CameraManager;
import com.example.merchant.decode.DecodeFormatManager;
import com.example.merchant.service.IParentActivity;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
public class CameraCaptureActivityDemo extends RotateableActivity implements SurfaceHolder.Callback, IParentActivity {
private static final String TAG = CameraCaptureActivityDemo.class.getSimpleName();
private CaptureActivityHandler handler;
private ViewfinderView viewfinderView;
private boolean hasSurface;
private Vector<BarcodeFormat> decodeFormats;
private String characterSet;
ViewfinderView getViewfinderView() {
return viewfinderView;
}
public Handler getHandler() {
return handler;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.camera_capture);
CameraManager.init(getApplication());
viewfinderView = (ViewfinderView) findViewById(R.id.camera_view_viewfinder_view);
handler = null;
hasSurface = false;
preinitCamera();
}
@Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
decodeFormats = DecodeFormatManager.parseDecodeFormats(intent);
}
private void preinitCamera() {
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.camera_view_preview_view);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
if (hasSurface) {
// The activity was paused but not stopped, so the surface still exists. Therefore
// surfaceCreated() won't be called, so init the camera here.
initCamera(surfaceHolder);
} else {
// Install the callback and wait for surfaceCreated() to init the
// camera.
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
}
@Override
protected void onPause() {
super.onPause();
if (handler != null) {
handler.quitSynchronously();
handler = null;
}
CameraManager.get().closeDriver();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public void surfaceCreated(SurfaceHolder holder) {
if (!hasSurface) {
hasSurface = true;
initCamera(holder);
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
hasSurface = false;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void handleDecode(Result rawResult, Bitmap barcode) {
//here is were you take the result and process it, etc
}
private void initCamera(SurfaceHolder surfaceHolder) {
try {
CameraManager.get().openDriver(surfaceHolder);
} catch (IOException ioe) {
Log.w(TAG, ioe);
displayFrameworkBugMessageAndExit();
return;
} catch (RuntimeException e) {
// Barcode Scanner has seen crashes in the wild of this variety:
// java.?lang.?RuntimeException: Fail to connect to camera service
Log.w(TAG, "Unexpected error initializating camera", e);
displayFrameworkBugMessageAndExit();
return;
}
if (handler == null) {
handler = new CaptureActivityHandler(this, decodeFormats, characterSet);
}
}
private void displayFrameworkBugMessageAndExit() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.app_name));
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();
}
public void drawViewfinder() {
viewfinderView.drawViewfinder();
}
}