package gt.otomat;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.googlecode.tesseract.android.TessBaseAPI;
import com.googlecode.tesseract.android.*;
import android.graphics.Bitmap;

public class otomat extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gt_otomat);


        public String detectText(Bitmap bitmap) {

            TessDataManager.initTessTrainedData(context);
            TessBaseAPI tessBaseAPI = new TessBaseAPI();

            String path = "/mnt/sdcard/packagename/tessdata/tr.traineddata";

            tessBaseAPI.setDebug(true);
            tessBaseAPI.init(path, "tr");



            tessBaseAPI.setImage(bitmap);

            String text = tessBaseAPI.getUTF8Text();

            Log.d(TAG, "Got data: " + result);
            tessBaseAPI.end();

            return text;
        }

    }
}

ı used teserract ocr library in this code.

but I get errors about cannot resolve symbol.like bitmap,text,TessDataManager.

how can I solve this problem? ı have tried every ways.but I couldn'n solve this problem

thank you for your helps...

3 Answers

0
ElliotM On

Looks like you have a method declaration inside onCreate - move it out otherwise the IDE won't know what you're doing.

public class otomat extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.gt_otomat);

    }

   //Move this function out of onCreate()
    public String detectText(Bitmap bitmap) {

        TessDataManager.initTessTrainedData(context);
        TessBaseAPI tessBaseAPI = new TessBaseAPI();

        String path = "/mnt/sdcard/packagename/tessdata/tr.traineddata";

        tessBaseAPI.setDebug(true);
        tessBaseAPI.init(path, "tr");



        tessBaseAPI.setImage(bitmap);

        String text = tessBaseAPI.getUTF8Text();

        Log.d(TAG, "Got data: " + result);
        tessBaseAPI.end();

        return text;
    }
}

If you're in Android Studio, any red squiggles can usually be resolved by pushing Alt+Enter on them and get a list of options to resolve, usually importing the class you need

0
Rajshree Tiwari On

In your code, you need to create TessDataManager.java class which will contains your folder directories, i got the reference from the following link:-

https://www.programcreek.com/java-api-examples/?code=simplezhli/Tesseract-OCR-Scanner/Tesseract-OCR-Scanner-master/app/src/main/java/com/zl/tesseract/scanner/tess/TessEngine.java

May be it will help you.

TessDataManager.Java

public class TessDataManager {

static final String TAG = "DBG_" + TessDataManager.class.getName();

private static final String tessdir = "tesseract";
private static final String subdir = "tessdata";
private static final String filename = "eng.traineddata";

private static String trainedDataPath;

private static String tesseractFolder;

public static String getTesseractFolder() {
    return tesseractFolder;
}

public static String getTrainedDataPath(){
    return initiated ? trainedDataPath : null;
}

private static boolean initiated;

public static void initTessTrainedData(Context context){

    if(initiated){
        return;
    }

    File appFolder = context.getFilesDir();
    File folder = new File(appFolder, tessdir);
    if(!folder.exists()){
        folder.mkdir();
    }

    tesseractFolder = folder.getAbsolutePath();

    File subfolder = new File(folder, subdir);
    if(!subfolder.exists()){
        subfolder.mkdir();
    }

    File file = new File(subfolder, filename);
    trainedDataPath = file.getAbsolutePath();
    Log.d(TAG, "Trained data filepath: " + trainedDataPath);

    if(!file.exists()) {

        try {
            FileOutputStream fileOutputStream;
            byte[] bytes = readRawTrainingData(context);
            if (bytes == null){
                return;
            }

            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(bytes);
            fileOutputStream.close();
            initiated = true;
            Log.d(TAG, "Prepared training data file");
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Error opening training data file\n" + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "Error opening training data file\n" + e.getMessage());
        }
    }
    else{
        initiated = true;
    }
}

private static byte[] readRawTrainingData(Context context){

    try {
        InputStream fileInputStream = context.getResources()
                .openRawResource(R.raw.eng_traineddata);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        byte[] b = new byte[1024];

        int bytesRead;

        while (( bytesRead = fileInputStream.read(b))!=-1){
            bos.write(b, 0, bytesRead);
        }

        fileInputStream.close();

        return bos.toByteArray();

    } catch (FileNotFoundException e) {
        Log.e(TAG, "Error reading raw training data file\n"+e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e(TAG, "Error reading raw training data file\n" + e.getMessage());
    }

    return null;
}
 }

TestActivity.class

 public class TestActivity extends AppCompatActivity {
 private Context context;

@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.test);
    context = this;
   **// here pass your bitmap value.**
    detectText(bitmap);
    }

public String detectText(Bitmap bitmap) {

    TessDataManager.initTessTrainedData(context);
    TessBaseAPI tessBaseAPI = new TessBaseAPI();

    String path = "/mnt/sdcard/packagename/tessdata/tr.traineddata";

    tessBaseAPI.setDebug(true);
    tessBaseAPI.init(path, "tr");


    tessBaseAPI.setImage(bitmap);

    String text = tessBaseAPI.getUTF8Text();

    Log.d(TAG, "Got data: " + result);
    tessBaseAPI.end();

    return text;
}

}

In your build.gradle, update your library:-

 implementation 'com.rmtheis:tess-two:9.0.0'

Hope it may help you. :)

0
olajide On

If you are using a third-party library, Make sure you are connected to a good network

Try adding Google maven repository into app's build.gradle file:

repositories {
    maven { url "https://maven.google.com" }
}

If step 1 won't work, remove gradle from your project root folder and rebuild your project again.

Try another

Goto >file -> other settings -> Under build, execution, and deployment -> you will see Gradle. uncheck the checkbox offline work