2012-05-15 127 views
-1

我正在开发一个使用tesseract android库函数的应用程序。我成功地编译了tesseract库并导入到我的工作区中。然后我的库引用添加到我的app.But这个方法是我写的原因力闭合:android tesseract ocr force close

public static String BitmapOku (Bitmap image){ 
      Bitmap image2=image.copy(Bitmap.Config.ARGB_8888, true); 
    TessBaseAPI baseApi = new TessBaseAPI(); 
    baseApi.init("/mnt/sdcard/tessdata/eng.traineddata", "eng"); 
    baseApi.setImage(image2); 
    String recognizedText = baseApi.getUTF8Text(); 
    baseApi.end(); 
return recognizedText; 

我的问题是---“做什么的问题我有一个正方体的Android应用OCR考虑和做我必须在我的应用中使用leptonica工具来处理图像处理的功能 - 例如旋转(因为我使用Opencv 2.4.0)?

+0

如果我没有弄错,图片必须是.tiff格式。 – sschrass

回答

0

有时,即使将语言文件插入到SD卡中,也有可能无法正确检测到。所以为了在SD卡上有效地建立文件,下面的代码可能是有用的。

String dirc = Environment.getExternalStorageDirectory()。getPath()。toString();

 String[] paths = new String[] { dirc,dirc + "/tessdata/" }; 

      for (String path :paths) { 
      File dir = new File(path); 
      if (!dir.exists()) { 
       if (!dir.mkdirs()) { 
        System.out.println("Creation of directory on sdcard failed"); 

       } else { 
        System.out.println("Creation of directory on sdcard successful"); 
       } 
      } 

     } 

     // lang.traineddata file with the app (in assets folder) 
       // You can get them at: 
       // http://code.google.com/p/tesseract-ocr/downloads/list 
       // This area needs work and optimization 
       if (!(new File(dirc + "/tessdata/" + "eng.traineddata")).exists()) { 
        try { 

         AssetManager assetManager = getAssets(); 
         InputStream in = assetManager.open("tessdata/eng.traineddata"); 
         //GZIPInputStream gin = new GZIPInputStream(in); 
         OutputStream out = new FileOutputStream(dirc + "/tessdata/eng.traineddata"); 

         // Transfer bytes from in to out 
         byte[] buf = new byte[1024]; 
         int len; 
         //while ((lenf = gin.read(buff)) > 0) { 
         while ((len = in.read(buf)) > 0) { 
          out.write(buf, 0, len); 
         } 
         in.close(); 
         //gin.close(); 
         out.close(); 

         System.out.println("copied eng.traineddata");; 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 

在这里,语言文件eng.traineddata被添加到项目中的assets文件夹中。 在资产文件夹中,创建一个tessdata文件夹,并把文件夹中的eng.traineddata ..

希望这可以解决您的问题。