2013-04-26 78 views
1

有人可以解釋究竟發生了什麼以及如何糾正它。所以我知道tesseract必須有一個圖像的目標,可能這將是我已經捕獲並試圖保存的位圖。該保存的位置將是我的_path變量。現在什麼是用於tesseract的DATA_PATH?圖像是否需要存儲在名爲'tesseract'的文件夾中?我是否創建該文件夾並在其中存儲某種培訓?我正在尋找一個解釋而不是代碼示例。Tesseract - 幫我理解數據路徑

http://gaut.am/making-an-ocr-android-app-using-tesseract/ - 我試圖按照本教程,並檢查其他人試圖瞭解所有人使用的路徑。

public class MainActivity extends Activity { 
private static ImageView imageView; 
protected String _path; 
// protected static Bitmap bit; 
static File myDir; 
protected static Bitmap mImageBitmap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    this.imageView = (ImageView) this.findViewById(R.id.imageView1); 
    Button photoButton = (Button) this.findViewById(R.id.button1); 

    _path = Environment.getExternalStorageDirectory() + "/images/test.bmp"; 

    Toast t = Toast.makeText(getApplicationContext(), "HEELLLLLLOO", 100000); 
    t.show(); 
    photoButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // CALL THE PICTURE 
      dispatchTakePictureIntent(0); 

     } 
    }); 
} 

private void handleSmallCameraPhoto(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    mImageBitmap = (Bitmap) extras.get("data"); 
    imageView.setImageBitmap(mImageBitmap); 

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 4; 

    Bitmap bitmap = BitmapFactory.decodeFile(_path, options); 

    imageView.setImageBitmap(bitmap); 

    //_path = path to the image to be OCRed 
    ExifInterface exif; 
    try { 
     exif = new ExifInterface(_path); 

    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(); 
    // DATA_PATH = Path to the storage 
    // lang for which the language data exists, usually "eng" 
    baseApi.init(_path, "eng"); //THIS SHOULD BE DATA_PATH ? 
    baseApi.setImage(bitmap); 
    String recognizedText = baseApi.getUTF8Text(); 
    System.out.println(recognizedText); 
    baseApi.end(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
    handleSmallCameraPhoto(data); 
} 

private void dispatchTakePictureIntent(int actionCode) { 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(takePictureIntent, actionCode); 
} 

protected static void identifyunicode() { 
    // DATA_PATH = Path to the storage 
    // lang for which the language data exists, usually "eng" 

    /* 
    * TessBaseAPI baseApi = new TessBaseAPI(); 
    * baseApi.init(myDir.toString(), "eng"); // myDir + // 
    * "/tessdata/eng.traineddata" // must be present baseApi.setImage(bit); 
    * String recognizedText = baseApi.getUTF8Text(); // Log or otherwise // 
    * display this // string... baseApi.end(); 
    */ 
} 

}

回答

1

DataPath是你從Assets複製您的tessdata文件的路徑。

import java.io.File; 
import java.util.ArrayList; 
import android.os.Environment; 

public class Utils { 

    public static boolean isSDCardMounted() { 
     boolean isMounted = false; 
     String state = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      isMounted = true; 
     } else if (Environment.MEDIA_BAD_REMOVAL.equals(state)) { 
      isMounted = false; 
     } else if (Environment.MEDIA_CHECKING.equals(state)) { 
      isMounted = false; 
     } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
      isMounted = false; 
     } else if (Environment.MEDIA_NOFS.equals(state)) { 
      isMounted = false; 
     } else if (Environment.MEDIA_REMOVED.equals(state)) { 
      isMounted = false; 
     } else if (Environment.MEDIA_UNMOUNTABLE.equals(state)) { 
      isMounted = false; 
     } else if (Environment.MEDIA_UNMOUNTED.equals(state)) { 
      isMounted = false; 
     } 
     return isMounted; 
    } 

    public static boolean isDirectoryExists(final String filePath) { 
     boolean isDirectoryExists = false; 
     File mFilePath = new File(filePath); 
     if(mFilePath.exists()) { 
      isDirectoryExists = true; 
     } else { 
      isDirectoryExists = mFilePath.mkdirs(); 
     } 
     return isDirectoryExists; 
    } 

    public static boolean deleteFile(final String filePath) { 
     boolean isFileExists = false; 
     File mFilePath = new File(filePath); 
     if(mFilePath.exists()) { 
      mFilePath.delete(); 
      isFileExists = true; 
     } 
     return isFileExists; 
    } 

    public static String getDataPath() { 
     String returnedPath = ""; 
     final String mDirName = "tesseract"; 
     final String mDataDirName = "tessdata"; 
     if(isSDCardMounted()) { 
      final String mSDCardPath = Environment.getExternalStorageDirectory() + File.separator + mDirName; 
      if(isDirectoryExists(mSDCardPath)) { 
       final String mSDCardDataPath = Environment.getExternalStorageDirectory() + File.separator + mDirName + 
         File.separator + mDataDirName; 
       isDirectoryExists(mSDCardDataPath); 
       return mSDCardPath; 
      } 
     } 
     return returnedPath; 
    } 
} 

Activity

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.res.AssetManager; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.ColorMatrix; 
import android.graphics.ColorMatrixColorFilter; 
import android.graphics.Matrix; 
import android.graphics.Paint; 
import android.media.ExifInterface; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.View; 
import android.widget.TextView; 
import com.googlecode.tesseract.android.TessBaseAPI; 

public class AndroidCommonTest extends Activity { 
    private static final String TAG = AndroidCommonTest.class.getSimpleName(); 
    private TextView txtGotTime = null; 
    private final int START_CODE = 101; 
    private String mDirPath = null; 
    private Uri mOutPutUri = null; 
    private static final String lang = "eng"; 
    private String mPath = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     txtGotTime = (TextView) findViewById(R.id.txtGotTime); 

     mDirPath = Utils.getDataPath(); 
     mPath = mDirPath + File.separator + "test.jpg"; 
     android.util.Log.i(TAG, "mDirPath: " + mDirPath + " mPath: " + mPath); 

     if (!(new File(mDirPath + File.separator + "tessdata" + File.separator + lang + ".traineddata")).exists()) { 
      try { 
       AssetManager assetManager = getAssets(); 
       InputStream in = assetManager.open("tessdata" + File.separator + lang + ".traineddata"); 
       OutputStream out = new FileOutputStream(mDirPath + File.separator 
         + "tessdata" + File.separator + lang + ".traineddata"); 
       byte[] buf = new byte[8024]; 
       int len; 
       while ((len = in.read(buf)) > 0) { 
        out.write(buf, 0, len); 
       } 
       in.close(); 
       out.close(); 
      } catch (IOException e) { 
       android.util.Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString()); 
      } 
     } else { 
      processImage(mDirPath + File.separator + "six.jpg", 0); 
     } 
    } 

    public void getTime(View view) { 
     android.util.Log.i(TAG, "mDirPath: " + mDirPath + " mPath: " + mPath); 
     if(mDirPath != null && mDirPath.length() > 0) { 
      mOutPutUri = Uri.fromFile(new File(mPath)); 
      Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mOutPutUri); 
      startActivityForResult(intent, START_CODE); 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(requestCode == START_CODE) { 
      if(resultCode == Activity.RESULT_OK) { 
       int rotation = -1; 
       long fileSize = new File(mPath).length(); 
       android.util.Log.i(TAG, "fileSize " + fileSize); 

       //Suppose Device Supports ExifInterface 
       ExifInterface exif; 
       try { 
        exif = new ExifInterface(mPath); 
        int exifOrientation = exif.getAttributeInt(
          ExifInterface.TAG_ORIENTATION, 
          ExifInterface.ORIENTATION_NORMAL); 
        switch (exifOrientation) { 
         case ExifInterface.ORIENTATION_ROTATE_90 : 
          rotation = 90; 
          break; 
         case ExifInterface.ORIENTATION_ROTATE_180 : 
          rotation = 180; 
          break; 
         case ExifInterface.ORIENTATION_ROTATE_270 : 
          rotation = 270; 
          break; 
         case ExifInterface.ORIENTATION_NORMAL: 
         case ExifInterface.ORIENTATION_UNDEFINED: 
          rotation = 0; 
          break; 
        } 
        android.util.Log.i(TAG, "Exif:rotation " + rotation); 

        if (rotation != -1) { 
         processImage(mPath, rotation); 
        } else { 
         //Device Does Not Support ExifInterface 
         Cursor mediaCursor = getContentResolver().query(mOutPutUri, 
           new String[] { MediaStore.Images.ImageColumns.ORIENTATION, 
           MediaStore.MediaColumns.SIZE }, 
           null, null, null); 
         if (mediaCursor != null && mediaCursor.getCount() != 0) { 
          while(mediaCursor.moveToNext()){ 
           long size = mediaCursor.getLong(1); 
           android.util.Log.i(TAG, "Media:size " + size); 
           if(size == fileSize){ 
            rotation = mediaCursor.getInt(0); 
            break; 
           } 
          } 
          android.util.Log.i(TAG, "Media:rotation " + rotation); 
          processImage(mPath, rotation); 
         } else { 
          android.util.Log.i(TAG, "Android Problem"); 
          txtGotTime.setText("Android Problem"); 
         } 
        } 
       } 
       catch (IOException exception) { 
        exception.printStackTrace(); 
       } 
      } else if(resultCode == Activity.RESULT_CANCELED) { 
       android.util.Log.i(TAG, "RESULT_CANCELED"); 
       txtGotTime.setText("RESULT_CANCELED"); 
      } 
     } 
    } 

    private void processImage(final String filePath, final int rotation) { 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 2; 
     options.inPurgeable = true; 
     Bitmap bitmap = BitmapFactory.decodeFile(filePath, options); 
     if (bitmap != null) { 
      int width = bitmap.getWidth(); 
      int height = bitmap.getHeight(); 
      Matrix matrix = new Matrix(); 
      matrix.postRotate(rotation); 
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false); 
      bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 

      TessBaseAPI baseApi = new TessBaseAPI(); 
      baseApi.setDebug(true); 
      baseApi.init(mDirPath, lang); 
      baseApi.setPageSegMode(100); 
      baseApi.setPageSegMode(7); 
      baseApi.setImage(bitmap); 
      String recognizedText = baseApi.getUTF8Text(); 
      android.util.Log.i(TAG, "recognizedText: 1 " + recognizedText); 
      baseApi.end(); 
      if(lang.equalsIgnoreCase("eng")) { 
       recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " "); 
      } 
      android.util.Log.i(TAG, "recognizedText: 2 " + recognizedText.trim()); 
      txtGotTime.setText(recognizedText.trim()); 
     } 
    } 

    private void saveImageAndroid(final Bitmap passedBitmap) { 
     try { 
      FileOutputStream mFileOutStream = new FileOutputStream(mDirPath + File.separator + "savedAndroid.jpg"); 
      passedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, mFileOutStream); 
      mFileOutStream.flush(); 
      mFileOutStream.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

tessdata文件夾中Assets

不要忘了給參考路徑Tess Library

謝謝。

+0

好的,你說把tessdata文件夾放在Assets中,tessdata文件夾在哪裏?我正在查看tesstwo目錄,但我沒有看到它。 – user1088595 2013-04-26 10:32:52

+0

啊我下載了一個包含tessdata文件夾的Tesseract訓練集,但現在我的應用程序在運行時崩潰了。 「Asset path /data/app/com.example.cameraocr-2.apk既不是目錄也不是文件(type = 1) – user1088595 2013-04-26 10:55:51

+0

@ user1088595您是否看到我的代碼?我正在從'eng.traineddata'文件中複製該文件資產給我創建的Dir。 – 2013-04-29 03:30:14