0

I am using rmtheis tess-two depencies in my project. Have read many tutorials but nothing helped me.

package com.example.shaur.ocrapp;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.googlecode.tesseract.android.TessBaseAPI;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private static int RESULT_LOAD_IMAGE = 1;

    Button button;
    ImageView image;
    TextView result;
    Bitmap bitmap;
    private static final int GALLERY = 4;

    private static String TESSBASE_PATH= Environment.getRootDirectory().getPath();
    private static final String DEFAULT_LANGUAGE = "eng";


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

        button = (Button) findViewById(R.id.button);
        image = (ImageView) findViewById(R.id.image);
        result = (TextView) findViewById(R.id.result);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");

                startActivityForResult(intent, GALLERY);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == GALLERY) {
            Uri uri = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
            } catch (IOException e) {
                e.printStackTrace();
            }
            image.setImageBitmap(bitmap);


            final TessBaseAPI baseApi = new TessBaseAPI();

            //Error HERE
            baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE);
            baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_LINE);
            baseApi.setVariable(TessBaseAPI.VAR_SAVE_BLOB_CHOICES, TessBaseAPI.VAR_TRUE);

            baseApi.setImage(bitmap);
            String outputText = baseApi.getUTF8Text();
            baseApi.end();

            Log.i("TEXT:",outputText);
            if(DEFAULT_LANGUAGE.equalsIgnoreCase("eng")){
                outputText = outputText.replaceAll("[^a-zA-Z0-9]+", " ");
            }
            result.setText(outputText);



        }
    }
}

I read somewhere set Data Path as getFilesDir()+ "/tesseract/". This method id not work. Then I tried PATH-> "/mnt/sdcard/tessract/"

My eng.traineddata is in the assets folder.

Log Message where Error Comes->

com.example.shaur.ocrapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.shaur.ocrapp, PID: 5736 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=Intent { dat=content://media/external/images/media/43105 flg=0x1 launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) }} to activity {com.example.shaur.ocrapp/com.example.shaur.ocrapp.MainActivity}: java.lang.IllegalArgumentException: Data path must contain subfolder tessdata! at android.app.ActivityThread.deliverResults(ActivityThread.java:4520) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4563) at android.app.ActivityThread.-wrap22(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1698) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) Caused by: java.lang.IllegalArgumentException: Data path must contain subfolder tessdata! at com.googlecode.tesseract.android.TessBaseAPI.init(TessBaseAPI.java:311) at com.googlecode.tesseract.android.TessBaseAPI.init(TessBaseAPI.java:284) at com.example.shaur.ocrapp.MainActivity.onActivityResult(MainActivity.java:73) at android.app.Activity.dispatchActivityResult(Activity.java:7295) at android.app.ActivityThread.deliverResults(ActivityThread.java:4516) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4563)  at android.app.ActivityThread.-wrap22(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1698)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6776)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

Log Message clearly tells "Caused by: java.lang.IllegalArgumentException: Data path must contain subfolder tessdata!". Where Should I create this directory. Is it my phone sdcard where I need to create a folder manually and name it as tesseract and add there a subfolder with tessdata having eng.traineddata.

Image of my directory view

If there is any other error in my code regarding tessaract do highlight and tell the correct method.

  • Links which I have tried so far ->http://imperialsoup.com/2016/04/29/simple-ocr-android-app-using-tesseract-tutorial/ , https://stackoverflow.com/questions/12877235/ocr-android-application-that-is-using-tesseract – Lumiere Vlogs Nov 14 '17 at 20:36
  • The stackoverflow answer confuses me why the path is "/mnt/sdcard/tesseract/tessdata" – Lumiere Vlogs Nov 14 '17 at 20:38

2 Answers2

1

Tesseract don't handle the assets folder, you need to copy the .datas to somewhere acessible like /data/Android/yourapppackage/assets/tesseract and use that path.

I did made the Tesseract work only, don't know if Tess-Two does handle that case.

Lets suppose you add the data to /assets/

Then in your code you get the Stream from assets and copy the file to inside the device in the path i mentioned, then in the tesseract impl you use it

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
0

I think you should use

private static final String TessBasePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/com.example.shaur.ocrapp/";

This worked for me!!

Thanos
  • 11
  • 5