diff --git a/tess-two-test/src/com/googlecode/tesseract/android/test/TessBaseAPITest.java b/tess-two-test/src/com/googlecode/tesseract/android/test/TessBaseAPITest.java index 24b42526f..63f9ab8fe 100644 --- a/tess-two-test/src/com/googlecode/tesseract/android/test/TessBaseAPITest.java +++ b/tess-two-test/src/com/googlecode/tesseract/android/test/TessBaseAPITest.java @@ -112,7 +112,7 @@ public void testChoiceIterator() { List> choicesAndConfidences; iterator.begin(); do { - choicesAndConfidences = iterator.getChoicesAndConfidence(PageIteratorLevel.RIL_SYMBOL); + choicesAndConfidences = iterator.getSymbolChoicesAndConfidence(); assertNotNull("Invalid result.", choicesAndConfidences); for (Pair choiceAndConfidence : choicesAndConfidences) { diff --git a/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp b/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp index 7f2424e97..cac17f0b0 100644 --- a/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp +++ b/tess-two/jni/com_googlecode_tesseract_android/resultiterator.cpp @@ -48,21 +48,20 @@ jfloat Java_com_googlecode_tesseract_android_ResultIterator_nativeConfidence(JNI return (jfloat) resultIterator->Confidence(enumLevel); } -jobjectArray Java_com_googlecode_tesseract_android_ResultIterator_nativeGetChoices(JNIEnv *env, - jobject thiz, jlong nativeResultIterator, jint level) { +jobjectArray Java_com_googlecode_tesseract_android_ResultIterator_nativeGetSymbolChoices(JNIEnv *env, + jclass clazz, jlong nativeResultIterator) { - // Get the actual result iterator and level (as C objects) - PageIteratorLevel enumLevel = (PageIteratorLevel) level; + // Get the actual result iterator (as C object) ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator; - // Create a choice iterator to determine to the number of alternatives + // Create a choice iterator to determine the number of alternatives tesseract::ChoiceIterator ci(*resultIterator); int numberOfAlternatives = 0; do { numberOfAlternatives++; } while (ci.Next()); - // Create a string array to hold the results + // Create a string array to hold the choices jobjectArray ret = (jobjectArray) env->NewObjectArray(numberOfAlternatives, env->FindClass("java/lang/String"), env->NewStringUTF("")); // Save each result to the output array @@ -73,7 +72,7 @@ jobjectArray Java_com_googlecode_tesseract_android_ResultIterator_nativeGetChoic const char *utfText = cb.GetUTF8Text(); // Add each string to the object array elements - char newString[strlen(utfText) + 5]; + char newString[strlen(utfText) + 7]; sprintf(newString, "%s|%.2f", utfText, cb.Confidence()); env->SetObjectArrayElement(ret, i, env->NewStringUTF(newString)); @@ -85,13 +84,29 @@ jobjectArray Java_com_googlecode_tesseract_android_ResultIterator_nativeGetChoic return ret; } +jboolean Java_com_googlecode_tesseract_android_ResultIterator_nativeIsAtBeginningOf(JNIEnv *env, + jclass clazz, jlong nativeResultIterator, jint level) { + ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator; + PageIteratorLevel enumLevel = (PageIteratorLevel) level; + + return (jboolean) (resultIterator->IsAtBeginningOf(enumLevel) ? JNI_TRUE : JNI_FALSE); +} + +jboolean Java_com_googlecode_tesseract_android_ResultIterator_nativeIsAtFinalElement(JNIEnv *env, + jclass clazz, jlong nativeResultIterator, jint level, jint element) { + ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator; + PageIteratorLevel enumLevel = (PageIteratorLevel) level; + PageIteratorLevel enumElement = (PageIteratorLevel) element; + + return (jboolean) (resultIterator->IsAtFinalElement(enumLevel, enumElement) ? JNI_TRUE : JNI_FALSE); +} + void Java_com_googlecode_tesseract_android_ResultIterator_nativeDelete(JNIEnv *env, jclass clazz, jlong nativeResultIterator) { ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator; if (resultIterator != 0) { delete resultIterator; } - return; } #ifdef __cplusplus diff --git a/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java b/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java index d970f01fa..f736c447b 100644 --- a/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java +++ b/tess-two/src/com/googlecode/tesseract/android/ResultIterator.java @@ -69,6 +69,30 @@ public float confidence(int level) { return nativeConfidence(mNativeResultIterator, level); } + /** + * Returns true if the iterator is at the start of an object at the given + * level. Possible uses include determining if a call to Next(RIL_WORD) + * moved to the start of a RIL_PARA. + * + * @param level the page iterator level. See {@link PageIteratorLevel}. + * @return {@code true} if iterator points to the start of an object at the given level. + */ + public boolean isAtBeginningOf(int level) { + return nativeIsAtBeginningOf(mNativeResultIterator, level); + } + + /** + * Returns whether the iterator is positioned at the last element in a + * given level. (e.g. the last word in a line, the last line in a block) + * + * @param level the page iterator level. See {@link PageIteratorLevel}. + * @param element the page iterator level. See {@link PageIteratorLevel}. + * @return {@code true} if iterator points to the last element in a given level. + */ + public boolean isAtFinalElement(int level, int element) { + return nativeIsAtFinalElement(mNativeResultIterator, level, element); + } + /** * Returns all possible matching text strings and their confidence level * for the current object at the given level. @@ -76,12 +100,11 @@ public float confidence(int level) { * The default matching text is blank (""). * The default confidence level is zero (0.0) * - * @param level the page iterator level. See {@link PageIteratorLevel}. - * @return A list of pairs with the UTF string and the confidence + * @return A list of pairs with the UTF symbol and the confidence */ - public List> getChoicesAndConfidence(int level) { + public List> getSymbolChoicesAndConfidence() { // Get the native choices - String[] nativeChoices = nativeGetChoices(mNativeResultIterator, level); + String[] nativeChoices = nativeGetSymbolChoices(mNativeResultIterator); // Create the output list ArrayList> pairedResults = new ArrayList>(); @@ -121,9 +144,11 @@ public void delete() { nativeDelete(mNativeResultIterator); } - private static native String[] nativeGetChoices(long nativeResultIterator, int level); + private static native String[] nativeGetSymbolChoices(long nativeResultIterator); private static native String nativeGetUTF8Text(long nativeResultIterator, int level); private static native float nativeConfidence(long nativeResultIterator, int level); - private static native void nativeDelete(long nativeIterator); + private static native boolean nativeIsAtBeginningOf(long nativeResultIterator, int level); + private static native boolean nativeIsAtFinalElement(long nativeResultIterator, int level, int element); + private static native void nativeDelete(long nativeResultIterator); }