You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.9 KiB
C++
73 lines
1.9 KiB
C++
2 years ago
|
#include "TerminalDevice.h"
|
||
|
#include <dlfcn.h>
|
||
|
|
||
|
typedef jbyteArray (*TakePhotoFunc)(int, int, int, int);
|
||
|
|
||
|
bool GetJniEnv(JavaVM *vm, JNIEnv **env)
|
||
|
{
|
||
|
bool did_attach_thread = false;
|
||
|
*env = nullptr;
|
||
|
// Check if the current thread is attached to the VM
|
||
|
auto get_env_result = vm->GetEnv((void**)env, JNI_VERSION_1_6);
|
||
|
if (get_env_result == JNI_EDETACHED)
|
||
|
{
|
||
|
if (vm->AttachCurrentThread(env, NULL) == JNI_OK) {
|
||
|
did_attach_thread = true;
|
||
|
} else {
|
||
|
// Failed to attach thread. Throw an exception if you want to.
|
||
|
}
|
||
|
} else if (get_env_result == JNI_EVERSION)
|
||
|
{
|
||
|
// Unsupported JNI version. Throw an exception if you want to.
|
||
|
}
|
||
|
return did_attach_thread;
|
||
|
}
|
||
|
|
||
|
|
||
|
CTerminalDevice::CTerminalDevice(JavaVM* vm, jobject service)
|
||
|
{
|
||
|
m_vm = vm;
|
||
|
JNIEnv* env = NULL;
|
||
|
bool attached = GetJniEnv(m_vm, &env);
|
||
|
m_javaService = env->NewGlobalRef(service);
|
||
|
if (attached)
|
||
|
{
|
||
|
vm->DetachCurrentThread();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
CTerminalDevice::~CTerminalDevice()
|
||
|
{
|
||
|
JNIEnv* env = NULL;
|
||
|
bool attached = GetJniEnv(m_vm, &env);
|
||
|
env->DeleteGlobalRef(m_javaService);
|
||
|
if (attached)
|
||
|
{
|
||
|
m_vm->DetachCurrentThread();
|
||
|
}
|
||
|
m_javaService = NULL;
|
||
|
}
|
||
|
|
||
|
bool CTerminalDevice::TakePhoto(unsigned char channel, unsigned char preset, const string& path, bool photo)
|
||
|
{
|
||
|
JNIEnv* env = NULL;
|
||
|
bool attached = GetJniEnv(m_vm, &env);
|
||
|
jclass serviceClass = env->GetObjectClass(m_javaService);
|
||
|
jmethodID mid = env->GetMethodID(serviceClass, "takePhoto", "(SSLjava/lang/String;)Z");
|
||
|
jstring str = env->NewStringUTF(path.c_str());
|
||
|
jboolean res = env->CallBooleanMethod (m_javaService, mid, (jint)channel, (jint)preset, str);
|
||
|
env->ReleaseStringUTFChars(str, path.c_str());
|
||
|
env->DeleteLocalRef(serviceClass);
|
||
|
|
||
|
if (!res)
|
||
|
{
|
||
|
int aa = 1;
|
||
|
}
|
||
|
if (attached)
|
||
|
{
|
||
|
m_vm->DetachCurrentThread();
|
||
|
}
|
||
|
return res == JNI_TRUE;
|
||
|
}
|
||
|
|