-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathPhotoActivity.java
216 lines (205 loc) · 7.95 KB
/
PhotoActivity.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
package yanyu.com.mrcar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
public class PhotoActivity extends Activity {
private static final String TAG = "MRCar";
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private static final int SELECT_IMAGE_ACTIVITY_REQUEST_CODE = 200;
private static final String sdcarddir="/sdcard/mrcar";
private static final String initimgPath="test.jpg";
private Bitmap Originbitmap;
private Bitmap bmp;
private ImageView im;
private ImageButton buttonCamera;
private ImageButton buttonFolder;
private EditText et;
private boolean b2Recognition=true;
private Uri fileUri;
private static String filePath=null;
private class plateTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
doRecognition();
return null;
}
}
private void doRecognition(){
Mat m = new Mat();
bmp=Originbitmap.copy(Bitmap.Config.RGB_565,true);
Utils.bitmapToMat(bmp, m);
try {
final MRPlate[] prs =MRCar.plateRecognition(m.getNativeObjAddr(), m.getNativeObjAddr());
Utils.matToBitmap(m, bmp);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (prs.length>0){
et.setText(prs[0].lic);
im.setImageBitmap(bmp);
}
}
}
);
}
catch (Exception e)
{
Log.d(TAG,"exception occured!");
}
}
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(new Runnable() {
@Override
public void run() {
initFile();
System.loadLibrary("mrcar");
MRCar.init(sdcarddir);
doRecognition();
}
}).start();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
@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_main);
im=(ImageView)findViewById(R.id.imageView);
et=(EditText)findViewById(R.id.editText);
buttonCamera=(ImageButton)findViewById(R.id.buttonCamera);
buttonFolder=(ImageButton)findViewById(R.id.buttonFolder);
buttonCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = MRMediaFileUtil.getOutputMediaFileUri(1);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
buttonFolder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECT_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
im.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(b2Recognition) {
if (Originbitmap != null) {
new Thread(new Runnable() {
@Override
public void run() {
doRecognition();
}
}).start();
}
}
else {
im.setImageBitmap(Originbitmap);
et.setText(""); }
b2Recognition=!b2Recognition;
}
});
initBitmap();
}
private void initBitmap(){
if(filePath!=null){
Originbitmap=BitmapFactory.decodeFile(filePath);
} else{
Originbitmap=BitmapFactory.decodeFile(sdcarddir+"/"+ initimgPath);
}
im.setImageBitmap(Originbitmap);
}
@Override
protected void onResume() {
super.onResume();
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);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
MRCar.release();
}
public static Bitmap loadBitmap(ImageView im, String filepath){
int width = im.getWidth();
int height = im.getHeight();
BitmapFactory.Options factoryOptions = new BitmapFactory.Options();
factoryOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filepath, factoryOptions);
int imageWidth = factoryOptions.outWidth;
int imageHeight = factoryOptions.outHeight;
int scaleFactor = Math.min(imageWidth / width, imageHeight / height);
factoryOptions.inJustDecodeBounds = false;
factoryOptions.inSampleSize = scaleFactor;
factoryOptions.inPurgeable = true;
Bitmap bmp = BitmapFactory.decodeFile(filepath, factoryOptions);
im.setImageBitmap(bmp);
return bmp;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE == requestCode) {
if (RESULT_OK == resultCode) {
if (data != null) {
if (data.hasExtra("data")) {
Bitmap thumbnail = data.getParcelableExtra("data");
im.setImageBitmap(thumbnail); }
} else {
filePath=fileUri.getPath(); }
}
}
else if(requestCode ==SELECT_IMAGE_ACTIVITY_REQUEST_CODE&& resultCode == RESULT_OK && null != data) {
fileUri = data.getData();
filePath=MRMediaFileUtil.getPath(this,fileUri);
}
initBitmap();
}
private void initFile(){
MRAssetUtil.CopyAssets(this,"mrcar",sdcarddir);
}
}