gpt4 book ai didi

android - CameraX 无法初始化

转载 作者:行者123 更新时间:2023-12-04 23:46:21 29 4
gpt4 key购买 nike

我尝试使用 CameraView。我是 Android 的初学者。我阅读了一些关于类的文章和信息,但我无法让它发挥作用。
我有以下错误:

Caused by: java.lang.IllegalStateException: CameraX is not initialized properly. Either CameraX.initialize() needs to have been called or the CameraXConfig.Provider interface must be implemented by your Application class



这是我的代码:

activity_main.xml
   <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:theme="@style/AppTheme">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.camera.view.CameraView
android:id="@+id/view_camera"
android:layout_width="match_parent"
android:layout_height="match_parent" />

MainActivity.java:
    package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.camera2.Camera2Config;
import androidx.camera.core.CameraX;
import androidx.camera.core.CameraXConfig;
import androidx.camera.core.impl.CameraFactory;
import androidx.camera.core.impl.PreviewConfig;
import androidx.camera.view.CameraView;

import android.os.Bundle;


public class MainActivity extends AppCompatActivity implements CameraXConfig.Provider {

private CameraView view_camera;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view_camera = findViewById(R.id.view_camera);
view_camera.bindToLifecycle(this);
}

@NonNull
@Override
public CameraXConfig getCameraXConfig() {
return Camera2Config.defaultConfig();
}
}

和 list :
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.CAMERA" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

应用程序的 gradle 文件:
    apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.1"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 26
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 {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
def camerax_version = "1.0.0-alpha10"// Add the CameraX core dependency implementation
implementation "androidx.camera:camera-core:${camerax_version}"// Add the CameraX Camera2 API interop support dependency
implementation "androidx.camera:camera-camera2:${camerax_version}"
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'
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"
}

谢谢你的帮助。

最佳答案

我在使用最新的 alpha11 cameraX 库时遇到了同样的问题,因此降级为

// CameraX View class
implementation "androidx.camera:camera-view:1.0.0-alpha10"

还添加了相机信息参数
preview?.setSurfaceProvider(viewFinder.createSurfaceProvider(camera?.cameraInfo))

关于android - CameraX 无法初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60209847/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com