From 1a27d88ed0a2d961bdd31b031b3c8b0089d3f8a2 Mon Sep 17 00:00:00 2001 From: Iliyan Malchev Date: Wed, 24 Sep 2008 13:47:25 -0700 Subject: add jni bindings Signed-off-by: Iliyan Malchev --- ccmain/jni.cpp | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 ccmain/jni.cpp diff --git a/ccmain/jni.cpp b/ccmain/jni.cpp new file mode 100644 index 0000000..3d3be3e --- /dev/null +++ b/ccmain/jni.cpp @@ -0,0 +1,126 @@ +/* +** +** Copyright 2008, Google Inc. +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +#include +#include +#include + +#define LOG_TAG "tesseract" +#include + +jboolean +ocr_open(JNIEnv *env, jobject thiz, jstring lang) +{ + LOGI("init"); + return JNI_TRUE; +} + +jstring +ocr_recognize(JNIEnv *env, jobject thiz, jbyteArray image) +{ + LOGI("recognize"); + return NULL; +} + +static void +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}, +}; + +/* + * Register several native methods for one class. + */ +static int registerNativeMethods(JNIEnv* env, const char* className, + JNINativeMethod* gMethods, int numMethods) +{ + jclass clazz; + + clazz = env->FindClass(className); + if (clazz == NULL) { + fprintf(stderr, + "Native registration unable to find class '%s'\n", className); + return JNI_FALSE; + } + if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { + fprintf(stderr, "RegisterNatives failed for '%s'\n", className); + return JNI_FALSE; + } + + return JNI_TRUE; +} + +/* + * 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. + */ + +typedef union { + JNIEnv* env; + void* venv; +} UnionJNIEnvToVoid; + +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; + } + env = uenv.env; + + assert(env != NULL); + + printf("In libtesseract JNI_OnLoad\n"); + + if (!registerNatives(env)) { + fprintf(stderr, "ERROR: quakemaster native registration failed\n"); + goto bail; + } + + /* success -- return valid version number */ + result = JNI_VERSION_1_4; + +bail: + return result; +} -- cgit v1.2.3