Reputation: 63
I try to use CameraView. And I'm a beginner in Android. I read some articles and information on class but I am not able to make it works. I have the following errors :
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
Here is my code :
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();
}
}
And the manifest :
<?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>
The gradle file for app:
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"
}
Thanks for your help.
Upvotes: 4
Views: 11813
Reputation: 7410
My camera-camera2
and camera-lifecycle
libraries were using version 1.0.2
while the camera-view
library was using 1.1.0-beta01
.
Changing them all to use 1.1.0-beta01
fixed it.
I'm guessing something in the upgrade from 1.0.2
to 1.1.0-beta01
causes an initialization error.
Upvotes: 0
Reputation: 377
Non of all this suggestion doesn't help me. My PreviewView was on Fragment not in Activity.
In my case I have to add class MyApplication which implements CameraXConfig.Provider
public class MyApp extends Application implements CameraXConfig.Provider {
@Override
public CameraXConfig getCameraXConfig() {
return CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig())
//.setCameraExecutor(myExecutor)
//.setSchedulerHandler(mySchedulerHandler)
.build();
}
}
Next thing I got to do it was to change from getInstance(this)
to:
cameraProviderFuture = ProcessCameraProvider.getInstance(getActivity().getApplicationContext());
Full code:
private void startCamera() {
Log.d(TAG, "startCamera: starting camera");
cameraProviderFuture = ProcessCameraProvider.getInstance(getActivity().getApplicationContext());
cameraProviderFuture.addListener(() -> {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
bindCameraPreview(cameraProvider);
} catch (ExecutionException | InterruptedException e)
Log.e(TAG, "startCamera: Error during starting camera", e);
}
}, ContextCompat.getMainExecutor(requireContext()));
}
private void bindCameraPreview(@NonNull ProcessCameraProvider cameraProvider) {
Log.d(TAG, "bindCameraPreview: start");
previewView.setImplementationMode(PreviewView.ImplementationMode.PERFORMANCE);
Preview preview = new Preview.Builder()
.build();
preview.setSurfaceProvider(previewView.getSurfaceProvider());
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build();
Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, preview);
}
Upvotes: 0
Reputation: 1227
createSurfaceProvider() has been renamed to getSurfaceProvider():
preview?.setSurfaceProvider(viewFinder.surfaceProvider)
Credits: https://stackoverflow.com/a/63993443/3176753
Upvotes: 3
Reputation: 31
None of the solutions from here worked for me, and I'm on 1.0.0-beta08
version of CameraX. What worked for me was to make sure that I'm calling the statement
Preview.Builder()
.build()
.also {
it.setSurfaceProvider(viewFinder.createSurfaceProvider())
}
after the future from ProcessCameraProvider.getInstance(context)
gets resolved.
So while I was trying to save a reference to the preview in the fragment, I ended up with something like this:
private fun startCamera() {
val context = requireContext()
val cameraProviderFuture = ProcessCameraProvider.getInstance(context)
cameraProviderFuture.addListener({
val cameraProvider = cameraProviderFuture.get()
val cameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()
val preview = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(viewFinder.createSurfaceProvider())
}
cameraProvider.bindToLifecycle(this, cameraSelector, preview)
}, ContextCompat.getMainExecutor(context))
}
Upvotes: 3
Reputation: 111
Please try these steps. I had the same issue and I fixed it like this.
Add the latest CameraX dependencies to your gradle:
def camerax_version = "1.0.0-beta06"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-view:1.0.0-alpha13"
implementation "androidx.camera:camera-extensions:1.0.0-alpha13"
Add compileOptions
JavaVersions
within android{}
in gradle and sync now:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Create a new Java class with the below attributes. Leave the rest as is:
Name: CameraXIntApplication
(You can give any name you like)
Superclass: android.app.Application
Go to the CameraXIntApplication
class you just created and create the onCreate
method. Note: I had to manually create the onCreate
method in this class and it was not there by default. However you might get it by default.
Inside onCreate
, put the below code:
CameraX.initialize(context: this, Camera2Config.defaultConfig());
Open up your Manifest.xml. in <application>
, give your Java class the name you created. If android:name
is not there, you can create it:
<application
android:name=".CameraXIntApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
>
From your main activity, remove these. They are not required any more:
implements CameraXConfig.Provider from public class MainActivity extends AppCompatActivity implements CameraXConfig.Provider
and this:
@NonNull
@Override
public CameraXConfig getCameraXConfig() {
return Camera2Config.defaultConfig();
}
That's it. This should initialize the camera when your app gets opened.
Upvotes: 1
Reputation: 921
I faced the same issue with beta04 and had to add the camera2 dependency also:
implementation "androidx.camera:camera-camera2:1.0.0-beta04"
Upvotes: 2
Reputation: 2367
I faced the same issue with the latest alpha11 cameraX library, so downgraded to
// CameraX View class
implementation "androidx.camera:camera-view:1.0.0-alpha10"
Also added the camera info parameter
preview?.setSurfaceProvider(viewFinder.createSurfaceProvider(camera?.cameraInfo))
Upvotes: 4