/ocr/ocrservice/jni/opticalflow/optical_flow-jni.cpp

http://eyes-free.googlecode.com/ · C++ · 274 lines · 205 code · 50 blank · 19 comment · 9 complexity · 113caaf5e3e0d6d8128f61cf7aacf8df 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. // Author: Andrew Harp
  17. #include <time.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <jni.h>
  21. #include "types.h"
  22. #include "optical_flow_utils.h"
  23. #include "time_log.h"
  24. #include "image.h"
  25. #include "optical_flow.h"
  26. namespace flow {
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. JNIEXPORT
  31. void
  32. JNICALL
  33. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_initNative(
  34. JNIEnv* env,
  35. jobject thiz,
  36. jint width,
  37. jint height,
  38. jint downsample_factor);
  39. JNIEXPORT
  40. void
  41. JNICALL
  42. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_addFrameNative(
  43. JNIEnv* env,
  44. jobject thiz,
  45. jbyteArray photo_data,
  46. jlong timestamp);
  47. JNIEXPORT
  48. void
  49. JNICALL
  50. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_computeFeaturesNative(
  51. JNIEnv* env,
  52. jobject thiz,
  53. jboolean cached_ok);
  54. JNIEXPORT
  55. void
  56. JNICALL
  57. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_computeFlowNative(
  58. JNIEnv* env,
  59. jobject thiz);
  60. JNIEXPORT
  61. void
  62. JNICALL
  63. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_printInfoNative(
  64. JNIEnv* env,
  65. jobject thiz);
  66. JNIEXPORT
  67. void
  68. JNICALL
  69. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_addInterestRegionNative(
  70. JNIEnv* env,
  71. jobject thiz,
  72. jint num_x, jint num_y,
  73. jfloat left, jfloat top, jfloat right, jfloat bottom);
  74. JNIEXPORT
  75. jfloatArray
  76. JNICALL
  77. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_getFeaturesNative(
  78. JNIEnv* env,
  79. jobject thiz,
  80. jboolean only_found_);
  81. JNIEXPORT
  82. void
  83. JNICALL
  84. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_getAccumulatedDeltaNative(
  85. JNIEnv* env,
  86. jobject thiz,
  87. jlong timestamp,
  88. jfloat position_x,
  89. jfloat position_y,
  90. jfloat radius,
  91. jfloatArray delta);
  92. JNIEXPORT
  93. void
  94. JNICALL
  95. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_resetNative(
  96. JNIEnv* env,
  97. jobject thiz);
  98. #ifdef __cplusplus
  99. }
  100. #endif
  101. OpticalFlow* optical_flow = NULL;
  102. JNIEXPORT
  103. void
  104. JNICALL
  105. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_initNative(
  106. JNIEnv* env,
  107. jobject thiz,
  108. jint width,
  109. jint height,
  110. jint downsample_factor) {
  111. SAFE_DELETE(optical_flow);
  112. LOGI("Initializing optical flow. %dx%d, %d",
  113. width, height, downsample_factor);
  114. optical_flow = new OpticalFlow(width, height, downsample_factor);
  115. }
  116. JNIEXPORT
  117. void
  118. JNICALL
  119. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_addFrameNative(
  120. JNIEnv* env,
  121. jobject thiz,
  122. jbyteArray photo_data,
  123. jlong timestamp) {
  124. CHECK(optical_flow != NULL, "Optical flow not initialized!");
  125. resetTimeLog();
  126. timeLog("Starting optical flow");
  127. // Copy image into currFrame.
  128. jboolean iCopied = JNI_FALSE;
  129. jbyte* pixels = env->GetByteArrayElements(photo_data, &iCopied);
  130. timeLog("Got elements");
  131. // Add the frame to the optical flow object.
  132. optical_flow->nextFrame(reinterpret_cast<uint8*>(pixels), timestamp);
  133. env->ReleaseByteArrayElements(photo_data, pixels, JNI_ABORT);
  134. timeLog("Released elements");
  135. }
  136. JNIEXPORT
  137. void
  138. JNICALL
  139. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_computeFeaturesNative(
  140. JNIEnv* env,
  141. jobject thiz,
  142. jboolean cached_ok) {
  143. CHECK(optical_flow != NULL, "Optical flow not initialized!");
  144. optical_flow->computeFeatures(cached_ok);
  145. }
  146. JNIEXPORT
  147. void
  148. JNICALL
  149. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_computeFlowNative(
  150. JNIEnv* env,
  151. jobject thiz) {
  152. CHECK(optical_flow != NULL, "Optical flow not initialized!");
  153. optical_flow->computeFlow();
  154. }
  155. JNIEXPORT
  156. void
  157. JNICALL
  158. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_printInfoNative(
  159. JNIEnv* env,
  160. jobject thiz) {
  161. CHECK(optical_flow != NULL, "Optical flow not initialized!");
  162. printTimeLog();
  163. optical_flow->printInfo();
  164. }
  165. JNIEXPORT
  166. void
  167. JNICALL
  168. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_addInterestRegionNative(
  169. JNIEnv* env,
  170. jobject thiz,
  171. jint num_x, jint num_y,
  172. jfloat left, jfloat top, jfloat right, jfloat bottom) {
  173. CHECK(optical_flow != NULL, "Optical flow not initialized!");
  174. optical_flow->addInterestRegion(num_x, num_y, left, top, right, bottom);
  175. timeLog("Added interest region.");
  176. }
  177. JNIEXPORT
  178. jfloatArray
  179. JNICALL
  180. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_getFeaturesNative(
  181. JNIEnv* env,
  182. jobject thiz,
  183. jboolean only_found) {
  184. CHECK(optical_flow != NULL, "Optical flow not initialized!");
  185. jfloat feature_arr[MAX_FEATURES * FEATURE_STEP];
  186. const int32 number_of_features =
  187. optical_flow->getFeatures(only_found, feature_arr);
  188. // Create and return the array that will be passed back to Java.
  189. jfloatArray features = env->NewFloatArray(number_of_features * FEATURE_STEP);
  190. if (features == NULL) {
  191. LOGE("null array!");
  192. return NULL;
  193. }
  194. env->SetFloatArrayRegion(
  195. features, 0, number_of_features * FEATURE_STEP, feature_arr);
  196. return features;
  197. }
  198. JNIEXPORT
  199. void
  200. JNICALL
  201. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_getAccumulatedDeltaNative(
  202. JNIEnv* env,
  203. jobject thiz,
  204. jlong timestamp,
  205. jfloat position_x,
  206. jfloat position_y,
  207. jfloat radius,
  208. jfloatArray delta) {
  209. CHECK(optical_flow != NULL, "Optical flow not initialized!");
  210. const Point2D query_position(position_x, position_y);
  211. const Point2D query_delta =
  212. optical_flow->getAccumulatedDelta(query_position, radius, timestamp);
  213. const jfloat point_arr[] = { query_delta.x, query_delta.y };
  214. env->SetFloatArrayRegion(delta, 0, 2, point_arr);
  215. }
  216. JNIEXPORT
  217. void
  218. JNICALL
  219. Java_com_googlecode_eyesfree_opticflow_OpticalFlow_resetNative(
  220. JNIEnv* env, jobject thiz) {
  221. LOGI("Cleaning up optical flow.");
  222. SAFE_DELETE(optical_flow);
  223. }
  224. } // namespace flow