226 lines
7.8 KiB
Java
226 lines
7.8 KiB
Java
package com.khronos.vulkan_samples;
|
|
|
|
|
|
import android.Manifest;
|
|
import android.content.Intent;
|
|
import android.content.pm.PackageManager;
|
|
import android.graphics.Color;
|
|
import android.os.Bundle;
|
|
import android.os.Debug;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.Button;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.activity.EdgeToEdge;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.camera.core.Camera;
|
|
import androidx.camera.core.CameraSelector;
|
|
import androidx.camera.core.Preview;
|
|
import androidx.camera.lifecycle.ProcessCameraProvider;
|
|
import androidx.camera.view.PreviewView;
|
|
import androidx.core.app.ActivityCompat;
|
|
import androidx.core.content.ContextCompat;
|
|
import androidx.core.graphics.Insets;
|
|
import androidx.core.view.ViewCompat;
|
|
import androidx.core.view.WindowInsetsCompat;
|
|
|
|
import com.google.androidgamesdk.GameActivity;
|
|
import com.google.common.util.concurrent.ListenableFuture;
|
|
import com.google.mediapipe.examples.facelandmarker.FaceLandmarkerHelper;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
|
|
public class MainActivity extends GameActivity {
|
|
|
|
|
|
private ExecutorService backgroundExecutor;
|
|
private FaceLandmarkerHelper faceLandmarkerHelper;
|
|
|
|
private PreviewView previewView;
|
|
private ProcessCameraProvider cameraProvider;
|
|
private Camera camera;
|
|
|
|
private void initCamera(){
|
|
// 添加 PreviewView 用于摄像头预览
|
|
previewView = new PreviewView(this);
|
|
previewView.setImplementationMode(PreviewView.ImplementationMode.COMPATIBLE);
|
|
|
|
// 将 PreviewView 添加到布局中
|
|
addContentView(previewView, new ViewGroup.LayoutParams(
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
ViewGroup.LayoutParams.MATCH_PARENT));
|
|
|
|
// ensureVulkanLayerTransparent();
|
|
|
|
// 检查权限并启动相机
|
|
if (hasCameraPermission()) {
|
|
startCamera();
|
|
} else {
|
|
requestCameraPermission();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
String native_lib_name = getResources().getString(R.string.native_lib_name);
|
|
try {
|
|
System.loadLibrary(native_lib_name);
|
|
} catch (UnsatisfiedLinkError e) {
|
|
Toast.makeText(getApplicationContext(), "Native code library failed to load.", Toast.LENGTH_SHORT).show();
|
|
}
|
|
String[] argArray = new String[2];
|
|
argArray[0] = "sample";
|
|
argArray[1] = "hello_triangle";
|
|
sendArgumentsToPlatform(argArray);
|
|
super.onCreate(savedInstanceState);
|
|
|
|
initCamera();
|
|
Log.i("MainActivity", "onCreate: ");
|
|
|
|
// 添加按钮到布局
|
|
// addButtonToLayout();
|
|
|
|
// backgroundExecutor = Executors.newSingleThreadExecutor();
|
|
// backgroundExecutor.execute(new Runnable() {
|
|
// @Override
|
|
// public void run() {
|
|
// if (faceLandmarkerHelper.isClose()) {
|
|
// faceLandmarkerHelper.setupFaceLandmarker();
|
|
// }
|
|
// }
|
|
// });
|
|
}
|
|
|
|
private void ensureVulkanLayerTransparent() {
|
|
// 延迟执行以确保 GameActivity 的视图已经创建
|
|
previewView.post(() -> {
|
|
ViewGroup rootView = (ViewGroup) getWindow().getDecorView().getRootView();
|
|
makeViewTransparent(rootView);
|
|
});
|
|
}
|
|
|
|
private void makeViewTransparent(ViewGroup viewGroup) {
|
|
for (int i = 0; i < viewGroup.getChildCount(); i++) {
|
|
View child = viewGroup.getChildAt(i);
|
|
if (child instanceof ViewGroup) {
|
|
makeViewTransparent((ViewGroup) child);
|
|
} else {
|
|
// 找到可能是 Vulkan 渲染层的视图并设置为透明
|
|
if (child.getBackground() != null) {
|
|
child.setBackgroundColor(Color.TRANSPARENT);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private boolean hasCameraPermission() {
|
|
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
|
== PackageManager.PERMISSION_GRANTED;
|
|
}
|
|
|
|
private void requestCameraPermission() {
|
|
ActivityCompat.requestPermissions(this,
|
|
new String[]{Manifest.permission.CAMERA},
|
|
REQUEST_CAMERA_PERMISSION);
|
|
}
|
|
|
|
private static final int REQUEST_CAMERA_PERMISSION = 1001;
|
|
|
|
private void startCamera() {
|
|
ListenableFuture<ProcessCameraProvider> cameraProviderFuture =
|
|
ProcessCameraProvider.getInstance(this);
|
|
|
|
cameraProviderFuture.addListener(() -> {
|
|
try {
|
|
cameraProvider = cameraProviderFuture.get();
|
|
|
|
// 创建预览用例
|
|
Preview preview = new Preview.Builder().build();
|
|
|
|
// 选择后置摄像头
|
|
CameraSelector cameraSelector = new CameraSelector.Builder()
|
|
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
|
|
.build();
|
|
|
|
// 将预览绑定到 PreviewView
|
|
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
|
|
|
// 绑定到生命周期
|
|
camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview);
|
|
|
|
} catch (ExecutionException | InterruptedException e) {
|
|
Log.e("MainActivity", "Error starting camera: " + e.getMessage());
|
|
}
|
|
}, ContextCompat.getMainExecutor(this));
|
|
}
|
|
|
|
private static final int REQUEST_CODE_PERMISSIONS = 1001;
|
|
private static final String[] REQUIRED_PERMISSIONS = new String[]{Manifest.permission.CAMERA};
|
|
private static final String TAG = "MainActivity";
|
|
|
|
private boolean allPermissionsGranted() {
|
|
for (String permission : REQUIRED_PERMISSIONS) {
|
|
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
if (requestCode == REQUEST_CODE_PERMISSIONS) {
|
|
if (allPermissionsGranted()) {
|
|
startCamera();
|
|
} else {
|
|
Log.e(TAG, "Permissions not granted by the user.");
|
|
finish(); // Close the activity if permission is denied
|
|
}
|
|
}
|
|
}
|
|
|
|
private void addButtonToLayout() {
|
|
// 创建按钮
|
|
Button button = new Button(this);
|
|
button.setText("Vulkan 按钮");
|
|
|
|
// 设置按钮位置(使用绝对坐标)
|
|
button.setX(50); // 距离左边50像素
|
|
button.setY(50); // 距离顶部50像素
|
|
|
|
// 设置按钮点击事件
|
|
button.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
onButtonClicked();
|
|
}
|
|
});
|
|
|
|
// 将按钮添加到 GameActivity 的布局中(不会覆盖 Vulkan 表面)
|
|
addContentView(button, new ViewGroup.LayoutParams(
|
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
ViewGroup.LayoutParams.WRAP_CONTENT
|
|
));
|
|
}
|
|
|
|
|
|
private void onButtonClicked() {
|
|
Toast.makeText(this, "Vulkan 按钮被点击了!", Toast.LENGTH_SHORT).show();
|
|
nativeButtonClicked();
|
|
// 启动SecondActivity
|
|
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
|
|
// 可以传递数据
|
|
intent.putExtra("message", "Hello from MainActivity!");
|
|
startActivity(intent);
|
|
}
|
|
|
|
private native void nativeButtonClicked();
|
|
|
|
private native void sendArgumentsToPlatform(String[] args);
|
|
} |