无法从AndroidX Camera库中导入PreviewConfig类

4

我正在尝试使用Camerax API制作相机应用,但是我遇到了一个问题。 PreviewConfig类无法解析。 以下是我的build.gradle(app)文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.cameraapi"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility = 1.8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    // CameraX core library
    def camerax_version = "1.0.0-alpha09"
    implementation "androidx.camera:camera-core:${camerax_version}"
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    // If you want to use the CameraX View class
    implementation "androidx.camera:camera-view:1.0.0-alpha06"
    // If you want to use the CameraX Extensions library
    implementation "androidx.camera:camera-extensions:1.0.0-alpha06"
    // If you want to use the CameraX Lifecycle library
    implementation "androidx.camera:camera-lifecycle:1.0.0-alpha03"
}

MainActivity.class文件是

package com.example.cameraapi;

import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.PreviewConfig; //this line is showing error
import android.os.Bundle;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}


在上面的代码行中,"import androidx.camera.core.PreviewConfig;" 无法解决。
3个回答

8

最近的CameraX版本(我认为从alpha06或alpha07开始)不再需要PreviewConfig来构建预览用例(这也适用于其他用例,如ImageAnalysis和ImageCapture)。现在可以使用Preview的Builder类构建Preview实例。

Preview preview = new Preview.Builder().build();

当然,您可以使用构建器的方法按照您的意愿进行配置(例如设置目标分辨率)。


5

相反,

// Create configuration object for the viewfinder use case
val previewConfig = PreviewConfig.Builder().apply {
    setTargetResolution(Size(640, 480))
}.build()


// Build the viewfinder use case
val preview = Preview(previewConfig)

使用

   // Build the viewfinder use case
    val preview = Preview.Builder()
        .setTargetResolution(Size(640, 480))
        .build()

0

很不幸,他们已经弃用它了,你应该看看更改。根据我所知,你应该使用import androidx.camera.core.impl.PreviewConfig而不是import androidx.camera.core.PreviewConfig;


1
是的,我尝试使用import androidx.camera.core.impl.PreviewConfig,但是由于没有关于如何使用这个类的文档,所以我无法使用它。 - gaurav_kumar
如果您拥有所有更新的依赖项,那么您可以导入该类,但是确实 - 我还没有看到任何关于此的文档。 - qki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接