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.
TermApp/app/src/main/cpp/TerminalDevice.cpp

88 lines
2.1 KiB
C++

#include "TerminalDevice.h"
#include <dlfcn.h>
#include "Camera.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)
{
jboolean res = JNI_FALSE;
CCamera camera;
camera.initCamera(NULL);
if (camera.isCameraReady())
{
camera.takePicture();
}
camera.closeCamera();
#if 0
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());
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();
}
#endif
return res == JNI_TRUE;
}