-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathMainActivity.java
605 lines (533 loc) · 22.7 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package io.github.subhamtyagi.ocr;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.MediaStore;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.progressindicator.LinearProgressIndicator;
import com.googlecode.tesseract.android.TessBaseAPI;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import io.github.subhamtyagi.ocr.ocr.ImageTextReader;
import io.github.subhamtyagi.ocr.utils.Constants;
import io.github.subhamtyagi.ocr.utils.Language;
import io.github.subhamtyagi.ocr.utils.SpUtil;
import io.github.subhamtyagi.ocr.utils.Utils;
public class MainActivity extends AppCompatActivity implements TessBaseAPI.ProgressNotifier {
public static final String TAG = "MainActivity";
private static final int REQUEST_CODE_SETTINGS = 797;
private static boolean isRefresh = false;
private ArrayList<String> allLanguageName;
private File dirBest;
private File dirStandard;
private File dirFast;
private File currentDirectory;
private ImageTextReader mImageTextReader;
/**
* TrainingDataType: i.e Best, Standard, Fast
*/
private String mTrainingDataType;
private int mPageSegMode;
private Map<String, String> parameters;
/**
* AlertDialog for showing when language data doesn't exists
*/
private AlertDialog dialog;
private ImageView mImageView;
private LinearProgressIndicator mProgressIndicator;
private SwipeRefreshLayout mSwipeRefreshLayout;
private FloatingActionButton mFloatingActionButton;
private LinearLayout mDownloadLayout;
/**
* Language name to be displayed
*/
private TextView mLanguageName;
private ExecutorService executorService;
private Handler handler;
private LinearProgressIndicator mProgressBar;
private TextView mProgressMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SpUtil.getInstance().init(this);
allLanguageName = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.ocr_engine_language)));
mImageView = findViewById(R.id.source_image);
mProgressIndicator = findViewById(R.id.progress_indicator);
mSwipeRefreshLayout = findViewById(R.id.swipe_to_refresh);
mFloatingActionButton = findViewById(R.id.btn_scan);
mLanguageName = findViewById(R.id.language_name1);
mProgressBar = findViewById(R.id.progress_bar);
mProgressMessage = findViewById(R.id.progress_message);
mDownloadLayout = findViewById(R.id.download_layout);
executorService = Executors.newFixedThreadPool(1);
handler = new Handler(Looper.getMainLooper());
initDirectories();
/*
* check if this was initiated by shared menu if yes then get the image uri and get the text
* language will be preselected by user in settings
*/
initIntent();
initializeOCR();
initViews();
}
private void initViews() {
mFloatingActionButton.setOnClickListener(v -> {
if (isNoLanguagesDataMissingFromSet()) {
if (mImageTextReader != null) {
selectImage();
} else {
initializeOCR();
}
} else {
downloadLanguageData();
}
});
mSwipeRefreshLayout.setOnRefreshListener(() -> {
if (isNoLanguagesDataMissingFromSet()) {
if (mImageTextReader != null) {
Drawable drawable = mImageView.getDrawable();
if (drawable != null) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
if (bitmap != null) {
isRefresh = true;
executorService.submit(new ConvertImageToText(bitmap));
}
}
} else {
initializeOCR();
}
} else {
downloadLanguageData();
}
mSwipeRefreshLayout.setRefreshing(false);
});
if (Utils.isPersistData()) {
Bitmap bitmap = loadBitmapFromStorage();
if (bitmap != null) {
mImageView.setImageBitmap(bitmap);
}
}
}
private void initIntent() {
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if (type.startsWith("image/")) {
Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
mImageView.setImageURI(imageUri);
showLanguageSelectionDialog(imageUri);
}
}
} else if (action != null && action.equals("screenshot")) {
// uri
}
}
private void showLanguageSelectionDialog(Uri imageUri) {
// if (this.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
ShareFragment frag = ShareFragment.newInstance(imageUri, allLanguageName);
frag.show(getSupportFragmentManager(), "shareFrag");
// }
}
@Override
protected void onResume() {
super.onResume();
mLanguageName.setText(Utils.getTrainingDataLanguages(this).stream().map(Language::getName).collect(Collectors.joining(", ")));
}
/* public void startOCRFromShareMenu(Uri imageUri, Set<Language> languages) {
initializeOCR(languages);
Utils.setLastUsedLanguage(this, languages);
// Log.d("radio", "showLanguageSelectionDialog: " + mLanguage);
mImageView.setImageURI(imageUri);
if (isNoLanguagesDataMissingFromSet(mTrainingDataType, languages)) {
CropImage.activity(imageUri).start(MainActivity.this);
}
}*/
private void initDirectories() {
String[] dirNames = {"best", "fast", "standard"};
for (String dirName : dirNames) {
File dir = new File(getExternalFilesDir(dirName), "tessdata");
if (dir.mkdirs() || dir.isDirectory()) {
switch (dirName) {
case "best":
dirBest = dir.getParentFile();
break;
case "fast":
dirFast = dir.getParentFile();
break;
case "standard":
dirStandard = dir.getParentFile();
break;
}
}
}
// Set currentDirectory to the last initialized directory (standard)
currentDirectory = new File(dirStandard, "tessdata");
}
/**
* initialize the OCR i.e tesseract api
* if there is no training data in directory than it will ask for download
*/
private void initializeOCR() {
Set<Language> languages=Utils.getTrainingDataLanguages(this);
File cf;
mTrainingDataType = Utils.getTrainingDataType();
mPageSegMode = Utils.getPageSegMode();
parameters = Utils.getAllParameters();
switch (mTrainingDataType) {
case "best":
currentDirectory = new File(dirBest, "tessdata");
cf = dirBest;
break;
case "standard":
cf = dirStandard;
currentDirectory = new File(dirStandard, "tessdata");
break;
default:
cf = dirFast;
currentDirectory = new File(dirFast, "tessdata");
}
if (isNoLanguagesDataMissingFromSet()) {
startImageTextReaderThread(cf, languages);
} else {
downloadLanguageData();
}
}
private void startImageTextReaderThread(File cf, Set<Language> languages) {
new Thread(() -> {
try {
if (mImageTextReader != null) {
mImageTextReader.tearDownEverything();
}
mImageTextReader = ImageTextReader.getInstance(cf.getAbsolutePath(), languages, mPageSegMode, parameters, Utils.isExtraParameterSet(), MainActivity.this);
if (mImageTextReader != null && !mImageTextReader.isSuccess()) {
handleReaderException(languages);
}
} catch (Exception e) {
handleReaderException(languages);
}
}).start();
}
private void handleReaderException(Set<Language> languages) {
File destFile = new File(currentDirectory, String.format(Constants.LANGUAGE_CODE, languages));
destFile.delete();
mImageTextReader = null;
}
private void downloadLanguageData() {
Set<Language> missingLanguage = new HashSet<>();
Set<Language> languages =Utils.getTrainingDataLanguages(this);
if (!Utils.isNetworkAvailable(getApplication())) {
Toast.makeText(this, getString(R.string.you_are_not_connected_to_internet), Toast.LENGTH_SHORT).show();
return;
}
for (Language l : languages) {
if (isLanguageDataMissing(mTrainingDataType, l)) {
missingLanguage.add(l);
}
}
String missingLangName = missingLanguage.stream().map(Language::getName).collect(Collectors.joining(", "));
String msg = String.format(getString(R.string.download_description), missingLangName);
dialog = new AlertDialog.Builder(this).setTitle(R.string.training_data_missing).setCancelable(false).setMessage(msg).setPositiveButton(R.string.yes, (dialog, which) -> {
dialog.cancel();
executorService.submit(new DownloadTraining(mTrainingDataType, missingLanguage));
}).setNegativeButton(R.string.no, (dialog, which) -> dialog.cancel()).create();
dialog.show();
}
private boolean isNoLanguagesDataMissingFromSet() {
final String dataType=mTrainingDataType;
Set<Language> languages=Utils.getTrainingDataLanguages(this);
for (Language language : languages) {
if (isLanguageDataMissing(dataType, language)) return false;
}
return true;
}
private boolean isLanguageDataMissing(final @NonNull String dataType, final @NonNull Language language) {
switch (dataType) {
case "best":
currentDirectory = new File(dirBest, "tessdata");
break;
case "standard":
currentDirectory = new File(dirStandard, "tessdata");
break;
default:
currentDirectory = new File(dirFast, "tessdata");
}
return !new File(currentDirectory, String.format(Constants.LANGUAGE_CODE, language.getCode())).exists();
}
private void selectImage() {
CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).start(this);
}
private void convertImageToText(Uri imageUri) {
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
} catch (IOException e) {
Log.e(TAG, "convertImageToText: " + e.getLocalizedMessage());
}
mImageView.setImageURI(imageUri);
executorService.submit(new ConvertImageToText(bitmap));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SETTINGS) {
initializeOCR();
}
if (resultCode == RESULT_OK) {
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
if (isNoLanguagesDataMissingFromSet()) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (result != null) {
convertImageToText(result.getUri());
}
} else {
initializeOCR();
}
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
executorService.shutdownNow();
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
if (mImageTextReader != null) mImageTextReader.tearDownEverything();
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem showHistoryItem = menu.findItem(R.id.action_history);
showHistoryItem.setVisible(Utils.isPersistData());
return true;
}
@Override
protected void onStop() {
super.onStop();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
startActivityForResult(new Intent(this, SettingsActivity.class), REQUEST_CODE_SETTINGS);
} else if (id == R.id.action_history) {
showOCRResult(Utils.getLastUsedText());
}
return super.onOptionsItemSelected(item);
}
@Override
public void onProgressValues(final TessBaseAPI.ProgressValues progressValues) {
runOnUiThread(() -> mProgressIndicator.setProgress((int) (progressValues.getPercent() * 1.46)));
}
public void saveBitmapToStorage(Bitmap bitmap) {
FileOutputStream fileOutputStream;
try {
fileOutputStream = openFileOutput("last_file.jpeg", Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, fileOutputStream);
fileOutputStream.close();
} catch (IOException e) {
Log.e(TAG, "loadBitmapFromStorage: " + e.getLocalizedMessage());
}
}
public Bitmap loadBitmapFromStorage() {
Bitmap bitmap = null;
FileInputStream fileInputStream;
try {
fileInputStream = openFileInput("last_file.jpeg");
bitmap = BitmapFactory.decodeStream(fileInputStream);
fileInputStream.close();
} catch (IOException e) {
Log.e(TAG, "loadBitmapFromStorage: " + e.getLocalizedMessage());
}
return bitmap;
}
public void showOCRResult(String text) {
if (this.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) {
BottomSheetResultsFragment bottomSheetResultsFragment = BottomSheetResultsFragment.newInstance(text);
bottomSheetResultsFragment.show(getSupportFragmentManager(), "bottomSheetResultsFragment");
}
}
private class ConvertImageToText implements Runnable {
private Bitmap bitmap;
public ConvertImageToText(Bitmap bitmap) {
this.bitmap = bitmap;
}
@Override
public void run() {
// Pre-execute on UI thread
handler.post(() -> {
mProgressIndicator.setProgress(0);
mProgressIndicator.setVisibility(View.VISIBLE);
animateImageViewAlpha(0.2f);
});
// Background execution
if (!isRefresh && Utils.isPreProcessImage()) {
bitmap = Utils.preProcessBitmap(bitmap);
}
isRefresh = false;
saveBitmapToStorage(bitmap);
String text = mImageTextReader.getTextFromBitmap(bitmap);
// Post-execution on UI thread
handler.post(() -> {
mProgressIndicator.setVisibility(View.GONE);
animateImageViewAlpha(1f);
String cleanText = Html.fromHtml(text).toString().trim();
showOCRResult(cleanText);
Toast.makeText(MainActivity.this, "With Confidence: " + mImageTextReader.getAccuracy() + "%", Toast.LENGTH_SHORT).show();
Utils.putLastUsedText(cleanText);
updateImageView();
});
}
private void animateImageViewAlpha(float alpha) {
mImageView.animate().alpha(alpha).setDuration(450).start();
}
private void updateImageView() {
Bitmap bitmap = loadBitmapFromStorage();
if (bitmap != null) {
mImageView.setImageBitmap(bitmap);
}
}
}
private class DownloadTraining implements Runnable {
private final String dataType;
private final Set<Language> languages;
private String size;
public DownloadTraining(String dataType, Set<Language> langs) {
this.dataType = dataType;
this.languages = langs;
}
@Override
public void run() {
handler.post(() -> {
mProgressMessage.setText(getString(R.string.downloading_language));
mDownloadLayout.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
});
final boolean[] success={true};
for (Language lang : languages) {
success[0] = success[0] && downloadTrainingData(dataType, lang.getCode());
}
handler.post(() -> {
mDownloadLayout.setVisibility(View.GONE);
if (success[0]) {
initializeOCR();
} else {
Toast.makeText(MainActivity.this, "Download failed", Toast.LENGTH_SHORT).show();
}
});
}
@SuppressLint("DefaultLocale")
private boolean downloadTrainingData(String dataType, String lang) {
String downloadURL = getDownloadUrl(dataType, lang);
if (downloadURL == null) {
return false;
}
try {
URL url = new URL(downloadURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(false);
downloadURL = followRedirects(conn, downloadURL);
conn = (HttpURLConnection) new URL(downloadURL).openConnection();
conn.connect();
int totalContentSize = conn.getContentLength();
if (totalContentSize <= 0) {
return false;
}
size = Utils.getSize(totalContentSize);
// Switch from indeterminate to determinate progress bar
handler.post(() -> {
mProgressBar.setVisibility(View.VISIBLE);
mProgressMessage.setText(String.format("0%s%s", getString(R.string.percentage_downloaded), size));
mProgressBar.setProgress(0); // Reset progress bar to 0
});
try (InputStream input = new BufferedInputStream(conn.getInputStream()); OutputStream output = new FileOutputStream(new File(currentDirectory, String.format(Constants.LANGUAGE_CODE, lang)))) {
byte[] data = new byte[6 * 1024];
int downloaded = 0;
int count;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
downloaded += count;
int percentage = (downloaded * 100) / totalContentSize;
handler.post(() -> {
mProgressBar.setProgress(percentage);
mProgressMessage.setText(String.format("%d%s%s.", percentage, getString(R.string.percentage_downloaded), size));
});
}
output.flush();
}
return true;
} catch (IOException e) {
Log.e(TAG, "Download failed: " + e.getLocalizedMessage());
return false;
}
}
private String getDownloadUrl(String dataType, String lang) {
switch (dataType) {
case "best":
return lang.equals("akk") ? Constants.TESSERACT_DATA_DOWNLOAD_URL_AKK_BEST : lang.equals("eqo") ? Constants.TESSERACT_DATA_DOWNLOAD_URL_EQU : String.format(Constants.TESSERACT_DATA_DOWNLOAD_URL_BEST, lang);
case "standard":
return lang.equals("akk") ? Constants.TESSERACT_DATA_DOWNLOAD_URL_AKK_STANDARD : lang.equals("eqo") ? Constants.TESSERACT_DATA_DOWNLOAD_URL_EQU : String.format(Constants.TESSERACT_DATA_DOWNLOAD_URL_STANDARD, lang);
default: // Assuming "fast" is the default
return lang.equals("akk") ? Constants.TESSERACT_DATA_DOWNLOAD_URL_AKK_FAST : lang.equals("eqo") ? Constants.TESSERACT_DATA_DOWNLOAD_URL_EQU : String.format(Constants.TESSERACT_DATA_DOWNLOAD_URL_FAST, lang);
}
}
private String followRedirects(HttpURLConnection conn, String downloadURL) throws IOException {
while (true) {
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
String location = conn.getHeaderField("Location");
URL base = new URL(downloadURL);
downloadURL = new URL(base, location).toExternalForm(); // Handle relative URLs
conn = (HttpURLConnection) new URL(downloadURL).openConnection(); // Re-open connection
} else {
break; // No more redirects
}
}
return downloadURL;
}
}
}