164 lines
5.9 KiB
Java
164 lines
5.9 KiB
Java
package com.khronos.vulkan_samples;
|
|
|
|
|
|
import android.Manifest;
|
|
import android.content.pm.PackageManager;
|
|
import android.graphics.Color;
|
|
import android.graphics.PixelFormat;
|
|
import android.graphics.SurfaceTexture;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.view.Surface;
|
|
import android.view.SurfaceView;
|
|
import android.view.TextureView;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.FrameLayout;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.camera.core.CameraSelector;
|
|
import androidx.camera.core.Preview;
|
|
import androidx.camera.core.SurfaceRequest;
|
|
import androidx.camera.lifecycle.ProcessCameraProvider;
|
|
import androidx.core.app.ActivityCompat;
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import com.google.androidgamesdk.GameActivity;
|
|
import com.google.common.util.concurrent.ListenableFuture;
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
public class MainActivity extends GameActivity {
|
|
private TextureView textureView; // 使用 TextureView 替代 PreviewView
|
|
private ProcessCameraProvider cameraProvider;
|
|
private FrameLayout containerLayout;
|
|
private void initCamera(){
|
|
// 获取 GameActivity 的根视图
|
|
ViewGroup rootView = (ViewGroup) getWindow().getDecorView();
|
|
|
|
// 创建容器布局
|
|
containerLayout = new FrameLayout(this);
|
|
containerLayout.setLayoutParams(new ViewGroup.LayoutParams(
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
ViewGroup.LayoutParams.MATCH_PARENT
|
|
));
|
|
|
|
// 创建 TextureView 用于摄像头预览
|
|
textureView = new TextureView(this);
|
|
textureView.setLayoutParams(new ViewGroup.LayoutParams(
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
ViewGroup.LayoutParams.MATCH_PARENT
|
|
));
|
|
|
|
// 将 TextureView 添加到容器
|
|
containerLayout.addView(textureView);
|
|
|
|
// 将容器添加到根视图的底部(作为背景)
|
|
//rootView.addView(containerLayout); // 添加到索引0(最底层)
|
|
rootView.addView(containerLayout, 0);
|
|
|
|
|
|
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: ");
|
|
}
|
|
|
|
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();
|
|
|
|
// 使用 TextureView 的 SurfaceTexture
|
|
SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
|
|
if (surfaceTexture != null) {
|
|
Surface surface = new Surface(surfaceTexture);
|
|
preview.setSurfaceProvider(new Preview.SurfaceProvider() {
|
|
@Override
|
|
public void onSurfaceRequested(@NonNull SurfaceRequest request) {
|
|
request.provideSurface(surface, ContextCompat.getMainExecutor(MainActivity.this),
|
|
result -> {});
|
|
}
|
|
});
|
|
}
|
|
|
|
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 native void sendArgumentsToPlatform(String[] args);
|
|
} |