CameraXConfig
CameraXConfig
class provides a way to customize CameraX's behaviors within our apps.
Providing CameraXConfig
​
A CameraXConfig
instance will persist for the lifetime of its provider. There are 2 ways to provide CameraXConfig
instance for our applications.
1. Using CameraXConfig.Provider
​
The first way to provide CameraXConfig
instance is by using CameraXConfig.Provider
interface. We can achieve this by creating a custom Application
and make it implements CameraXConfig.Provider
like this:
package com.hanmajid.androidnotebook
import android.app.Application
import android.util.Log
import androidx.camera.camera2.Camera2Config
import androidx.camera.core.CameraXConfig
/**
* Simple [Application] class that provides [CameraXConfig].
*/
class MainApplication : Application(), CameraXConfig.Provider {
override fun getCameraXConfig(): CameraXConfig {
return CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig())
.setMinimumLoggingLevel(Log.INFO)
.build()
}
}
The example shown above provides a CameraXConfig
instance that sets the minimum logging level to Log.INFO
.
Next, you simply need to add this custom Application
to your AndroidManifest.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:name=".MainApplication"
>
</application>
</manifest>
By doing this, now all of your app's CameraX instances will use have their minimum logging level set to Log.INFO
.
2. Using ProcessCameraProvider
​
The second way of providing CameraXConfig
instance is by using ProcessCameraProvider
class. To use this method, first you need to add camera-lifecycle
dependency to your application:
dependencies {
val camerax_version = "1.4.0-beta01"
implementation("androidx.camera:camera-lifecycle:${camerax_version}")
}
Next, you need to call configureInstance()
method once in your app:
package com.hanmajid.androidnotebook
import android.app.Application
import android.util.Log
import androidx.camera.camera2.Camera2Config
import androidx.camera.core.CameraXConfig
import androidx.camera.lifecycle.ProcessCameraProvider
/**
* Simple [Application] class that provides [CameraXConfig].
*/
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
ProcessCameraProvider.configureInstance(
CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig())
.setMinimumLoggingLevel(Log.ERROR)
.build()
)
}
}
Then you only need to add this custom Application
to your AndroidManifest.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:name=".MainApplication"
>
</application>
</manifest>
CameraXConfig
Configurations​
There are several things that we could configure with CameraXConfig
.
Minimum Logging Level​
We can set CameraX's minimum logging level by using setMinimumLoggingLevel
method:
import android.util.Log
import androidx.camera.camera2.Camera2Config
import androidx.camera.core.CameraXConfig
CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig())
.setMinimumLoggingLevel(Log.ERROR)
.build()
Available Cameras Limiter​
With CameraXConfig
, we can also limit the type of cameras that can be used by our application. By doing this, we can optimize CameraX initialization process by not initializing unused camera types. In the example shown below, we are limitting our application to only use default back facing camera:
import androidx.camera.camera2.Camera2Config
import androidx.camera.core.CameraSelector
import androidx.camera.core.CameraXConfig
CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig())
.setAvailableCamerasLimiter(CameraSelector.DEFAULT_BACK_CAMERA)
.build()