package com.example.patrice.mysql01;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.googlecode.tesseract.android.TessBaseAPI;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

// http://imperialsoup.com/2016/04/29/simple-ocr-android-app-using-tesseract-tutorial/

public class OCR01 extends AppCompatActivity
{

    Bitmap image;
    private TessBaseAPI mTess;
    String datapath = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ocr01_layout);

        //init image
        image = BitmapFactory.decodeResource(getResources(), R.drawable.snap_ocr);

        //initialize Tesseract API
        String language = "eng";
        datapath = getFilesDir()+ "/tesseract/";
        mTess = new TessBaseAPI();

        checkFile(new File(datapath + "tessdata/"));

        mTess.init(datapath, language);



        }

public void processImage(View view){
        String OCRresult = null;
        mTess.setImage(image);
        OCRresult = mTess.getUTF8Text();
        TextView OCRTextView = (TextView) findViewById(R.id.OCRTextView);
        OCRTextView.setText(OCRresult);
        }

private void checkFile(File dir) {
        if (!dir.exists()&& dir.mkdirs()){
        copyFiles();
        }
        if(dir.exists()) {
        String datafilepath = datapath+ "/tessdata/eng.traineddata";
        File datafile = new File(datafilepath);

        if (!datafile.exists()) {
        copyFiles();
        }
        }
        }

private void copyFiles() {
        try {
        String filepath = datapath + "/tessdata/eng.traineddata";
        AssetManager assetManager = getAssets();

        InputStream instream = assetManager.open("tessdata/eng.traineddata");
        OutputStream outstream = new FileOutputStream(filepath);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = instream.read(buffer)) != -1) {
        outstream.write(buffer, 0, read);
        }


        outstream.flush();
        outstream.close();
        instream.close();

        File file = new File(filepath);
        if (!file.exists()) {
        throw new FileNotFoundException();
        }
        } catch (FileNotFoundException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
        }

/*

public class OCR01 extends AppCompatActivity
{

    Bitmap image;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        TessBaseAPI baseApi = new TessBaseAPI();

        //Environment.getExternalStorageDirectory().getAbsolutePath()
        //static String strSDCardPathName = Environment.getExternalStorageDirectory()

         //       /mnt/sdcard/

        baseApi.init("/storage/emulated/0/tesseract/tessdata/eng.traineddata", "eng");
        //init image
        image = BitmapFactory.decodeResource(getResources(), R.drawable.snap_ocr);

        baseApi.setImage(image);
        String recognizedText = baseApi.getUTF8Text();
        baseApi.end();

    }

}
*/