NDK产生共享库文件
将gocr的src源码放在gocr/jni/ 文件夹内,并在该路径下添加Androd.mk文件。
Android.mk
因为最后一句是include $(BUILD_EXECUTABLE) ,所以最后生成的是可执行 文件。
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := gocr LOCAL_SRC_FILES := barcode.c box.c database.c detect.c gocr.c job.c lines.c list.c ocr0.c ocr0n.c ocr1.c otsu.c output.c pcx.c pgm2asc.c pixel.c pnm.c progress.c remove.c tga.c unicode.c include $(BUILD_EXECUTABLE)
执行命令:ndk-build NDK_PROJECT_PATH=/home/GOCR/andorid-gocr/sources/gocr
最后生成/gocr/libs/armeabi/gocr可执行文件:
Install : gocr => libs/armeabi/gocr
这个可执行文件可以通过以下方式运行:
djpeg -pnm -gray SomeSmallImage.jpg > someimage.pnm
adb push someimage.pnm /data/local
adb shell /data/local/gocr /data/local/someimage.pnm
因为目标是生成共享库文件,所以include $(BUILD_EXECUTABLE )改为include $(BUILD_SHARED_LIBRARY ),最后生成:
Install : libgocr.so => libs/armeabi/libgocr.so
1.在Android项目中添加JAVA接口类,这个接口要能实现这样的功能:传入pnm图片的路径(字符串),执行后能返回识别出的文字(字符串)。
GocrJni.java
package enleo.jni; public class GocrJni{ public native String GocrProcess(String sPath); }
2.在Android项目通过调用本地方法
static { System.loadLibrary("gocr"); } private static String getBucketId(String paramString) { return String.valueOf(paramString.toLowerCase().hashCode()); } ... String result = jni.GocrProcess("HELLO"); ...
3. 本地实现 jni/gocr.c
jstring Java_enleo_jni_GocrJni_GocrProcess( JNIEnv* env, jstring thiz ) { // (1) jstring->char* char* rtn = NULL; jclass clsstring = env->FindClass(thiz); jstring strencode = env->NewStringUTF("utf-8"); jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B"); jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode); jsize alen = env->GetArrayLength(barr); jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE); if (alen > 0) { rtn = (char*)malloc(alen + 1); memcpy(rtn, ba, alen); rtn[alen] = 0; } env->ReleaseByteArrayElements(barr, ba, 0); int multipnm=1; job_t job; JOB = &job; setvbuf(stdout, (char *) NULL, _IONBF, 0); /* not buffered */ while (multipnm==1) { // (2) configure job_init(&job); job->src.fname = rtn; // (3) call ocr if it is used as lib mark_start(&job); multipnm = read_picture(&job); if (multipnm<0) break; /* read error */ call_ocr(&job); mark_end(&job); // (4)output ... } }
(需要代码实现一个转换器,Linux现有的转换工具有DJPEG)
【参考】
环境
http://www.ideasandroid.com/archives/224
移植
http://blog.csdn.net/fsfcvictor/archive/2010/10/28/5972044.aspx
Android.mk
http://fanwei51880.blog.163.com/blog/static/3240674020111149375810/
makefile
http://www.huihoo.org/gnu/linux1/makefile.html
gocr
http://android-gocr.sourceforge.net/
http://code.google.com/p/wordsnap-ocr/issues/detail?id=3
wordsnap
http://code.google.com/p/wordsnap-ocr/
Native接口设计
http://blog.csdn.net/Zengyangtech/archive/2010/12/05/6055913.aspx