我正在尝试将 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;”没有解决。

最佳答案

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

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

您当然可以使用 Builder 的方法(例如设置目标分辨率)根据需要配置它。

关于android - 无法从包 androidx.camera.core 导入 PreviewConfig 类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59940096/

10-10 09:32