2015-11-30 114 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(); 
}