Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. package pl.edu.pwr.s200400.tesseract_test_ver2;
  2.  
  3. import android.content.res.AssetManager;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.TextView;
  10.  
  11. import com.googlecode.tesseract.android.TessBaseAPI;
  12.  
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19.  
  20. public class MainActivity extends AppCompatActivity {
  21.  
  22.     Bitmap image; //our image
  23.     private TessBaseAPI mTess; //Tess API reference
  24.     String datapath = ""; //path to folder containing language data file
  25.  
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.activity_main);
  30.  
  31.         //init image
  32.         image = BitmapFactory.decodeResource(getResources(), R.drawable.test_image2);
  33.  
  34.         datapath = getFilesDir() + "/tesseract/";
  35.         System.out.println(datapath);
  36.  
  37.         //make sure training data has been copied
  38.         checkFile(new File(datapath + "tessdata/"));
  39.  
  40.         //initialize Tesseract API
  41.         String lang = "eng";
  42.         mTess = new TessBaseAPI();
  43.         mTess.init(datapath, lang);
  44.     }
  45.  
  46.     private void copyFiles() {
  47.         try {
  48.             //location we want the files to be at
  49.             String filepath = datapath + "/tessdata/eng.traineddata";
  50.  
  51.             //get access to AssetManager
  52.             AssetManager assetManager = getAssets();
  53.  
  54.             //open byte streams for reading/writing
  55.             InputStream instream = assetManager.open("tessdata/eng.traineddata");
  56.             OutputStream outstream = new FileOutputStream(filepath);
  57.  
  58.             byte[] buffer = new byte[1024];
  59.             int read;
  60.             while ((read = instream.read(buffer)) != -1) {
  61.                 outstream.write(buffer, 0, read);
  62.             }
  63.  
  64.             outstream.flush();
  65.             outstream.close();
  66.             instream.close();
  67.  
  68.             File file = new File(filepath);
  69.             if (!file.exists()) {
  70.                 throw new FileNotFoundException();
  71.             }
  72.  
  73.         } catch (FileNotFoundException e) {
  74.             e.printStackTrace();
  75.         } catch (IOException e) {
  76.             e.printStackTrace();
  77.         }
  78.     }
  79.  
  80.     private void checkFile(File dir) {
  81.         //directory does not exist, but we can successfully create it
  82.         if (!dir.exists() && dir.mkdirs()){
  83.             copyFiles();
  84.         }
  85.  
  86.         //The directory exists, but there is no data file in it
  87.         if(dir.exists()) {
  88.             String datafilepath = datapath+ "/tessdata/eng.traineddata";
  89.             File datafile = new File(datafilepath);
  90.             if (!datafile.exists()) {
  91.                 copyFiles();
  92.             }
  93.         }
  94.     }
  95.  
  96.     public void processImage(View view) {
  97.         String OCRresult = null;
  98.         mTess.setImage(image);
  99.         OCRresult = mTess.getUTF8Text();
  100.         TextView OCRTextView = (TextView) findViewById(R.id.OCRTextView);
  101.         OCRTextView.setText(OCRresult);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement