-
Notifications
You must be signed in to change notification settings - Fork 56
/
CameraActivity.java
297 lines (285 loc) · 12.3 KB
/
CameraActivity.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
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
package yanyu.com.mrcar;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
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.ImageView;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
public class CameraActivity extends Activity {
private static final String TAG = "MRCar";
private static final String sdcarddir = "/sdcard/mrcar";
private TextView tv;
private ImageView rv;
@Nullable
private Camera mCamera;
private boolean mlibLoaded=false;
private SurfaceHolder surfaceHolder;
private static final int REQUEST_PERMISSIONS_CODE = 1;
private static final String[] REQUIRED_PERMISSIONS = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
final int PREVIEW_WIDTH=640;
final int PREVIEW_HEIGHT=480;
private static final int PREVIEW_FORMAT = ImageFormat.NV21;
private boolean killed = false;
private byte nv21[];
private boolean isNV21ready = false;
private boolean bshowresult=false;
private BeepManager beepManager;
private ViewfinderView finderview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_camera);
tv=findViewById(R.id.rstLic);
rv=findViewById(R.id.resultview);
finderview=findViewById(R.id.finderview);
SurfaceView cameraPreview = findViewById(R.id.surface_view);
cameraPreview.getHolder().addCallback(new PreviewSurfaceCallback());
nv21 = new byte[PREVIEW_WIDTH * PREVIEW_HEIGHT * 2];
beepManager = new BeepManager(this);
beepManager.setPlayBeep(true);
rv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rv.setVisibility(View.GONE);
finderview.setVisibility(View.VISIBLE);
tv.setText("");
bshowresult=false;
}
});
}
private void setPreviewSurface(@Nullable SurfaceHolder previewSurface) {
Camera camera = mCamera;
if (camera != null && previewSurface != null) {
try {
camera.setPreviewDisplay(previewSurface);
Log.d(TAG, "setPreviewSurface() called");
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class PreviewSurfaceCallback implements SurfaceHolder.Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
surfaceHolder=holder;
setPreviewSize(width,height);
setPreviewSurface(holder);
startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
stopPreview();
}
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
new Thread() {
@Override
public void run() {
initFile();
System.loadLibrary("mrcar");
if(!mlibLoaded){
MRCar.init(sdcarddir);
mlibLoaded = true;
}
}
}.start();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
private boolean isRequiredPermissionsGranted() {
for (String permission : REQUIRED_PERMISSIONS) {
if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_DENIED) {
return false;
}
}
return true;
}
@Override
protected void onResume() {
super.onResume();
if (!isRequiredPermissionsGranted() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(REQUIRED_PERMISSIONS, REQUEST_PERMISSIONS_CODE);
}
if (!OpenCVLoader.initDebug()) {
Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this, mLoaderCallback);
} else {
Log.d(TAG, "OpenCV library found inside package. Using it!");
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
mCamera=Camera.open(0);
killed = false;
final byte[] tmp = new byte[PREVIEW_WIDTH * PREVIEW_HEIGHT * 2];
Thread thread = new Thread(){
@Override
public void run() {
super.run();
while (!killed) {
if(!isNV21ready)
continue;
if(bshowresult)
continue;
synchronized (nv21) {
System.arraycopy(nv21, 0, tmp, 0, nv21.length);
isNV21ready = false;
}
if(mlibLoaded)
{
final MRPlate[] prs = MRCar.plateNV21(tmp,PREVIEW_HEIGHT,PREVIEW_WIDTH);
if (prs.length > 0){
runOnUiThread(new Runnable() {
@Override
public void run() {
if(prs.length > 0 & prs[0].lic.length()>=6){
bshowresult=true;
//tv.position
tv.setText(prs[0].lic);
beepManager.playBeepSoundAndVibrate();
// new Handler().postDelayed(new Runnable() {
// @Override
// public void run() {
// tv.setText("");
// }
// },3000);
Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
YuvImage image = new YuvImage(tmp, ImageFormat.NV21, previewSize.width, previewSize.height, null);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height), 80, stream);
Bitmap bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
Matrix m=new Matrix();
m.setRotate(90, bitmap.getWidth()/2, bitmap.getHeight()/2);
Bitmap bmp2=Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
Bitmap mutableBitmap = bmp2.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
for (MRPlate pr : prs){
int left = pr.rect.left;
int top = pr.rect.top;
int right = pr.rect.right;
int bottom = pr.rect.bottom;
Log.d(TAG, String.format("%d,%d -> %d,%d",left,top,right,bottom));
canvas.drawRect(left, top, right, bottom, paint);
}
bitmap.recycle();
bmp2.recycle();
rv.setImageBitmap(mutableBitmap);
rv.setVisibility(View.VISIBLE);
finderview.setVisibility(View.GONE);
}
}
});
}
}
}
}
};
thread.start();
}
private void closeCamera() {
Camera camera = mCamera;
mCamera = null;
if (camera != null) {
camera.release();
}
}
protected void onPause() {
super.onPause();
closeCamera();
beepManager.close();
}
private void setPreviewSize(int shortSide, int longSide) {
Camera camera = mCamera;
if (camera != null && shortSide != 0 && longSide != 0) {
Camera.Parameters parameters = camera.getParameters();
int frameWidth = PREVIEW_WIDTH;
int frameHeight = PREVIEW_HEIGHT;
int previewFormat = parameters.getPreviewFormat();
parameters.setPreviewSize(PREVIEW_WIDTH, PREVIEW_HEIGHT);
parameters.setPreviewFormat(PREVIEW_FORMAT);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
camera.setParameters(parameters);
PixelFormat pixelFormat = new PixelFormat();
PixelFormat.getPixelFormatInfo(previewFormat, pixelFormat);
int bufferSize = (frameWidth * frameHeight * pixelFormat.bitsPerPixel) / 8;
camera.addCallbackBuffer(new byte[bufferSize]);
camera.addCallbackBuffer(new byte[bufferSize]);
camera.addCallbackBuffer(new byte[bufferSize]);
Log.d(TAG, "Add three callback buffers with size: " + bufferSize);
}
}
private class PreviewCallback implements Camera.PreviewCallback {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
synchronized (nv21) {
System.arraycopy(data, 0, nv21, 0, data.length);
isNV21ready = true;
}
camera.addCallbackBuffer(data);
}
}
private void startPreview() {
Camera camera = mCamera;
mCamera.setDisplayOrientation(90);
SurfaceHolder previewSurface = surfaceHolder;
if (camera != null && previewSurface != null) {
camera.setPreviewCallbackWithBuffer(new PreviewCallback());
camera.startPreview();
Log.d(TAG, "startPreview() called");
}
}
private void stopPreview() {
if (mCamera != null) {
mCamera.stopPreview();
Log.d(TAG, "stopPreview() called");
}
}
private void initFile(){
MRAssetUtil.CopyAssets(this,"mrcar",sdcarddir);
}
}