android中ocr解决方案(tesseract)
android应用中ocr的解决方案大致有两种,而采用最多的还是tesseract.小弟就在这里将我最近两天解决思路写下来,如有缺陷,欢迎拍砖:
有两种解决方案,一种是采用tesseract cloud-service,这钟是把图片信息发送到云端,然后获得图片分析数据;第二种就是不用联网,本地化分析图片上信息。我就说说第二种,第一种我会在最后给大家一个链接(文章很不错)。
搜先就是下载Tesseract native android library.这里有两个链接,你选哪个链接都可以:
a.svn checkout http://tesseract-android-tools.googlecode.com/svn/trunk/ tesseract-android-tools。(如果不能checkout到,废话别说就到官方上下:http://code.google.com/p/tesseract-android-tools/)
b.可能上面一个下载后编译有些人会遇到一些问题,比如找不到jgep库,编译不成功。所以有了这个项目:git clone git://github.com/rmtheis/tess-two.git (这个包里面内容太多,不过也省得下那么多库了)
这里先说采用第一个源下载:下载成功后,打开README文件,做下修改(如下):
git clone git://android.git.kernel.org/platform/external/jpeg.git libjpeg
修改为:
git clone https://android.googlesource.com/platform/external/jpeg libjpeg
n
对于第二个源下载,由于里面没有README文件,操作命令如下:
cd <project-directory>/tess-two
export TESSERACT_PATH=${PWD}/external/tesseract-3.01
export LEPTONICA_PATH=${PWD}/external/leptonica-1.68
export LIBJPEG_PATH=${PWD}/external/libjpeg
ndk-build
android update project --path .
ant release
最终两个都得到你想要的libs里面的so文件和src里面的对so文件的封装类。这个就是我们开发所用到的东东啦。
然后新建工程,代码如下:
public class MainActivity extends Activity {
private static final String TAG = "MainActivity ...";
private static final String TESSBASE_PATH = "/mnt/sdcard/tesseract/";
private static final String DEFAULT_LANGUAGE = "eng";
private static final String IMAGE_PATH = "/mnt/sdcard/test1.jpg";
private static final String EXPECTED_FILE = TESSBASE_PATH + "tessdata/" + DEFAULT_LANGUAGE
+ ".traineddata";
private TessBaseAPI service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testOcr();
}
public void testOcr(){
mHandler.post(new Runnable() {
@Override
public void run() {
Log.d(TAG, "begin>>>>>>>");
ocr();
//test();
}
});
}
public void test(){
// First, make sure the eng.traineddata file exists.
/*assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to "
+ EXPECTED_FILE, new File(EXPECTED_FILE).exists());*/
final TessBaseAPI baseApi = new TessBaseAPI();
baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE);
final Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.test);
//digits is a .jpg image I found in one of the issues here.
ImageView img = (ImageView) findViewById(R.id.image);
img.setImageBitmap(bmp);//I can see the ImageView. So we know that it should work if I sent it to the setImage()
baseApi.setImage(bmp);
Log.v("Kishore","Kishore:Working");//This statement is never reached. Futhermore, on putting some more Log.v commands in the setImage function, I found out that the native function nativeSetImagePix is never accessed. I have attached the Logcat output below to show that it is not accessed.
String outputText = baseApi.getUTF8Text();
Log.v("Kishore","Kishore:"+outputText);
baseApi.end();
bmp.recycle();
}
protected void ocr() {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(IMAGE_PATH, options);
try {
ExifInte易做图ce exif = new ExifInte易做图ce(IMAGE_PATH);
int exifOrientation = exif.getAttributeInt(ExifInte易做图ce.TAG_ORIENTATION, ExifInte易做图ce.ORIENTATION_NORMAL);
Log.v(TAG, "Orient: " + exifOrientation);
int rotate = 0;
switch (exifOrientation) {
case ExifInte易做图ce.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInte易做图ce.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInte易做图ce.ORIENTATION_ROTATE_270:
rotate = 270;
&nbs
补充:移动开发 , Android ,