costaricardo / tesseract-android-tools

Automatically exported from code.google.com/p/tesseract-android-tools
1 stars 0 forks source link

Error in loading eng.traineddata (3.01) using com.android.content.res.AssetManager() #40

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. I put the latest eng.traineddata file in the android "asset" folder
2. This is the code for copying the traineddata file from the "asset" folder to 
the sd-card.

        File tessdata=new File(DEFAULT_DATA_PATH+"tessdata/"+DEFAULT_LANG+".traineddata");
        if(!tessdata.exists()){
            try {
                tessdata.createNewFile();
                InputStream in=packageResource.getAssets().open(DEFAULT_LANG+".traineddata");

                OutputStream out=new FileOutputStream(tessdata);
                byte[] buffer = new byte[1024];
                int read;
                while((read = in.read(buffer)) != -1){
                  out.write(buffer, 0, read);
                }

                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("tag", e.getMessage());
            }

        }
3.

What is the expected output? What do you see instead?
   I expect to copy bytes from the eng.traineddata file using inputstream.read(byte[]) but an IOException come out from the native level.I tried to do the same thing copying bytes from the file using java.io.FileInputStream() and everything is OK.I think the error concerns with Android.

What version of the product are you using? On what operating system?
  tesseract-android-tools....1.0
  tesseract..................3.01
  leptonica-.................1.69

    Windows 7 32 bits

Please provide any additional information below.
     The following are the Log-cat output on the IOException:
(My project package name is"com.balancecharger")

10-24 12:18:27.108: W/System.err(8898): java.io.IOException
10-24 12:18:27.118: W/System.err(8898):     at 
android.content.res.AssetManager.readAsset(Native Method)
10-24 12:18:27.118: W/System.err(8898):     at 
android.content.res.AssetManager.access$700(AssetManager.java:36)
10-24 12:18:27.118: W/System.err(8898):     at 
android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
10-24 12:18:27.148: W/System.err(8898):     at 
com.balancecharger.Converter.convertBitmapToString(Converter.java:41)
10-24 12:18:27.179: W/System.err(8898):     at 
com.balancecharger.MainActivity$2.onClick(MainActivity.java:144)
10-24 12:18:27.188: W/System.err(8898):     at 
android.view.View.performClick(View.java:2408)
10-24 12:18:27.188: W/System.err(8898):     at 
android.view.View$PerformClick.run(View.java:8816)
10-24 12:18:27.208: W/System.err(8898):     at 
android.os.Handler.handleCallback(Handler.java:587)
10-24 12:18:27.208: W/System.err(8898):     at 
android.os.Handler.dispatchMessage(Handler.java:92)
10-24 12:18:27.228: W/System.err(8898):     at 
android.os.Looper.loop(Looper.java:123)
10-24 12:18:27.248: W/System.err(8898):     at 
android.app.ActivityThread.main(ActivityThread.java:4627)
10-24 12:18:27.268: W/System.err(8898):     at 
java.lang.reflect.Method.invokeNative(Native Method)
10-24 12:18:27.268: W/System.err(8898):     at 
java.lang.reflect.Method.invoke(Method.java:521)
10-24 12:18:27.278: W/System.err(8898):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-24 12:18:27.298: W/System.err(8898):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-24 12:18:27.308: W/System.err(8898):     at 
dalvik.system.NativeStart.main(Native Method)

Original issue reported on code.google.com by minthant...@gmail.com on 24 Oct 2012 at 8:04

GoogleCodeExporter commented 9 years ago
One thing I left to mention is that the IOException comes out where 
in.read(bytes);..And the Exception will not occur when I use an image file in 
the place of eng.traineddata file and everything runs OK.

Original comment by minthant...@gmail.com on 24 Oct 2012 at 8:08

GoogleCodeExporter commented 9 years ago
what's the packageResource in sentence:
InputStream in=packageResource.getAssets().open(DEFAULT_LANG+".traineddata");

I tried with
 InputStream in=getAssets().open(DEFAULT_LANG+".traineddata");
and it turns out alright.

Original comment by xinton.B...@gmail.com on 24 Oct 2012 at 9:27

GoogleCodeExporter commented 9 years ago
  I put all the above codes into only one function in a separate class so that I have to pass Resources() object (here packageResource) from main Activity() class into the function.... I've tried both with getResources().getAssets().open(DEFAULT_LANG+".traineddata"); and getResources().openRawResource(R.id.eng);. ...Both of them gave the same IOException() on calling the in.read(bytes) function.

Original comment by minthant...@gmail.com on 25 Oct 2012 at 9:24

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I've decided to put the "eng.trainedata" file bytes into an array of C++ file 
and embed into the compiled library file.But the file size is more than 3MB.I 
haven't tested but I'm worried about the performance issue.Any ideas please 
help!

Original comment by minthant...@gmail.com on 30 Oct 2012 at 9:39

GoogleCodeExporter commented 9 years ago
You need to put your traineddata file in "asset/tessdata". Looks like thats 
where you are trying to copy from.

1. I put the latest eng.traineddata file in the android "asset" folder
2. This is the code for copying the traineddata file from the "asset" folder to 
the sd-card. 

        File tessdata=new File(DEFAULT_DATA_PATH+"tessdata/"+DEFAULT_LANG+".traineddata");

Original comment by Bibh...@gmail.com on 19 Nov 2012 at 6:39