2015-11-30 78 views

回答

0

我已经从库中构建了jar文件。按照my gist下载和使用的库:

  1. 名为libs.zip下面的链接下载的正方体库。 https://www.dropbox.com/s/9fwqz88sck3xlk4/libs.zip?dl=0

  2. 解压zip文件夹。

    • 如果您使用的是Eclipse,那么将libs文件夹中的所有文件和文件夹复制到项目中的libs文件夹中。
    • 如果您使用的是Android Studio,然后将所有文件夹从libs文件夹复制到项目中的src/main/jniLibs文件夹,并将 classes.jar复制到libs文件夹。
  3. 在您的下载文件夹中添加包含文本的图像并给出名称a.png。

  4. 在项目中的资产文件夹内创建名为tessdata的文件夹。

  5. 从下面的链接下载名为eng.traineddata的文件,并将其复制到第4步创建的tessdata文件夹中。 https://www.dropbox.com/s/7xdnfzp8qsy4ll9/eng.traineddata?dl=0

  6. 添加权限体现 “android.permission.WRITE_EXTERNAL_STORAGE”

  7. 复制和粘贴下面的代码。

try{ 
    bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/download/a.png"); 

    // _path = path to the image to be OCRed 
    ExifInterface exif = new ExifInterface(Environment.getExternalStorageDirectory().getAbsolutePath()+"/download/a.png"); 
    int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
ExifInterface.ORIENTATION_NORMAL); 

    int rotate = 0; 

    switch (exifOrientation) { 
    case ExifInterface.ORIENTATION_ROTATE_90: 
     rotate = 90; 
     break; 
    case ExifInterface.ORIENTATION_ROTATE_180: 
     rotate = 180; 
     break; 
    case ExifInterface.ORIENTATION_ROTATE_270: 
     rotate = 270; 
     break; 
    } 

    if (rotate != 0) { 
     int w = bitmap.getWidth(); 
     int h = bitmap.getHeight(); 

     // Setting pre rotate 
     Matrix mtx = new Matrix(); 
     mtx.preRotate(rotate); 

     // Rotating Bitmap & convert to ARGB_8888, required by tess 
     bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false); 
    } 
    bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 
    TessBaseAPI baseApi = new TessBaseAPI(); 

    // tesseract reads language from tesseract folder, create it if not exists. 
    File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tesseract/tessdata"); 
    if(!f.exists()){ 
     f.mkdirs(); 
    } 

    // copy the eng lang file from assets folder if not exists. 
    File f1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tesseract/tessdata/eng.traineddata"); 
    if(!f1.exists()){ 
     InputStream in = getAssets().open("tessdata/eng.traineddata"); 
     FileOutputStream fout = new FileOutputStream(f1); 
     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = in.read(buf)) > 0) { 
      fout.write(buf, 0, len); 
     } 
     in.close(); 
     fout.close(); 
    } 

    // DATA_PATH = Path to the storage and data path must contain tessdata subdirectory 
    // lang = for which the language data exists, usually "eng" 
    // Eg. baseApi.init("/mnt/sdcard/tesseract/tessdata/eng.traineddata", "eng"); 
    baseApi.init(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tesseract", "eng"); 

    baseApi.setImage(bitmap); 
    String recognizedText = baseApi.getUTF8Text(); 
    baseApi.end(); 
    Toast.makeText(getApplicationContext(), recognizedText, Toast.LENGTH_LONG).show(); 
    System.out.println("Text is>>>>>>>>>>>>>>>>>" + recognizedText); 
}catch(Exception e){ 
    e.printStackTrace(); 
}