66 jobject oActivity = mApp->activity->clazz;
67 jclass cActivity = env->GetObjectClass(oActivity);
70 jfieldID camera_backend_fieldID = env->GetFieldID(cActivity,
"mCameraBackend",
"Lorg/imaginer/scol/CameraBackend;");
71 if (camera_backend_fieldID)
72 result.
instance = env->GetObjectField(oActivity, camera_backend_fieldID);
76 result.
method = env->GetMethodID(result.
clazz, name, signature);
78 env->DeleteLocalRef(cActivity);
96 if (mApp->activity->vm->GetEnv((
void**)&env, JNI_VERSION_1_6) != JNI_OK)
97 mApp->activity->vm->AttachCurrentThread(&env, NULL);
100 jobject oActivity = mApp->activity->clazz;
101 jclass cActivity = env->GetObjectClass(oActivity);
105 bool authPassed =
false;
108 jfieldID reqBoolean = env->GetFieldID(cActivity,
"mMinAuthPassed",
"Z");
109 authPassed = (bool) env->GetBooleanField(oActivity, reqBoolean);
111 boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
115 jmethodID camera_permission = env->GetMethodID(cActivity,
"RequestCameraPermission",
"()V");
116 if (camera_permission)
117 env->CallVoidMethod(oActivity, camera_permission);
119 if (env->ExceptionCheck())
121 env->ExceptionClear();
122 MMechostr(MSKRUNTIME,
"Android camera exception : Check android.permission.CAMERA\n");
128 jfieldID reqBoolean = env->GetFieldID(cActivity,
"mCameraAuthPassed",
"Z");
129 authPassed = (bool) env->GetBooleanField(oActivity, reqBoolean);
131 boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
133 env->DeleteLocalRef(cActivity);
136 LOGI(
">>>>>>> JgrabMethod look for 'openCamera'");
140 LOGI(
">>>>>>> JgrabMethod failed for 'openCamera'");
144 LOGI(
">>>>>>> JgrabMethod call method");
145 env->CallVoidMethod(method_box.
instance, method_box.
method, (jint)width, (jint)height, (jint)index);
146 env->DeleteLocalRef(method_box.
instance);
147 env->DeleteLocalRef(method_box.
clazz);
149 if (env->ExceptionCheck())
151 env->ExceptionDescribe();
152 env->ExceptionClear();
153 LOGI(
">>>>>>> 'openCamera' error");
160 mFrameBuffer = cv::Mat(mSize.height + mSize.height / 2, mSize.width, CV_8UC1);
161 mFrameRGB = cv::Mat(mSize.height, mSize.width, CV_8UC3);
167 mSize = cv::Size(0, 0);
178 if (mApp->activity->vm->GetEnv((
void**)&env, JNI_VERSION_1_6) != JNI_OK)
179 mApp->activity->vm->AttachCurrentThread(&env, NULL);
184 LOGI(
">>>>>>> JgrabMethod failed for 'closeCamera'");
189 env->DeleteLocalRef(method_box.
instance);
190 env->DeleteLocalRef(method_box.
clazz);
192 if (env->ExceptionCheck())
194 env->ExceptionClear();
195 LOGI(
">>>>>>> JgrabMethod failed for 'closeCamera'");
203 if (mApp->activity->vm->GetEnv((
void**)&env, JNI_VERSION_1_6) != JNI_OK)
204 mApp->activity->vm->AttachCurrentThread(&env, NULL);
209 LOGI(
">>>>>>> JgrabMethod failed for 'torchOn'");
214 env->DeleteLocalRef(method_box.
instance);
215 env->DeleteLocalRef(method_box.
clazz);
217 if (env->ExceptionCheck())
219 env->ExceptionClear();
220 LOGI(
">>>>>>> JgrabMethod failed for 'torchOn'");
227 if (mApp->activity->vm->GetEnv((
void**)&env, JNI_VERSION_1_6) != JNI_OK)
228 mApp->activity->vm->AttachCurrentThread(&env, NULL);
233 LOGI(
">>>>>>> JgrabMethod failed for 'torchOn'");
238 env->DeleteLocalRef(method_box.
instance);
239 env->DeleteLocalRef(method_box.
clazz);
241 if (env->ExceptionCheck())
243 env->ExceptionClear();
244 LOGI(
">>>>>>> JgrabMethod failed for 'torchOn'");
252 if (mApp->activity->vm->GetEnv((
void**)&env, JNI_VERSION_1_6) != JNI_OK)
253 mApp->activity->vm->AttachCurrentThread(&env, NULL);
258 LOGI(
">>>>>>> JgrabMethod failed for 'getDeviceOrientation'");
262 ret = (int)env->CallIntMethod(method_box.
instance, method_box.
method);
263 env->DeleteLocalRef(method_box.
instance);
264 env->DeleteLocalRef(method_box.
clazz);
266 if (env->ExceptionCheck())
267 env->ExceptionClear();
287 if (mApp->activity->vm->GetEnv((
void**)&env, JNI_VERSION_1_6) != JNI_OK)
288 mApp->activity->vm->AttachCurrentThread(&env, NULL);
293 LOGI(
">>>>>>> JgrabMethod failed for 'isCameraOpened'");
297 ret = (bool)env->CallBooleanMethod(method_box.
instance, method_box.
method);
298 env->DeleteLocalRef(method_box.
instance);
299 env->DeleteLocalRef(method_box.
clazz);
301 if (env->ExceptionCheck())
302 env->ExceptionClear();
314 cv::Size nSize(0, 0);
316 if (mApp->activity->vm->GetEnv((
void**)&env, JNI_VERSION_1_6) != JNI_OK)
317 mApp->activity->vm->AttachCurrentThread(&env, NULL);
322 LOGI(
">>>>>>> JgrabMethod failed for 'getPreviewSize'");
326 jobject previewSize_object = env->CallObjectMethod(method_box.
instance, method_box.
method);
328 if (env->ExceptionCheck())
330 env->ExceptionClear();
334 if (previewSize_object == 0)
337 jclass cameraSize_class = env->GetObjectClass(previewSize_object);
338 jfieldID size_width_field = env->GetFieldID(cameraSize_class,
"x",
"I");
339 jfieldID size_height_field = env->GetFieldID(cameraSize_class,
"y",
"I");
341 int width = env->GetIntField(previewSize_object, size_width_field);
342 int height = env->GetIntField(previewSize_object, size_height_field);
343 nSize = cv::Size(width, height);
345 env->DeleteLocalRef(cameraSize_class);
346 env->DeleteLocalRef(previewSize_object);
347 env->DeleteLocalRef(method_box.
instance);
348 env->DeleteLocalRef(method_box.
clazz);
357 if (mApp->activity->vm->GetEnv((
void**)&env, JNI_VERSION_1_6) != JNI_OK)
358 mApp->activity->vm->AttachCurrentThread(&env, NULL);
364 LOGI(
">>>>>>> JgrabMethod failed for 'isCameraFacing'");
368 bool camIsFacing = (bool)env->CallBooleanMethod(facing_method_box.
instance, facing_method_box.
method);
370 if (env->ExceptionCheck())
371 env->ExceptionClear();
373 env->DeleteLocalRef(facing_method_box.
instance);
374 env->DeleteLocalRef(facing_method_box.
clazz);
380 LOGI(
">>>>>>> JgrabMethod failed for 'grabFrame'");
384 jbyteArray frame_buffer_object = (jbyteArray)env->CallObjectMethod(method_box.
instance, method_box.
method);
385 if (!frame_buffer_object || env->ExceptionCheck())
388 env->ExceptionClear();
389 env->DeleteLocalRef(method_box.
instance);
390 env->DeleteLocalRef(method_box.
clazz);
394 int length = env->GetArrayLength((jarray)frame_buffer_object);
395 env->GetByteArrayRegion(frame_buffer_object, 0, length, (jbyte*)
mFrameBuffer.data);
396 env->DeleteLocalRef(frame_buffer_object);
397 env->DeleteLocalRef(method_box.
instance);
398 env->DeleteLocalRef(method_box.
clazz);
403 env->ExceptionClear();
404 MMechostr(MSKRUNTIME,
"Could not get camera frame");
411 cv::cvtColor(
mFrameBuffer, rgbmat, cv::COLOR_YUV2BGR_NV21, 3);
421 cv::flip(rgbmat,
mFrameRGB, camIsFacing ? 1 : -1);
423 else if (rotation == 90)
428 else if (rotation == 270)
459 int w = (int)ANativeWindow_getWidth(mApp->window);
460 int h = (int)ANativeWindow_getHeight(mApp->window);
463 if (mApp->activity->vm->GetEnv((
void**)&env, JNI_VERSION_1_6) != JNI_OK)
464 mApp->activity->vm->AttachCurrentThread(&env, NULL);
469 LOGI(
">>>>>>> JgrabMethod failed for 'setFocusPoint'");
473 env->CallVoidMethod(method_box.
instance, method_box.
method, (jint)x, (jint)y, (jint)w, (jint)h);
475 if (env->ExceptionCheck())
476 env->ExceptionClear();
479 env->DeleteLocalRef(method_box.
instance);
480 env->DeleteLocalRef(method_box.
clazz);
617 MMechostr(MSKDEBUG,
"CameraInputAndroid::RenderToScreen : updating frame");
620 catch (std::exception e)
622 MMechostr(MSKDEBUG,
"CameraInputAndroid::RenderToScreen : %s", e.what());
628 MMechostr(MSKDEBUG,
"CameraInputAndroid::RenderToScreen : frame empty!");
632 ANativeWindow* window = (ANativeWindow*)SCgetExtra(
"hscol");
635 MMechostr(MSKDEBUG,
"CameraInputAndroid::RenderToScreen : error: window empty !");
638 ANativeWindow_Buffer buffer;
641 if (ANativeWindow_lock(window, &buffer, 0) < 0)
646 cv::resize(frame, frame, cv::Size(buffer.width, buffer.height), 0, 0, cv::INTER_CUBIC);
648 for (
unsigned int y = 0; y < frame.rows; y++)
650 for (
unsigned int x = 0; x < frame.cols; x++)
652 unsigned long srcByte = (x * 3) + (frame.cols * 3 * y);
653 unsigned long destByte = (x * 4) + (buffer.width * 4 * y);
655 ((
unsigned char*)buffer.bits)[destByte + 2] = frame.data[srcByte];
656 ((
unsigned char*)buffer.bits)[destByte + 1] = frame.data[srcByte + 1];
657 ((
unsigned char*)buffer.bits)[destByte + 0] = frame.data[srcByte + 2];
658 ((
unsigned char*)buffer.bits)[destByte + 3] = 255;
663 ANativeWindow_unlockAndPost(window);