-
Notifications
You must be signed in to change notification settings - Fork 272
/
MainActivity.java
389 lines (333 loc) · 18.5 KB
/
MainActivity.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package com.sandro.openalprsample;
import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.transition.Explode;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.squareup.picasso.Picasso;
import org.openalpr.OpenALPR;
import org.openalpr.model.Candidate;
import org.openalpr.model.Coordinate;
import org.openalpr.model.Result;
import org.openalpr.model.Results;
import org.openalpr.model.ResultsError;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private final int REQUEST_IMAGE = 100;
private final int REQUEST_FILE = 42;
private final int STORAGE = 1;
private String ANDROID_DATA_DIR;
private File imgFolder;
private File imageFile;
private Context appCtx;
private ImageView imageView;
private EditText txtCountry;
private EditText txtRegion;
private EditText txtCandidatesNum;
private TableLayout resultTable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPermission();
appCtx = this;
ANDROID_DATA_DIR = this.getApplicationInfo().dataDir;
txtCandidatesNum = (EditText) findViewById(R.id.txtCandidatesNum);
txtCountry = (EditText) findViewById(R.id.txtCountry);
txtRegion = (EditText) findViewById(R.id.txtRegion);
resultTable = (TableLayout) findViewById(R.id.resultTable);
imageView = (ImageView) findViewById(R.id.imageView);
findViewById(R.id.btnTakePicture).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
takePicture();
}
});
findViewById(R.id.btnLoad).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadPicture();
}
});
findViewById(R.id.btnClear).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultTable.setVisibility(View.GONE);
int count = resultTable.getChildCount();
for (int i = 1; i < count; i++) {
View child = resultTable.getChildAt(i);
if (child instanceof TableRow) ((ViewGroup) child).removeAllViews();
}
Toast.makeText(appCtx, "Result table cleared!", Toast.LENGTH_LONG).show();
}
});
findViewById(R.id.btnFlushDir).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int cont = 0;
for (File file : imgFolder.listFiles()) {
if (file.delete()) ++cont;
}
Toast.makeText(appCtx, cont + " files deleted successfully!", Toast.LENGTH_LONG).show();
}
});
imgFolder = new File(Environment.getExternalStorageDirectory() + "/OpenALPR/");
if (!imgFolder.exists()) {
imgFolder.mkdir();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == REQUEST_IMAGE || requestCode == REQUEST_FILE) && resultCode == Activity.RESULT_OK) {
final long startTime = System.currentTimeMillis();
final long[] endTime = new long[1];
final ProgressDialog progress = ProgressDialog.show(this, "Loading", "Parsing result...", true);
final String openAlprConfFile = ANDROID_DATA_DIR + File.separatorChar + "runtime_data" + File.separatorChar + "openalpr.conf";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
if (requestCode == REQUEST_FILE) {
if (data != null && data.getData() != null) {
String path = Environment.getExternalStorageDirectory().getPath() + "/" + data.getData().getLastPathSegment().split(":")[1];
imageFile = new File(path);
Picasso.with(MainActivity.this).invalidate(imageFile);
}
}
final int[] x1 = { 0 };
final int[] x2 = { 0 };
final int[] y1 = { 0 };
final int[] y2 = { 0 };
final String[] plate = {""};
AsyncTask.execute(new Runnable() {
@Override
public void run() {
int candidates = txtCandidatesNum.getText().toString().isEmpty()? 5 : Integer.parseInt((txtCandidatesNum.getText().toString()));
String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DIR).recognizeWithCountryRegionNConfig(txtCountry.getText().toString(), txtRegion.getText().toString(), imageFile.getAbsolutePath(), openAlprConfFile, candidates);
Log.d("OPEN ALPR", result);
try {
final Results results = new Gson().fromJson(result, Results.class);
runOnUiThread(new Runnable() {
@Override
public void run() {
resultTable.setVisibility(View.VISIBLE);
if (results == null || results.getResults() == null || results.getResults().size() == 0) {
Toast.makeText(MainActivity.this, "It was not possible to detect the licence plate.", Toast.LENGTH_LONG).show();
} else {
endTime[0] = System.currentTimeMillis();
TableLayout.LayoutParams rowLayoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams cellLayoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
List<Result> resultsList = results.getResults();
for(int i = 0; i < resultsList.size(); ++i) {
Result result = resultsList.get(i);
if (i == 0) { // save rectangle coordinates and plate of best result
x1[0] = result.getCoordinates().get(0).getX();
y1[0] = result.getCoordinates().get(0).getY();
x2[0] = result.getCoordinates().get(2).getX();
y2[0] = result.getCoordinates().get(2).getY();
plate[0] = result.getPlate();
}
TableRow tableRow = new TableRow(appCtx);
tableRow.setLayoutParams(rowLayoutParams);
if (result.getConfidence() < 60)
tableRow.setBackgroundColor(Color.RED);
else if (result.getConfidence() < 85)
tableRow.setBackgroundColor(Color.YELLOW);
else if (result.getConfidence() >= 85)
tableRow.setBackgroundColor(Color.GREEN);
TextView cellValue = new TextView(appCtx);
cellValue.setTypeface(null, Typeface.BOLD);
cellValue.setText(result.getPlate());
cellValue.setLayoutParams(cellLayoutParams);
tableRow.addView(cellValue);
cellValue = new TextView(appCtx);
cellValue.setTypeface(null, Typeface.BOLD);
cellValue.setText(String.format("%.2f", result.getConfidence())+"%");
cellValue.setLayoutParams(cellLayoutParams);
tableRow.addView(cellValue);
String region = txtCountry.getText().toString()+"_"+txtRegion.getText().toString();
cellValue = new TextView(appCtx);
cellValue.setTypeface(null, Typeface.BOLD);
cellValue.setText(region.length() == 1? "n/a" : region);
cellValue.setLayoutParams(cellLayoutParams);
tableRow.addView(cellValue);
cellValue = new TextView(appCtx);
cellValue.setTypeface(null, Typeface.BOLD);
cellValue.setText(String.format("%.2f", result.getMatchesTemplate()));
cellValue.setLayoutParams(cellLayoutParams);
tableRow.addView(cellValue);
cellValue = new TextView(appCtx);
cellValue.setTypeface(null, Typeface.BOLD);
cellValue.setText(String.format("%.2f", ((result.getProcessingTimeMs() / 1000.0) % 60)) + " s");
cellValue.setLayoutParams(cellLayoutParams);
tableRow.addView(cellValue);
resultTable.addView(tableRow);
List<Candidate> candidates = result.getCandidates();
for (int j = 1; j < candidates.size(); ++j) {
Candidate candidate = candidates.get(j);
tableRow = new TableRow(appCtx);
tableRow.setLayoutParams(rowLayoutParams);
tableRow.setBackgroundColor(Color.LTGRAY);
cellValue = new TextView(appCtx);
cellValue.setText(candidate.getPlate());
cellValue.setLayoutParams(cellLayoutParams);
tableRow.addView(cellValue, 0);
cellValue = new TextView(appCtx);
cellValue.setText(String.format("%.2f", candidate.getConfidence())+"%");
cellValue.setLayoutParams(cellLayoutParams);
tableRow.addView(cellValue, 1);
tableRow.addView(new TextView(appCtx), 2);
cellValue = new TextView(appCtx);
cellValue.setText(String.valueOf(candidate.getMatchesTemplate()));
cellValue.setLayoutParams(cellLayoutParams);
tableRow.addView(cellValue, 3);
resultTable.addView(tableRow);
}
}
resultTable.invalidate();
Toast.makeText(appCtx, "Processing time: " + String.format("%.2f", (((endTime[0]-startTime) / 1000.0) % 60)) + " s", Toast.LENGTH_LONG).show();
}
}
});
} catch (JsonSyntaxException exception) {
final ResultsError resultsError = new Gson().fromJson(result, ResultsError.class);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(appCtx, resultsError.getMsg(), Toast.LENGTH_LONG).show();
}
});
}
progress.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
// Picasso requires permission.WRITE_EXTERNAL_STORAGE
Picasso.with(MainActivity.this).load(imageFile).fit().centerCrop().into(imageView);
if (imageView.getDrawable() != null) {
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Bitmap originalBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), new BitmapFactory.Options());
float viewWidth = bitmap.getWidth();
float viewHeigth = bitmap.getHeight();
float originalWidth = originalBitmap.getWidth();
float originalHeigth = originalBitmap.getHeight();
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(8);
// map rectangle coordinates to imageview
int p1_x = (int)((x1[0] * viewWidth) / originalWidth);
int p1_y = (int)((y1[0] * viewHeigth) / originalHeigth);
int p2_x = (int)((x2[0] * viewWidth) / originalWidth);
int p2_y = (int)((y2[0] * viewHeigth) / originalHeigth);
canvas.drawRect(new Rect(p1_x, p1_y, p2_x, p2_y), paint);
paint.setTextSize(75);
paint.setStyle(Paint.Style.FILL);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setColor(Color.YELLOW);
canvas.drawText(plate[0], p1_x, p1_y-10, paint);
imageView.setImageBitmap(bitmap);
}
}
});
}
});
}
}
private void checkPermission() {
List<String> permissions = new ArrayList<>();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissions.isEmpty()) {
Toast.makeText(this, "Storage access needed to manage the picture.", Toast.LENGTH_LONG).show();
String[] params = permissions.toArray(new String[permissions.size()]);
ActivityCompat.requestPermissions(this, params, STORAGE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case STORAGE:{
Map<String, Integer> perms = new HashMap<>();
// Initial
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for WRITE_EXTERNAL_STORAGE
Boolean storage = perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
if (storage) {
// permission was granted, yay!
} else {
// Permission Denied
Toast.makeText(this, "Storage permission is needed to analyse the picture.", Toast.LENGTH_LONG).show();
}
}
default:
break;
}
}
public String dateToString(Date date, String format) {
SimpleDateFormat df = new SimpleDateFormat(format, Locale.getDefault());
return df.format(date);
}
public void takePicture() {
// Generate the path for the next photo
String name = dateToString(new Date(), "yyyy-MM-dd-hh-mm-ss");
imageFile = new File(imgFolder, name + ".jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
startActivityForResult(intent, REQUEST_IMAGE);
}
public void loadPicture() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_FILE);
}
@Override
protected void onResume() {
super.onResume();
if (imageFile != null) {// Picasso does not seem to have an issue with a null value, but to be safe
Picasso.with(MainActivity.this).load(imageFile).fit().centerCrop().into(imageView);
}
}
}