MCPcopy Create free account
hub / github.com/android/ndk-samples / LoadImage

Method LoadImage

teapots/common/ndk_helper/JNIHelper.cpp:337–390  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

335}
336
337jobject JNIHelper::LoadImage(const char* file_name, int32_t* outWidth,
338 int32_t* outHeight, bool* hasAlpha) {
339 if (activity_ == NULL) {
340 LOGI(
341 "JNIHelper has not been initialized. Call init() to initialize the "
342 "helper");
343 return 0;
344 }
345
346 // Lock mutex
347 std::lock_guard<std::mutex> lock(mutex_);
348
349 JNIEnv* env = AttachCurrentThread();
350 jstring name = env->NewStringUTF(file_name);
351
352 jmethodID mid = env->GetMethodID(jni_helper_java_class_, "loadImage",
353 "(Ljava/lang/String;)Ljava/lang/Object;");
354
355 jobject out = env->CallObjectMethod(jni_helper_java_ref_, mid, name);
356
357 jclass javaCls =
358 RetrieveClass(env, "com/sample/helper/NDKHelper$TextureInformation");
359 jfieldID fidRet = env->GetFieldID(javaCls, "ret", "Z");
360 jfieldID fidHasAlpha = env->GetFieldID(javaCls, "alphaChannel", "Z");
361 jfieldID fidWidth = env->GetFieldID(javaCls, "originalWidth", "I");
362 jfieldID fidHeight = env->GetFieldID(javaCls, "originalHeight", "I");
363 bool ret = env->GetBooleanField(out, fidRet);
364 bool alpha = env->GetBooleanField(out, fidHasAlpha);
365 int32_t width = env->GetIntField(out, fidWidth);
366 int32_t height = env->GetIntField(out, fidHeight);
367 if (!ret) {
368 LOGI("Texture load failed %s", file_name);
369 }
370 LOGI("Loaded texture original size:%dx%d alpha:%d", width, height,
371 (int32_t)alpha);
372 if (outWidth != NULL) {
373 *outWidth = width;
374 }
375 if (outHeight != NULL) {
376 *outHeight = height;
377 }
378 if (hasAlpha != NULL) {
379 *hasAlpha = alpha;
380 }
381
382 jfieldID fidImage = env->GetFieldID(javaCls, "image", "Ljava/lang/Object;");
383 jobject array = env->GetObjectField(out, fidImage);
384 jobject objGlobal = env->NewGlobalRef(array);
385
386 env->DeleteLocalRef(name);
387 env->DeleteLocalRef(javaCls);
388
389 return objGlobal;
390}
391
392std::string JNIHelper::ConvertString(const char* str, const char* encode) {
393 if (activity_ == NULL) {

Callers

nothing calls this directly

Calls 1

CallObjectMethodMethod · 0.80

Tested by

no test coverage detected