Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ee028248d | ||
|
|
84cc693593 | ||
|
|
4cf1990b16 | ||
|
|
2130cf0e6b | ||
|
|
6717047d1d |
@@ -16,6 +16,7 @@ FaceApp* g_Application = nullptr;
|
|||||||
AAssetManager* g_assetManager = nullptr;
|
AAssetManager* g_assetManager = nullptr;
|
||||||
|
|
||||||
jobject g_callback = nullptr;
|
jobject g_callback = nullptr;
|
||||||
|
jobject g_callbackAnimationFinished = nullptr;
|
||||||
|
|
||||||
void handle_cmd(android_app *pApp, int32_t cmd) {
|
void handle_cmd(android_app *pApp, int32_t cmd) {
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
@@ -149,22 +150,7 @@ Java_com_hmwl_face_1sdk_FaceActivity_SetCppInitArg(JNIEnv *env, jobject thiz, js
|
|||||||
}
|
}
|
||||||
memcpy(g_InitArgString, nativeString, len);
|
memcpy(g_InitArgString, nativeString, len);
|
||||||
}
|
}
|
||||||
extern "C"
|
|
||||||
JNIEXPORT void JNICALL
|
|
||||||
Java_com_hmwl_face_1sdk_FaceActivity_ChangeStateCpp(JNIEnv *env, jobject thiz, jstring json) {
|
|
||||||
// TODO: implement ChangeStateCpp()
|
|
||||||
const char *nativeString = env->GetStringUTFChars(json, nullptr);
|
|
||||||
jsize len = env->GetStringUTFLength(json);
|
|
||||||
if(len > ArgLen)
|
|
||||||
{
|
|
||||||
aout << "ArgLen to long:" << len << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(g_Application->isInited())
|
|
||||||
{
|
|
||||||
g_Application->changeMotion(nativeString);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
extern "C"
|
extern "C"
|
||||||
JNIEXPORT void JNICALL
|
JNIEXPORT void JNICALL
|
||||||
Java_com_hmwl_face_1sdk_FaceActivity_StopRunning(JNIEnv *env, jobject thiz) {
|
Java_com_hmwl_face_1sdk_FaceActivity_StopRunning(JNIEnv *env, jobject thiz) {
|
||||||
@@ -241,6 +227,63 @@ void CppCallback(const string& motion_type)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void CppAnimationFinishedCallback()
|
||||||
|
{
|
||||||
|
JNIEnv* env = nullptr;
|
||||||
|
bool needDetach = false;
|
||||||
|
|
||||||
|
// 获取当前线程的 JNIEnv
|
||||||
|
JavaVM* vm = g_jvm; // 你需要提前保存 JavaVM* 到全局变量 g_jvm
|
||||||
|
int status = vm->GetEnv((void**)&env, JNI_VERSION_1_6);
|
||||||
|
if (status == JNI_EDETACHED) {
|
||||||
|
// 当前线程未 attach,需 attach
|
||||||
|
if (vm->AttachCurrentThread(&env, nullptr) != 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
needDetach = true;
|
||||||
|
} else if (status != JNI_OK) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (g_callbackAnimationFinished == nullptr) {
|
||||||
|
if (needDetach) vm->DetachCurrentThread();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. 获取 CallbackInterface 的 class
|
||||||
|
jclass callbackClass = env->GetObjectClass(g_callbackAnimationFinished);
|
||||||
|
if (callbackClass == nullptr) {
|
||||||
|
if (needDetach) vm->DetachCurrentThread();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 获取 OnLoadActionFinished 方法 ID(注意方法名大小写必须一致!)
|
||||||
|
jmethodID methodId = env->GetMethodID(callbackClass, "OnAnimationFinished", "()V");
|
||||||
|
if (methodId == nullptr) {
|
||||||
|
env->ExceptionDescribe(); // 打印异常
|
||||||
|
env->ExceptionClear();
|
||||||
|
if (needDetach) vm->DetachCurrentThread();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 调用 Java 回调
|
||||||
|
env->CallVoidMethod(g_callbackAnimationFinished, methodId);
|
||||||
|
|
||||||
|
// 5. 检查是否抛出异常
|
||||||
|
if (env->ExceptionCheck()) {
|
||||||
|
env->ExceptionDescribe();
|
||||||
|
env->ExceptionClear();
|
||||||
|
}
|
||||||
|
|
||||||
|
env->DeleteLocalRef(callbackClass);
|
||||||
|
|
||||||
|
// 7. 如果是临时 attach 的线程,要 detach
|
||||||
|
if (needDetach) {
|
||||||
|
vm->DetachCurrentThread();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
JNIEXPORT jstring JNICALL
|
JNIEXPORT jstring JNICALL
|
||||||
@@ -268,4 +311,31 @@ Java_com_hmwl_face_1sdk_FaceActivity_PreReadAction(JNIEnv *env, jobject thiz, js
|
|||||||
|
|
||||||
std::string ret = g_Application->preReadyMotion(nativeString, CppCallback);
|
std::string ret = g_Application->preReadyMotion(nativeString, CppCallback);
|
||||||
return env->NewStringUTF(ret.c_str());
|
return env->NewStringUTF(ret.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
JNIEXPORT void JNICALL
|
||||||
|
Java_com_hmwl_face_1sdk_FaceActivity_ChangeMotionCpp(JNIEnv *env, jobject thiz, jstring json,
|
||||||
|
jobject callback, jboolean loop) {
|
||||||
|
// TODO: implement ChangeMotionCpp()
|
||||||
|
const char *nativeString = env->GetStringUTFChars(json, nullptr);
|
||||||
|
jsize len = env->GetStringUTFLength(json);
|
||||||
|
if(len > ArgLen)
|
||||||
|
{
|
||||||
|
aout << "ArgLen to long:" << len << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存新的回调
|
||||||
|
if (callback != nullptr) {
|
||||||
|
g_callbackAnimationFinished = env->NewGlobalRef(callback);
|
||||||
|
} else {
|
||||||
|
g_callbackAnimationFinished = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(g_Application->isInited())
|
||||||
|
{
|
||||||
|
g_Application->changeMotion(nativeString, CppAnimationFinishedCallback, loop);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -50,6 +50,21 @@ public class FaceActivity extends GameActivity implements FaceLandmarkerHelper.L
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||||
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||||
|
|
||||||
|
if (requestCode == REQUEST_CAMERA_PERMISSION) {
|
||||||
|
// 检查权限是否被授予
|
||||||
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
// 用户授予了权限,启动相机
|
||||||
|
startCamera();
|
||||||
|
} else {
|
||||||
|
requestCameraPermission();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void startCamera() {
|
private void startCamera() {
|
||||||
ListenableFuture<ProcessCameraProvider> cameraProviderFuture =
|
ListenableFuture<ProcessCameraProvider> cameraProviderFuture =
|
||||||
ProcessCameraProvider.getInstance(this);
|
ProcessCameraProvider.getInstance(this);
|
||||||
@@ -276,15 +291,19 @@ public class FaceActivity extends GameActivity implements FaceLandmarkerHelper.L
|
|||||||
void OnLoadActionFinished(String result);
|
void OnLoadActionFinished(String result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface AnimationFinishedCallbackInterface {
|
||||||
|
void OnAnimationFinished();
|
||||||
|
}
|
||||||
|
|
||||||
protected native String PreReadAction(String motion, CallbackInterface callback);
|
protected native String PreReadAction(String motion, CallbackInterface callback);
|
||||||
protected String PreLoadAction(String json, CallbackInterface callback){
|
protected String PreLoadAction(String json, CallbackInterface callback){
|
||||||
return PreReadAction(json, callback);
|
return PreReadAction(json, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected native void ChangeStateCpp(String json);
|
protected native void ChangeMotionCpp(String json, AnimationFinishedCallbackInterface callback, boolean loop);
|
||||||
protected void ChangeMotion(String json){
|
protected void ChangeMotion(String json, AnimationFinishedCallbackInterface callback, boolean loop){
|
||||||
Log.i("FaceActivity", "ChangeState:" + json);
|
Log.i("FaceActivity", "ChangeState:" + json);
|
||||||
ChangeStateCpp(json);
|
ChangeMotionCpp(json, callback, loop);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加这个缺失的方法
|
// 添加这个缺失的方法
|
||||||
|
|||||||
@@ -80,4 +80,7 @@ dependencies {
|
|||||||
implementation 'com.google.flogger:flogger-system-backend:0.7.4'
|
implementation 'com.google.flogger:flogger-system-backend:0.7.4'
|
||||||
|
|
||||||
implementation 'com.google.guava:guava:31.1-android'
|
implementation 'com.google.guava:guava:31.1-android'
|
||||||
|
|
||||||
|
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7") // 提供 lifecycleScope
|
||||||
|
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.9.0")
|
||||||
}
|
}
|
||||||
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 10 KiB |
|
Before Width: | Height: | Size: 10 KiB |
|
Before Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |