脸型匹配完成
This commit is contained in:
@@ -4,6 +4,8 @@ package com.khronos.vulkan_samples;
|
||||
import android.Manifest;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.Toast;
|
||||
@@ -33,7 +35,7 @@ import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class MainActivity extends GameActivity implements FaceLandmarkerHelper.LandmarkerListener{
|
||||
public class MainActivity extends GameActivity implements FaceLandmarkerHelper.LandmarkerListener, CameraDataFetcher.CameraDataCallback {
|
||||
private ProcessCameraProvider cameraProvider;
|
||||
private void initCamera(){
|
||||
backgroundExecutor = Executors.newSingleThreadExecutor();
|
||||
@@ -50,6 +52,9 @@ public class MainActivity extends GameActivity implements FaceLandmarkerHelper.L
|
||||
);
|
||||
}
|
||||
|
||||
private Handler handler = new Handler(Looper.getMainLooper());
|
||||
private Runnable cameraDataRunnable;
|
||||
private static final long INTERVAL = 1000; // 5秒间隔
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -87,12 +92,44 @@ public class MainActivity extends GameActivity implements FaceLandmarkerHelper.L
|
||||
}
|
||||
});
|
||||
|
||||
// 初始化Runnable
|
||||
cameraDataRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
fetchCameraData();
|
||||
handler.postDelayed(this, INTERVAL); // 再次调度
|
||||
}
|
||||
};
|
||||
|
||||
// 开始周期性调用
|
||||
startPeriodicFetching();
|
||||
|
||||
|
||||
Log.i("MainActivity", "onCreate: ");
|
||||
}
|
||||
|
||||
boolean canFetch = true;
|
||||
private void fetchCameraData() {
|
||||
if(canFetch)
|
||||
{
|
||||
CameraDataFetcher.fetchCameraData("http://175.178.100.240:5000/data", MainActivity.this);
|
||||
canFetch = false;
|
||||
}
|
||||
}
|
||||
private void startPeriodicFetching() {
|
||||
handler.postDelayed(cameraDataRunnable, INTERVAL);
|
||||
}
|
||||
|
||||
private void stopPeriodicFetching() {
|
||||
handler.removeCallbacks(cameraDataRunnable);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
stopPeriodicFetching(); // 避免内存泄漏
|
||||
}
|
||||
|
||||
private boolean hasCameraPermission() {
|
||||
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
||||
== PackageManager.PERMISSION_GRANTED;
|
||||
@@ -133,12 +170,12 @@ public class MainActivity extends GameActivity implements FaceLandmarkerHelper.L
|
||||
@Override
|
||||
public void analyze(@NonNull ImageProxy image) {
|
||||
try {
|
||||
Log.d("Camera", "Received image: " + image.getWidth() + "x" + image.getHeight());
|
||||
//Log.d("Camera", "Received image: " + image.getWidth() + "x" + image.getHeight());
|
||||
processImageForVulkan(image);
|
||||
detectFace(image);
|
||||
} finally {
|
||||
//image.close(); // 确保在这里关闭
|
||||
Log.d("Camera", "Image closed, ready for next frame");
|
||||
//Log.d("Camera", "Image closed, ready for next frame");
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -151,6 +188,7 @@ public class MainActivity extends GameActivity implements FaceLandmarkerHelper.L
|
||||
}, ContextCompat.getMainExecutor(this));
|
||||
}
|
||||
|
||||
|
||||
private void processImageForVulkan(ImageProxy image) {
|
||||
// 获取图像信息
|
||||
int width = image.getWidth();
|
||||
@@ -230,9 +268,6 @@ public class MainActivity extends GameActivity implements FaceLandmarkerHelper.L
|
||||
nativeBuffer.order(ByteOrder.nativeOrder());
|
||||
}
|
||||
|
||||
float offsetX = (640.f-480.f)/640.0f;
|
||||
offsetX /= 2;
|
||||
offsetX *= -1;
|
||||
int index = 0;
|
||||
// Iterate through each detected face
|
||||
for (List<NormalizedLandmark> faceLandmarks : faceLandmarkerResult.faceLandmarks()) {
|
||||
@@ -244,7 +279,7 @@ public class MainActivity extends GameActivity implements FaceLandmarkerHelper.L
|
||||
//Log.i(TAG, "Index" + index + " x:"+p.x() + " y:"+p.y() + " z:"+p.z());
|
||||
points[arrayIndex++] = p.x();
|
||||
points[arrayIndex++] = p.y();
|
||||
points[arrayIndex++] = p.z();
|
||||
points[arrayIndex++] = p.z() * z_rate;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
@@ -255,5 +290,26 @@ public class MainActivity extends GameActivity implements FaceLandmarkerHelper.L
|
||||
floatBuffer.position(0);
|
||||
passDataToNative(nativeBuffer, index, width, height);
|
||||
}
|
||||
|
||||
public float z_rate = 1.0f;
|
||||
|
||||
public native void passDataToNative(ByteBuffer buffer, int pointCount, int width, int height);
|
||||
public native void upDateCameraData(float fov, float x, float y, float z, float sx, float sy, float sz, float rotx, float roty, float rotz, float camx, float camy, float camz);
|
||||
|
||||
@Override
|
||||
public void onSuccess(CameraDataFetcher.CameraData data) {
|
||||
z_rate = data.z_rate;
|
||||
upDateCameraData(data.fov, data.x, data.y, data.z, data.sx, data.sy, data.sz,
|
||||
data.rotx, data.roty, data.rotz,
|
||||
data.camx, data.camy, data.camz
|
||||
);
|
||||
canFetch = true;
|
||||
Log.e(TAG, "GetCameraData " + data.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(String errorMessage) {
|
||||
Log.e(TAG, "GetCameraData Error");
|
||||
canFetch = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user