1
1
package io .github .subhamtyagi .ocr ;
2
2
3
3
import android .annotation .SuppressLint ;
4
- import android .app .ActivityManager ;
5
- import android .app .ProgressDialog ;
6
4
import android .content .Context ;
7
5
import android .content .Intent ;
8
6
import android .graphics .Bitmap ;
12
10
import android .net .ConnectivityManager ;
13
11
import android .net .NetworkInfo ;
14
12
import android .net .Uri ;
15
- import android .os .AsyncTask ;
16
13
import android .os .Bundle ;
17
14
import android .os .Handler ;
18
15
import android .os .Looper ;
23
20
import android .view .MenuItem ;
24
21
import android .view .View ;
25
22
import android .widget .ImageView ;
26
- import android .widget .ProgressBar ;
27
23
import android .widget .TextView ;
28
24
import android .widget .Toast ;
29
25
@@ -120,6 +116,12 @@ public class MainActivity extends AppCompatActivity implements TessBaseAPI.Progr
120
116
private ExecutorService executorService ;
121
117
private Handler handler ;
122
118
119
+ private LinearProgressIndicator mProgressSpinner ;
120
+ private LinearProgressIndicator mProgressBar ;
121
+ private TextView mProgressTitle ;
122
+ private TextView mProgressMessage ;
123
+
124
+
123
125
@ Override
124
126
protected void onCreate (Bundle savedInstanceState ) {
125
127
super .onCreate (savedInstanceState );
@@ -176,7 +178,7 @@ private void initViews() {
176
178
Bitmap bitmap = ((BitmapDrawable ) drawable ).getBitmap ();
177
179
if (bitmap != null ) {
178
180
isRefresh = true ;
179
- executorService .submit (new ConvertImageToTextTask (bitmap ));
181
+ executorService .submit (new ConvertImageToText (bitmap ));
180
182
}
181
183
}
182
184
} else {
@@ -351,7 +353,7 @@ private void downloadLanguageData(final String dataType, Set<Language> languages
351
353
.setMessage (msg )
352
354
.setPositiveButton (R .string .yes , (dialog , which ) -> {
353
355
dialog .cancel ();
354
- executorService .submit (new DownloadTrainingTask (dataType , language .getCode ()));
356
+ executorService .submit (new DownloadTraining (dataType , language .getCode ()));
355
357
})
356
358
.setNegativeButton (R .string .no , (dialog , which ) -> dialog .cancel ()).create ();
357
359
dialog .show ();
@@ -395,10 +397,6 @@ private boolean languageDataMissing(final @NonNull String dataType, final @NonNu
395
397
return !new File (currentDirectory , String .format (Constants .LANGUAGE_CODE , language .getCode ())).exists ();
396
398
}
397
399
398
- /**
399
- * select the image when button is clicked
400
- * using
401
- */
402
400
private void selectImage () {
403
401
CropImage .activity ()
404
402
.setGuidelines (CropImageView .Guidelines .ON )
@@ -421,7 +419,7 @@ private void convertImageToText(Uri imageUri) {
421
419
Log .e (TAG , "convertImageToText: " + e .getLocalizedMessage ());
422
420
}
423
421
mImageView .setImageURI (imageUri );
424
- executorService .submit (new ConvertImageToTextTask (bitmap ));
422
+ executorService .submit (new ConvertImageToText (bitmap ));
425
423
}
426
424
427
425
@ Override
@@ -468,23 +466,6 @@ public boolean onCreateOptionsMenu(final Menu menu) {
468
466
@ Override
469
467
protected void onStop () {
470
468
super .onStop ();
471
-
472
- /*if (dialog != null) {
473
- dialog.dismiss();
474
- dialog = null;
475
- }
476
- if (mProgressDialog != null) {
477
- mProgressDialog.cancel();
478
- mProgressDialog = null;
479
- }
480
-
481
- if (convertImageToTextTask !=null && convertImageToTextTask.getStatus()== AsyncTask.Status.RUNNING){
482
- convertImageToTextTask.cancel(true);
483
- Log.d(TAG, "onDestroy: image processing canceled");
484
- }
485
- if (downloadTrainingTask !=null && downloadTrainingTask.getStatus()== AsyncTask.Status.RUNNING){
486
- downloadTrainingTask.cancel(true);
487
- }*/
488
469
}
489
470
490
471
@ Override
@@ -542,14 +523,10 @@ public void showOCRResult(String text) {
542
523
543
524
}
544
525
545
- /**
546
- * A Runnable class to to convert the image into text the return the text in String
547
- */
548
-
549
- private class ConvertImageToTextTask implements Runnable {
526
+ private class ConvertImageToText implements Runnable {
550
527
private Bitmap bitmap ;
551
528
552
- public ConvertImageToTextTask (Bitmap bitmap ) {
529
+ public ConvertImageToText (Bitmap bitmap ) {
553
530
this .bitmap = bitmap ;
554
531
}
555
532
@@ -597,27 +574,19 @@ private void updateImageView() {
597
574
}
598
575
}
599
576
600
- /**
601
- * Download the training Data and save this to external storage
602
- */
603
- private LinearProgressIndicator mProgressSpinner ;
604
- private LinearProgressIndicator mProgressBar ;
605
- private TextView mProgressTitle ;
606
- private TextView mProgressMessage ;
607
-
608
- private class DownloadTrainingTask implements Runnable {
577
+ private class DownloadTraining implements Runnable {
609
578
private final String dataType ;
610
579
private final String lang ;
611
580
private String size ;
612
581
613
- public DownloadTrainingTask (String dataType , String lang ) {
582
+ public DownloadTraining (String dataType , String lang ) {
614
583
this .dataType = dataType ;
615
584
this .lang = lang ;
616
585
}
617
586
618
587
@ Override
619
588
public void run () {
620
- handler .post (() -> {
589
+ handler .post (() -> {
621
590
mProgressTitle .setText (getString (R .string .downloading ));
622
591
mProgressMessage .setText (getString (R .string .downloading_language ));
623
592
mProgressTitle .setVisibility (View .VISIBLE );
@@ -636,27 +605,24 @@ public void run() {
636
605
if (success ) {
637
606
initializeOCR (Utils .getTrainingDataLanguages (MainActivity .this ));
638
607
} else {
639
- // Handle failure case
640
608
Toast .makeText (MainActivity .this , "Download failed" , Toast .LENGTH_SHORT ).show ();
641
609
}
642
610
});
643
611
}
644
612
613
+ @ SuppressLint ("DefaultLocale" )
645
614
private boolean downloadTrainingData (String dataType , String lang ) {
646
615
String downloadURL = getDownloadUrl (dataType , lang );
647
616
if (downloadURL == null ) {
648
617
return false ;
649
618
}
650
-
651
619
try {
652
620
URL url = new URL (downloadURL );
653
621
HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
654
622
conn .setInstanceFollowRedirects (false );
655
623
downloadURL = followRedirects (conn , downloadURL );
656
-
657
624
conn = (HttpURLConnection ) new URL (downloadURL ).openConnection ();
658
625
conn .connect ();
659
-
660
626
int totalContentSize = conn .getContentLength ();
661
627
if (totalContentSize <= 0 ) {
662
628
return false ;
@@ -665,9 +631,9 @@ private boolean downloadTrainingData(String dataType, String lang) {
665
631
666
632
// Switch from indeterminate to determinate progress bar
667
633
handler .post (() -> {
668
- mProgressSpinner .setVisibility (View .GONE ); // Hide spinner
669
- mProgressBar .setVisibility (View .VISIBLE ); // Show determinate progress bar
670
- mProgressMessage .setText ("0%" + getString (R .string .percentage_downloaded ) + size ); // Update message
634
+ mProgressSpinner .setVisibility (View .GONE );
635
+ mProgressBar .setVisibility (View .VISIBLE );
636
+ mProgressMessage .setText (String . format ( "%d %s %d" , 0 , getString (R .string .percentage_downloaded ), size ) ); // Update message
671
637
mProgressBar .setProgress (0 ); // Reset progress bar to 0
672
638
});
673
639
@@ -682,11 +648,9 @@ private boolean downloadTrainingData(String dataType, String lang) {
682
648
output .write (data , 0 , count );
683
649
downloaded += count ;
684
650
int percentage = (downloaded * 100 ) / totalContentSize ;
685
-
686
- // Update progress bar and message
687
651
handler .post (() -> {
688
652
mProgressBar .setProgress (percentage );
689
- mProgressMessage .setText (percentage + "% " + getString (R .string .percentage_downloaded ) + size );
653
+ mProgressMessage .setText (String . format ( "%d %s %d" , percentage , getString (R .string .percentage_downloaded ), size ) );
690
654
});
691
655
}
692
656
output .flush ();
@@ -715,6 +679,7 @@ private String getDownloadUrl(String dataType, String lang) {
715
679
String .format (Constants .TESSERACT_DATA_DOWNLOAD_URL_FAST , lang );
716
680
}
717
681
}
682
+
718
683
private String followRedirects (HttpURLConnection conn , String downloadURL ) throws IOException {
719
684
while (true ) {
720
685
int responseCode = conn .getResponseCode ();
0 commit comments