diff options
author | Iliyan Malchev <malchev@google.com> | 2008-09-26 16:36:49 -0700 |
---|---|---|
committer | Iliyan Malchev <malchev@google.com> | 2008-09-26 16:36:49 -0700 |
commit | 17686c35db25e65505c1248e6aa3407120f6ce83 (patch) | |
tree | 9939c32bba0029e7edda797d30b735bc00315b97 | |
parent | a6e3d96ab9bde61223610e9880dffc6d8a8c4e5d (diff) | |
download | tesseract-17686c35db25e65505c1248e6aa3407120f6ce83.tar.gz |
Implement libtesseract_main JNI bindings.
Signed-off-by: Iliyan Malchev <malchev@google.com>
-rw-r--r-- | ccmain/jni.cpp | 130 |
1 files changed, 89 insertions, 41 deletions
diff --git a/ccmain/jni.cpp b/ccmain/jni.cpp index 3d3be3e..81fe1b7 100644 --- a/ccmain/jni.cpp +++ b/ccmain/jni.cpp @@ -16,24 +16,87 @@ */ #include <nativehelper/jni.h> -#include <stdio.h> #include <assert.h> +#include <stdio.h> // debug -#define LOG_TAG "tesseract" +#include "baseapi.h" +#include "varable.h" +#include "tessvars.h" + +BOOL_VAR (tessedit_write_images, TRUE, + "Capture the image from the IPE"); + +#define LOG_TAG "OcrLib" #include <utils/Log.h> +static tesseract::TessBaseAPI api; + jboolean ocr_open(JNIEnv *env, jobject thiz, jstring lang) { - LOGI("init"); - return JNI_TRUE; + if (lang == NULL) { + LOGE("lang string is null!"); + return JNI_FALSE; + } + + const char *c_lang = env->GetStringUTFChars(lang, NULL); + if (c_lang == NULL) { + LOGE("could not extract lang string!"); + return JNI_FALSE; + } + + jboolean res = JNI_TRUE; + + LOGI("lang %s\n", c_lang); + if (api.Init("/sdcard/", c_lang)) { + LOGE("could not initialize tesseract!"); + res = JNI_FALSE; + } + else if (!api.ReadConfigFile("/sdcard/tessdata/ratings")) { + LOGE("could not read config file, using defaults!"); + // This is not a fatal error. + } + + env->ReleaseStringUTFChars(lang, c_lang); + LOGI("successfully initialized tesseract!"); + return res; } jstring -ocr_recognize(JNIEnv *env, jobject thiz, jbyteArray image) +ocr_recognize(JNIEnv *env, jobject thiz, + jbyteArray image, + jint width, jint height, jint rowWidth) { - LOGI("recognize"); - return NULL; + int x = width, y = height, rw = rowWidth; + LOGI("recognize image x=%d, y=%d, rw=%d\n", x, y, rw); + + if (env->GetArrayLength(image) < width * height) { + LOGE("image length = %ld is less than width * height = %ld!", + env->GetArrayLength(image), + width * height); + } + + jbyte* buffer = env->GetByteArrayElements(image, NULL); + api.SetImage((const unsigned char *)buffer, x, y, 1, rw); + char * text = api.GetUTF8Text(); + env->ReleaseByteArrayElements(image, buffer, JNI_ABORT); + + if (tessedit_write_images) { + page_image.write("/data/tessinput.tif"); + } + + if (text) { // debug + const char *outfile = "/data/out.txt"; + LOGI("write to output %s\n", outfile); + FILE* fp = fopen(outfile, "w"); + if (fp != NULL) { + fwrite(text, strlen(text), 1, fp); + fclose(fp); + } + } + + // Will that work on a NULL? + return env->NewStringUTF(text); } static void @@ -42,12 +105,10 @@ ocr_close(JNIEnv *env, jobject thiz) LOGI("quit"); } -static const char *classPathName = "com/android/ocr/Ocr"; - static JNINativeMethod methods[] = { - {"open", "(Ljava/lang/String;)Z", (void*)ocr_open}, - {"recognize", "([B)Ljava/lang/String;", (void*)ocr_recognize}, - {"close", "()V", (void*)ocr_close}, + {"open", "(Ljava/lang/String;)Z", (void*)ocr_open}, + {"recognize", "([BIII)Ljava/lang/String;", (void*)ocr_recognize}, + {"close", "()V", (void*)ocr_close}, }; /* @@ -56,16 +117,15 @@ static JNINativeMethod methods[] = { static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods, int numMethods) { - jclass clazz; + jclass clazz = env->FindClass(className); - clazz = env->FindClass(className); if (clazz == NULL) { - fprintf(stderr, - "Native registration unable to find class '%s'\n", className); + LOGE("Native registration unable to find class %s", className); return JNI_FALSE; } + if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { - fprintf(stderr, "RegisterNatives failed for '%s'\n", className); + LOGE("RegisterNatives failed for %s", className); return JNI_FALSE; } @@ -73,19 +133,6 @@ static int registerNativeMethods(JNIEnv* env, const char* className, } /* - * Register native methods for all classes we know about. - */ -static int registerNatives(JNIEnv* env) -{ - if (!registerNativeMethods(env, classPathName, - methods, sizeof(methods) / sizeof(methods[0]))) { - return JNI_FALSE; - } - - return JNI_TRUE; -} - -/* * Set some test stuff up. * * Returns the JNI version on success, -1 on failure. @@ -100,27 +147,28 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { UnionJNIEnvToVoid uenv; uenv.venv = NULL; - jint result = -1; JNIEnv* env = NULL; if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) { - fprintf(stderr, "ERROR: GetEnv failed\n"); - goto bail; + LOGE("GetEnv failed\n"); + return (jint)-1; } env = uenv.env; assert(env != NULL); - printf("In libtesseract JNI_OnLoad\n"); + LOGI("In OcrLib JNI_OnLoad\n"); - if (!registerNatives(env)) { - fprintf(stderr, "ERROR: quakemaster native registration failed\n"); - goto bail; + if (JNI_FALSE == + registerNativeMethods(env, + "com/android/ocr/OcrLib", + methods, + sizeof(methods) / sizeof(methods[0]))) { + LOGE("OcrLib native registration failed\n"); + return (jint)-1; } /* success -- return valid version number */ - result = JNI_VERSION_1_4; - -bail: - return result; + LOGI("OcrLib native registration succeeded!\n"); + return (jint)JNI_VERSION_1_4; } |