1

I try to run a simple OCR with tessera. My image is very simple as shown below:

enter image description here

So if it works fine, the output is an extracted text as: SONY TV ...

When I run the program on Android, I get the following problem with line: baseApi.init(myDir, "eng");

as it says it couldn't find the source but as shown in the image it is in tssD/tessdata/eng.traineddata.

enter image description here

Here is my original code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String myDir= "tssD/tessdata/eng.traineddata";
        String imagePath = "myImages/Remote1.bmp";

        ImageView image = (ImageView) findViewById(R.id.imageView1);
        Bitmap bMap = BitmapFactory.decodeFile(imagePath);

        TessBaseAPI baseApi = new TessBaseAPI();
        baseApi.init(myDir, "eng"); 
        baseApi.setImage(bMap);
        String recognizedText = baseApi.getUTF8Text(); 
        EditText text = (EditText) findViewById(R.id.editText1);
        text.setText(recognizedText);   
        image.setImageBitmap(bMap);
        baseApi.end();
    }
farzin parsa
  • 537
  • 2
  • 9
  • 33
  • May be reading something wrong, or unaware of the process, but why are you calling toString on a string? – zgc7009 Apr 11 '14 at 23:42
  • That doesnt make any difference,as it is a string – farzin parsa Apr 11 '14 at 23:58
  • I know, which is why I am wondering why you are doing it. Seems redundant. Not like it is a big deal just extra code – zgc7009 Apr 12 '14 at 00:17
  • Here is not the problem,it is already gone – farzin parsa Apr 12 '14 at 00:20
  • I didnt know if there was an explicit reason for using .toString(), was wondering for my own reference. Thought it might just be something weird. Glad you got it to work – zgc7009 Apr 12 '14 at 00:22
  • Look here,http://stackoverflow.com/questions/12877235/android-ocr-app-that-using-tesseract you might be using the wrong directory – zgc7009 Apr 12 '14 at 00:32
  • Thanks dude. The thing is : I have already included it in my code directory which is **String myDir= "tssD/** as I have circled in red in image shown above. what if somebody doesnt want it to have it on sdcard – farzin parsa Apr 12 '14 at 00:43

1 Answers1

3

The tssD folder will not be built into your APK and will not be installed on your device. If you want to include miscellaneous files with your APK, you'll need to put them into assets or res/raw.

You can open a file in assets for reading with something like:

InputStream input = assetManager.open("sample.txt");

The reference page for the AssetManager is here.

scottt
  • 8,301
  • 1
  • 31
  • 41
  • I put my tessdata into assets folder but stil the error persists: assets//tessdata Also, assestManager does not work on my android project although I imported "java.lang.ref.*" & "java.lang.*" – farzin parsa Apr 16 '14 at 23:15
  • The import for the AssetManager class is "android.content.res.AssetManager". How are you accessing the file? What sort of error message are you getting? – scottt Apr 17 '14 at 02:19
  • The error: Cant find source file I tried this also: AssetManager assetManager = getAssets(); InputStream in = assetManager.open("tessdata/eng.traineddata"); But for getAssests() if I put it outside an activity class it is undefined.{My function is out of an activity class} – farzin parsa Apr 17 '14 at 19:54
  • Ah, most of the Android framework API requires access to a context. The usual approach if you're in another class, is to pass in and save the current activity or application context in its constructor. To pass in an activity context you'd use something like 'MyClass(this);' to create your class; for an application context, you'd use something like 'MyClass(getApplicationContext());'. Your constructor would look like: 'public MyClass(Context context) { this.context = context; }' – scottt Apr 18 '14 at 01:22