PageRenderTime 28ms CodeModel.GetById 6ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/ocr/ocrservice/jni/hydrogen/jni/hydrogentextdetector.cpp

http://eyes-free.googlecode.com/
C++ | 264 lines | 184 code | 64 blank | 16 comment | 18 complexity | 2bf7777c8aae2358262054fa1cda7674 MD5 | raw file
  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
 17#include "common.h"
 18#include "hydrogentextdetector.h"
 19
 20#define DEBUG_MODE false
 21
 22#ifdef __cplusplus
 23extern "C" {
 24#endif  /* __cplusplus */
 25
 26jint Java_com_googlecode_eyesfree_textdetect_HydrogenTextDetector_nativeConstructor(
 27    JNIEnv *env,
 28    jclass clazz,
 29    jint nativePtr) {
 30  if (DEBUG_MODE) LOGV(__FUNCTION__);
 31
 32  HydrogenTextDetector *ptr = new HydrogenTextDetector();
 33
 34  return (jint) ptr;
 35}
 36
 37void Java_com_googlecode_eyesfree_textdetect_HydrogenTextDetector_nativeDestructor(
 38    JNIEnv *env,
 39    jclass clazz,
 40    jint nativePtr) {
 41  if (DEBUG_MODE) LOGV(__FUNCTION__);
 42
 43  HydrogenTextDetector *ptr = (HydrogenTextDetector *) nativePtr;
 44
 45  delete ptr;
 46}
 47
 48bool getBoolField(JNIEnv *env, jclass &clazz, jobject &obj, const char *field) {
 49  jfieldID fieldID = env->GetFieldID(clazz, field, "Z");
 50
 51  return (env->GetBooleanField(obj, fieldID) == JNI_TRUE);
 52}
 53
 54int getIntField(JNIEnv *env, jclass &clazz, jobject &obj, const char *field) {
 55  jfieldID fieldID = env->GetFieldID(clazz, field, "I");
 56
 57  return (int) env->GetIntField(obj, fieldID);
 58}
 59
 60float getFloatField(JNIEnv *env, jclass &clazz, jobject &obj, const char *field) {
 61  jfieldID fieldID = env->GetFieldID(clazz, field, "F");
 62
 63  return (float) env->GetFloatField(obj, fieldID);
 64}
 65
 66void getStringField(JNIEnv *env, jclass &clazz, jobject &obj, const char *field,
 67                    char *dst) {
 68  jfieldID fieldID = env->GetFieldID(clazz, field, "Ljava/lang/String;");
 69  jstring str = (jstring) env->GetObjectField(obj, fieldID);
 70
 71  if (str != NULL) {
 72    jsize len = L_MIN(env->GetStringLength(str), 254);
 73
 74    env->GetStringUTFRegion(str, 0, len, dst);
 75
 76    dst[len + 1] = 0;
 77  } else {
 78    dst[0] = 0;
 79  }
 80}
 81
 82void Java_com_googlecode_eyesfree_textdetect_HydrogenTextDetector_nativeSetParameters(
 83    JNIEnv *env,
 84    jclass clazz,
 85    jint nativePtr,
 86    jobject params) {
 87  if (DEBUG_MODE) LOGV(__FUNCTION__);
 88
 89  HydrogenTextDetector *ptr = (HydrogenTextDetector *) nativePtr;
 90  HydrogenTextDetector::TextDetectorParameters *myParams = ptr->GetMutableParameters();
 91
 92  jclass paramClass = env->GetObjectClass(params);
 93
 94  getStringField(env, paramClass, params, "out_dir", myParams->out_dir);
 95
 96  myParams->debug = getBoolField(env, paramClass, params, "debug");
 97  myParams->edge_tile_x = getIntField(env, paramClass, params, "edge_tile_x");
 98  myParams->edge_tile_y = getIntField(env, paramClass, params, "edge_tile_y");
 99  myParams->edge_thresh = getIntField(env, paramClass, params, "edge_thresh");
100  myParams->edge_avg_thresh = getIntField(env, paramClass, params, "edge_avg_thresh");
101
102  myParams->skew_enabled = getBoolField(env, paramClass, params, "skew_enabled");
103  myParams->skew_min_angle = getFloatField(env, paramClass, params, "skew_min_angle");
104  myParams->skew_sweep_range = getFloatField(env, paramClass, params, "skew_sweep_range");
105  myParams->skew_sweep_delta = getFloatField(env, paramClass, params, "skew_sweep_delta");
106  myParams->skew_sweep_reduction = getIntField(env, paramClass, params, "skew_sweep_reduction");
107  myParams->skew_search_reduction = getIntField(env, paramClass, params, "skew_search_reduction");
108  myParams->skew_search_min_delta = getFloatField(env, paramClass, params, "skew_search_min_delta");
109
110  myParams->single_min_aspect = getFloatField(env, paramClass, params, "single_min_aspect");
111  myParams->single_max_aspect = getFloatField(env, paramClass, params, "single_max_aspect");
112  myParams->single_min_area = getIntField(env, paramClass, params, "single_min_area");
113  myParams->single_min_density = getFloatField(env, paramClass, params, "single_min_density");
114
115  myParams->pair_h_ratio = getFloatField(env, paramClass, params, "pair_h_ratio");
116  myParams->pair_d_ratio = getFloatField(env, paramClass, params, "pair_d_ratio");
117  myParams->pair_h_dist_ratio = getFloatField(env, paramClass, params, "pair_h_dist_ratio");
118  myParams->pair_v_dist_ratio = getFloatField(env, paramClass, params, "pair_v_dist_ratio");
119  myParams->pair_h_shared = getFloatField(env, paramClass, params, "pair_h_shared");
120
121  myParams->cluster_width_spacing = getIntField(env, paramClass, params, "cluster_width_spacing");
122  myParams->cluster_shared_edge = getFloatField(env, paramClass, params, "cluster_shared_edge");
123  myParams->cluster_h_ratio = getFloatField(env, paramClass, params, "cluster_h_ratio");
124
125  myParams->cluster_min_blobs = getIntField(env, paramClass, params, "cluster_min_blobs");
126  myParams->cluster_min_aspect = getFloatField(env, paramClass, params, "cluster_min_aspect");
127  myParams->cluster_min_fdr = getFloatField(env, paramClass, params, "cluster_min_fdr");
128  myParams->cluster_min_edge = getIntField(env, paramClass, params, "cluster_min_edge");
129  myParams->cluster_min_edge_avg = getIntField(env, paramClass, params, "cluster_min_edge_avg");
130}
131
132jint Java_com_googlecode_eyesfree_textdetect_HydrogenTextDetector_nativeGetTextAreas(
133    JNIEnv *env,
134    jclass clazz,
135    jint nativePtr) {
136  if (DEBUG_MODE) LOGV(__FUNCTION__);
137
138  HydrogenTextDetector *ptr = (HydrogenTextDetector *) nativePtr;
139
140  PIXA *textAreas = ptr->GetTextAreas();
141
142  return (jint) textAreas;
143}
144
145jfloat Java_com_googlecode_eyesfree_textdetect_HydrogenTextDetector_nativeGetSkewAngle(
146    JNIEnv *env,
147    jclass clazz,
148    jint nativePtr) {
149  if (DEBUG_MODE) LOGV(__FUNCTION__);
150
151  HydrogenTextDetector *ptr = (HydrogenTextDetector *) nativePtr;
152
153  l_float32 skew_angle = ptr->GetSkewAngle();
154
155  return (jfloat) skew_angle;
156}
157
158jint Java_com_googlecode_eyesfree_textdetect_HydrogenTextDetector_nativeGetSourceWidth(
159    JNIEnv *env,
160    jclass clazz,
161    jint nativePtr) {
162  if (DEBUG_MODE) LOGV(__FUNCTION__);
163
164  HydrogenTextDetector *ptr = (HydrogenTextDetector *) nativePtr;
165  PIX *pix = ptr->GetSourceImage();
166  jint h = pixGetWidth(pix);
167
168  pixDestroy(&pix);
169
170  return (jint) h;
171}
172
173jint Java_com_googlecode_eyesfree_textdetect_HydrogenTextDetector_nativeGetSourceHeight(
174    JNIEnv *env,
175    jclass clazz,
176    jint nativePtr) {
177  //LOGV(__FUNCTION__);
178
179  HydrogenTextDetector *ptr = (HydrogenTextDetector *) nativePtr;
180  PIX *pix = ptr->GetSourceImage();
181  jint w = pixGetWidth(pix);
182
183  pixDestroy(&pix);
184
185  return (jint) w;
186}
187
188jfloatArray Java_com_googlecode_eyesfree_textdetect_HydrogenTextDetector_nativeGetTextConfs(
189    JNIEnv *env,
190    jclass clazz,
191    jint nativePtr) {
192  if (DEBUG_MODE) LOGV(__FUNCTION__);
193
194  HydrogenTextDetector *ptr = (HydrogenTextDetector *) nativePtr;
195  NUMA *confs = ptr->GetTextConfs();
196  l_int32 count = numaGetCount(confs);
197  jfloatArray ret = env->NewFloatArray(count);
198  l_float32 nval;
199  jfloat jval;
200
201  if (ret != NULL) {
202    for (int i = 0; i < count; i++) {
203      numaGetFValue(confs, i, &nval);
204      jval = (jfloat) nval;
205      env->SetFloatArrayRegion(ret, i, 1, &jval);
206    }
207  }
208
209  numaDestroy(&confs);
210
211  return ret;
212}
213
214jint Java_com_googlecode_eyesfree_textdetect_HydrogenTextDetector_nativeGetSourceImage(
215    JNIEnv *env,
216    jclass clazz,
217    jint nativePtr) {
218  if (DEBUG_MODE) LOGV(__FUNCTION__);
219
220  HydrogenTextDetector *ptr = (HydrogenTextDetector *) nativePtr;
221
222  return (jint) ptr->GetSourceImage();
223}
224
225void Java_com_googlecode_eyesfree_textdetect_HydrogenTextDetector_nativeSetSourceImage(
226    JNIEnv *env,
227    jclass clazz,
228    jint nativePtr,
229    jint nativePix) {
230  if (DEBUG_MODE) LOGV(__FUNCTION__);
231
232  HydrogenTextDetector *ptr = (HydrogenTextDetector *) nativePtr;
233  PIX *pix = (PIX *) nativePix;
234
235  ptr->SetSourceImage(pix);
236}
237
238void Java_com_googlecode_eyesfree_textdetect_HydrogenTextDetector_nativeDetectText(
239    JNIEnv *env,
240    jclass clazz,
241    jint nativePtr) {
242  if (DEBUG_MODE) LOGV(__FUNCTION__);
243
244  HydrogenTextDetector *ptr = (HydrogenTextDetector *) nativePtr;
245
246  ptr->DetectText();
247}
248
249void Java_com_googlecode_eyesfree_textdetect_HydrogenTextDetector_nativeClear(
250    JNIEnv *env,
251    jclass clazz,
252    jint nativePtr) {
253  if (DEBUG_MODE) LOGV(__FUNCTION__);
254
255  HydrogenTextDetector *ptr = (HydrogenTextDetector *) nativePtr;
256
257  ptr->Clear();
258}
259
260#ifdef __cplusplus
261}
262#endif  /* __cplusplus */
263
264#undef DEBUG_MODE