我试图按照谷歌的“CameraX 入门”代码实验室进行操作,我尝试用 Java 而不是 Kotlin 来完成,但是当我运行它并尝试拍照时,它给了我一个错误,提示未绑定到有效相机. 我找不到代码中的错误所在。我检查了 logcat,它说表面可能有问题,它可能无效,但我不确定如何解决这个问题,有人可以帮我。我将在我的 XML 文件中包含我拥有的内容以及 startCamera 和 takePhoto 函数。
//xml file code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/camera_capture_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:scaleType="fitCenter"
android:text="Take Photo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:elevation="2dp" />
<androidx.camera.view.PreviewView
android:id="@+id/viewFinder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
//start camera function
private void startCamera() throws ExecutionException, InterruptedException {
previewView = findViewById(R.id.viewFinder);
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(MainActivity.this);
cameraProviderFuture.addListener(() -> {
try {
// Used to bind the lifecycle of cameras to the lifecycle owner …Run Code Online (Sandbox Code Playgroud)