Android Open Source - OrzEye O C R Tool






From Project

Back to project page OrzEye.

License

The source code is released under:

GNU General Public License

If you think the Android project OrzEye listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.dylan.orzeye.ocr;
/*from   w w  w  .  j a  va  2  s.com*/
import java.util.List;

import com.googlecode.tesseract.android.TessBaseAPI;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Environment;

public class OCRTool {
  private TessBaseAPI tessOCRApi = null;

  public static final String DATA_PATH = Environment
      .getExternalStorageDirectory().toString() + "/OrzEye/";

  public static final String lang = "eng";

  public OCRTool() {
    initOcrEngine();
  }

  public String OCRStart(Bitmap ocrBitmap) {
    try {
      
      Rect centerObjectRect = getCenterObjectRect(ocrBitmap);
      if(centerObjectRect == null) {
        return "";
      }
      tessOCRApi.setImage(Bitmap.createBitmap(ocrBitmap,
          centerObjectRect.left,
          centerObjectRect.top, 
          centerObjectRect.width(),
          centerObjectRect.height()));
      String recognizedText = tessOCRApi.getUTF8Text();
      return recognizedText;
    } catch (Exception e) {
    
    }
    tessOCRApi.end();
    return "";

  }

  private Rect getCenterObjectRect(Bitmap ocrBitmap) {
    tessOCRApi.setImage(ocrBitmap);
    List<Rect> wordsRects = tessOCRApi.getWords().getBoxRects();
    int objectIndex = 0;
    for(Rect rect :wordsRects) {
      if(rect.contains(ocrBitmap.getWidth()/2, ocrBitmap.getHeight()/2)) {
        break;
      }
      objectIndex ++;  
    }
    if(objectIndex == wordsRects.size()) {
      return null;
    }
    else {
      return wordsRects.get(objectIndex);
    }
  }

  private void initOcrEngine() {
    tessOCRApi = new TessBaseAPI();
    tessOCRApi.setDebug(true);
    try {
      tessOCRApi.init(DATA_PATH, lang);
    } catch (IllegalArgumentException e) {
      tessOCRApi = null;
    }
  }

  public boolean isOCREngineReasy() {
    return tessOCRApi == null ? false : true;
  }
}




Java Source Code List

com.dylan.orzeye.CameraActivity.java
com.dylan.orzeye.DrawRectView.java
com.dylan.orzeye.SplashActivity.java
com.dylan.orzeye.dictionary.DictionaryTool.java
com.dylan.orzeye.dictionary.YoudaoJsonParser.java
com.dylan.orzeye.dictionary.YoudaoTranslaterAPIConnection.java
com.dylan.orzeye.dictionary.YoudaoTranslater.java
com.dylan.orzeye.image.ImageProcessTool.java
com.dylan.orzeye.ocr.OCRTool.java