resultiterator.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2011, Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include "common.h"
  18. #include "resultiterator.h"
  19. #include "allheaders.h"
  20. #include "pageres.h"
  21. #include "tesseractclass.h"
  22. using namespace tesseract;
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif /* __cplusplus */
  26. jstring Java_com_googlecode_tesseract_android_ResultIterator_nativeGetUTF8Text(JNIEnv *env,
  27. jclass clazz, jlong nativeResultIterator, jint level) {
  28. ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator;
  29. PageIteratorLevel enumLevel = (PageIteratorLevel) level;
  30. char *text = resultIterator->GetUTF8Text(enumLevel);
  31. jstring result = env->NewStringUTF(text);
  32. free(text);
  33. return result;
  34. }
  35. jfloat Java_com_googlecode_tesseract_android_ResultIterator_nativeConfidence(JNIEnv *env,
  36. jclass clazz, jlong nativeResultIterator, jint level) {
  37. ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator;
  38. PageIteratorLevel enumLevel = (PageIteratorLevel) level;
  39. return (jfloat) resultIterator->Confidence(enumLevel);
  40. }
  41. jobjectArray Java_com_googlecode_tesseract_android_ResultIterator_nativeGetChoices(JNIEnv *env,
  42. jobject thiz, jlong nativeResultIterator, jint level) {
  43. // Get the actual result iterator and level (as C objects)
  44. PageIteratorLevel enumLevel = (PageIteratorLevel) level;
  45. ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator;
  46. // Create a choice iterator to determine to the number of alternatives
  47. tesseract::ChoiceIterator ci(*resultIterator);
  48. int numberOfAlternatives = 0;
  49. do {
  50. numberOfAlternatives++;
  51. } while (ci.Next());
  52. // Create a string array to hold the results
  53. jobjectArray ret = (jobjectArray) env->NewObjectArray(numberOfAlternatives, env->FindClass("java/lang/String"), env->NewStringUTF(""));
  54. // Save each result to the output array
  55. int i = 0;
  56. tesseract::ChoiceIterator cb(*resultIterator);
  57. do {
  58. // Create the string output
  59. const char *utfText = cb.GetUTF8Text();
  60. // Add each string to the object array elements
  61. char newString[strlen(utfText) + 5];
  62. sprintf(newString, "%s|%.2f", utfText, cb.Confidence());
  63. env->SetObjectArrayElement(ret, i, env->NewStringUTF(newString));
  64. // Move to the next element in the list
  65. i++;
  66. } while(cb.Next());
  67. // Return the string array
  68. return ret;
  69. }
  70. void Java_com_googlecode_tesseract_android_ResultIterator_nativeDelete(JNIEnv *env, jclass clazz,
  71. jlong nativeResultIterator) {
  72. ResultIterator *resultIterator = (ResultIterator *) nativeResultIterator;
  73. if (resultIterator != 0) {
  74. delete resultIterator;
  75. }
  76. return;
  77. }
  78. #ifdef __cplusplus
  79. }
  80. #endif /* __cplusplus */