aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIliyan Malchev <malchev@google.com>2008-09-24 13:47:25 -0700
committerIliyan Malchev <malchev@google.com>2008-09-24 13:47:25 -0700
commit1a27d88ed0a2d961bdd31b031b3c8b0089d3f8a2 (patch)
treeda3b2385fcfac5f0fd4a5a0fa612f172191ed5c3
parent5f583ec3250631d7eb4615d6da5aea1eb8b98e80 (diff)
downloadtesseract-1a27d88ed0a2d961bdd31b031b3c8b0089d3f8a2.tar.gz
add jni bindings
Signed-off-by: Iliyan Malchev <malchev@google.com>
-rw-r--r--ccmain/jni.cpp126
1 files changed, 126 insertions, 0 deletions
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 <nativehelper/jni.h>
+#include <stdio.h>
+#include <assert.h>
+
+#define LOG_TAG "tesseract"
+#include <utils/Log.h>
+
+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;
+}