android ocr tesseract

来源:互联网 发布:macbook怎么下载淘宝 编辑:程序博客网 时间:2024/05/29 09:04
tesseract是开源ocr项目,不过在1.03之后才支持中文并且要在android4.0以上才可以。

现在有android版本的
地址:http://code.google.com/p/tesseract-android-tools/

这个版本得自己git 三个库 leptonica  tesseract  libjpeg ,我自己是编译成功了,但测试的时候native层总是crash。

 

于是发现了tess的android的另一个分支 tess-two

推荐linux上编译

 

一、下载&编译

1、首先下载tess-two

git clone git://github.com/rmtheis/tess-two tess


2、进入 tess目录,里面有三个项目,我们只需要进入tess-two就可以直接编译了

cd tess/tess-two
ndk-build


 3、编译好后,将src下的两个包以及libs导入到自己的项目就可以用啦

这里把我我把编译好后的东西放出来,用的话不用再编译了

下载:tess-two.zip

 

二、使用

tesseract 使用了 leptonica的图像处理库,对于图像处理还是比较强大的

Android官方地址:tesseract-android-tools

但它必须要一个匹配库,即tessdata,我们可以从官方拷贝,在前面git的项目里面tesseract源码目录有现成的tessdata可以用,对于中文,google code上也有下载,当然也可以自己训练不同语言的tessdata。

匹配库可以到这里下载:http://tesseract-ocr.googlecode.com/svn/trunk

package com.test.tess;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.googlecode.tesseract.android.TessBaseAPI;

public class TessActivity extends Activity {
/** Called when the activity is first created. */

private final String TAG = "TessActivity";
private String imagePath = "/mnt/sdcard/test.jpg";
private final String lang = "chi_sim";
private final String path = "/sdcard/";
String text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) this.findViewById(R.id.text);
try {
tess();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tv.setText(text);

}

private void tess() throws IOException {

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
// bitmap,我这里是以流的形式,只要能形成Bitmap就行
Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/test.jpg", options);
// Bitmap bitmap = BitmapFactory.decodeStream(instream, null, options);
// instream.close();
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

// 如果图片有Alpha值,那么最好设置一下
ExifInterface exif = new ExifInterface(imagePath);
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) {

// Getting width & height of the given image.
int w = bitmap.getWidth();
int h = bitmap.getHeight();

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

// Rotating Bitmap
bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
// tesseract req. ARGB_8888
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
}

TessBaseAPI baseApi = new TessBaseAPI();

baseApi.setDebug(true);

// 初始化tess
// android下面,tessdata肯定得放到sd卡里了
// 如果tessdata这个目录放在sd卡的根目录
// 那么path直接传入sd卡的目录
// eng就是英文,关于语言,按ISO 639-3标准的代码就行,具体请移步wiki
boolean isInitSuccess = baseApi.init(path, lang);// lang
// options是为了缩放图片,这个酌情缩放,图片小的话可以不缩放
if (isInitSuccess) {
Log.e("TAG", "初始化tess成功了");
// 设置要ocr的图片bitmap
baseApi.setImage(bitmap);
// 根据Init的语言,获得ocr后的字符串
text = baseApi.getUTF8Text();

Log.e(TAG, text);
// 释放bitmap
baseApi.clear();
// 如果连续ocr多张图片,这个end可以不调用,但每次ocr之后,必须调用clear来对bitmap进行释放
// 释放native内存
baseApi.end();
} else {
Log.e("TAG", "初始化tess失败");
}
}
}

原创粉丝点击