save code
This commit is contained in:
@@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
<!-- Declare permissions -->
|
<!-- Declare permissions -->
|
||||||
<uses-permission android:name="android.permission.CAMERA" />
|
<uses-permission android:name="android.permission.CAMERA" />
|
||||||
|
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
|
||||||
|
|
||||||
<application android:label="@string/app_name"
|
<application android:label="@string/app_name"
|
||||||
android:debuggable="true"
|
android:debuggable="true"
|
||||||
@@ -50,9 +51,13 @@
|
|||||||
android:value="@string/native_lib_name" />
|
android:value="@string/native_lib_name" />
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<activity
|
<activity android:name=".SecondActivity"
|
||||||
android:name=".SecondActivity"
|
android:exported="false" >
|
||||||
android:exported="false" />
|
</activity>
|
||||||
|
|
||||||
|
<activity android:name=".CameraActivity"
|
||||||
|
android:exported="true" >
|
||||||
|
</activity>
|
||||||
|
|
||||||
<provider
|
<provider
|
||||||
android:authorities="${applicationId}.provider"
|
android:authorities="${applicationId}.provider"
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package com.khronos.vulkan_samples;
|
package com.khronos.vulkan_samples;
|
||||||
|
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Debug;
|
import android.os.Debug;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -10,14 +14,24 @@ import android.widget.Button;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.activity.EdgeToEdge;
|
import androidx.activity.EdgeToEdge;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
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.graphics.Insets;
|
||||||
import androidx.core.view.ViewCompat;
|
import androidx.core.view.ViewCompat;
|
||||||
import androidx.core.view.WindowInsetsCompat;
|
import androidx.core.view.WindowInsetsCompat;
|
||||||
|
|
||||||
import com.google.androidgamesdk.GameActivity;
|
import com.google.androidgamesdk.GameActivity;
|
||||||
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
import com.google.mediapipe.examples.facelandmarker.FaceLandmarkerHelper;
|
import com.google.mediapipe.examples.facelandmarker.FaceLandmarkerHelper;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
@@ -27,6 +41,29 @@ public class MainActivity extends GameActivity {
|
|||||||
private ExecutorService backgroundExecutor;
|
private ExecutorService backgroundExecutor;
|
||||||
private FaceLandmarkerHelper faceLandmarkerHelper;
|
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
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@@ -41,20 +78,111 @@ public class MainActivity extends GameActivity {
|
|||||||
argArray[1] = "hello_triangle";
|
argArray[1] = "hello_triangle";
|
||||||
sendArgumentsToPlatform(argArray);
|
sendArgumentsToPlatform(argArray);
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
initCamera();
|
||||||
Log.i("MainActivity", "onCreate: ");
|
Log.i("MainActivity", "onCreate: ");
|
||||||
|
|
||||||
// 添加按钮到布局
|
// 添加按钮到布局
|
||||||
addButtonToLayout();
|
// addButtonToLayout();
|
||||||
|
|
||||||
backgroundExecutor = Executors.newSingleThreadExecutor();
|
// backgroundExecutor = Executors.newSingleThreadExecutor();
|
||||||
backgroundExecutor.execute(new Runnable() {
|
// backgroundExecutor.execute(new Runnable() {
|
||||||
@Override
|
// @Override
|
||||||
public void run() {
|
// public void run() {
|
||||||
if (faceLandmarkerHelper.isClose()) {
|
// if (faceLandmarkerHelper.isClose()) {
|
||||||
faceLandmarkerHelper.setupFaceLandmarker();
|
// 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() {
|
private void addButtonToLayout() {
|
||||||
|
|||||||
Reference in New Issue
Block a user