Camera

CameraX: HDR, Night and Portrait mode

Simplest way to click and analyse photos in Android

Some important notes :

Use case combinations
video

Requirements :

Permissions :

Declare dependencies :

allprojects {
repositories {
google()
jcenter()
}
}
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
...

// CameraX core library using the camera2 implementation
def camerax_version = "1.0.0-beta02"
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-alpha09"
// If you want to use the CameraX Extensions library
implementation "androidx.camera:camera-extensions:1.0.0-alpha09"
// If you want to use the CameraX Lifecycle library
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
}

Use cases

1. Preview :

class App: Application(), CameraXConfig.Provider {
override fun getCameraXConfig(): CameraXConfig {
return Camera2Config.defaultConfig()
}
}
<application
android:name=".App"
...
</application>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
private lateinit var cameraProviderFuture : ListenableFuture<ProcessCameraProvider>private lateinit var previewView:PreviewViewprivate lateinit var preview:Preview
private lateinit var cameraSelector:CameraSelector
private lateinit var cameraProvider: ProcessCameraProvider
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
previewView = findViewById(R.id.previewView)
cameraProviderFuture = ProcessCameraProvider.getInstance(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
...

cameraProviderFuture.addListener(Runnable {
cameraProvider = cameraProviderFuture.get()
bindPreview()
}, ContextCompat.getMainExecutor(this))
setOnClickListener()
}
fun bindPreview(cameraProvider : ProcessCameraProvider) {
preview = Preview.Builder()
.build()

cameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()

var camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)

preview.setSurfaceProvider(previewView.createSurfaceProvider(camera.cameraInfo))
}
Preview Screen

2. Image Capture

<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
...

<ImageView
android:id="@+id/captureBtn"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="bottom|center"
android:src="@drawable/ic_camera_black_24dp"
tools:ignore="ContentDescription" />
</FrameLayout>
private lateinit var imageCapture: ImageCapture
private fun captureImage() {    val builder = ImageCapture.Builder()

imageCapture = builder
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.setTargetRotation(binding.root.display.rotation)
.build()

cameraProvider.bindToLifecycle(this, cameraSelector, imageCapture)
}
private fun bindPreview() {
...
captureImage()
}
private lateinit var captureBtn: ImageViewoverride fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

...
setOnClickListener()
}
private fun setOnClickListener() {
binding.captureBtn.setOnClickListener {
takePhoto()
}
}
private fun takePhoto() {
val dialog = AlertDialog.Builder(this)
.setMessage("Saving picture...")
.setCancelable(false)
.show()
val file = File(getExternalFilesDir(null)?.absolutePath, System.currentTimeMillis().toString() + ".jpg")
file.createNewFile()
val outputFileOptions = ImageCapture.OutputFileOptions.Builder(file).build()
imageCapture.takePicture(outputFileOptions,
Executors.newSingleThreadExecutor(),
object : ImageCapture.OnImageSavedCallback {
override fun onError(exception: ImageCaptureException) {
runOnUiThread {
dialog.dismiss()
Toast.makeText(this@MainActivity, "Error - ${exception.message}",Toast.LENGTH_LONG).show()
}
}
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
runOnUiThread {
dialog.dismiss()
Toast.makeText(this@MainActivity, "Saved successfully !!",Toast.LENGTH_LONG).show()
}
}
})
}
Image capture screen

Add Ons :

private fun captureImage() {
val builder = ImageCapture.Builder()
val bokehImageCapture = BokehImageCaptureExtender.create(builder) // Query if extension is available (optional).
if (bokehImageCapture.isExtensionAvailable(cameraSelector)) {
// Enable the extension if available.
bokehImageCapture.enableExtension(cameraSelector)
}

...
}
private fun captureImage() {
val builder = ImageCapture.Builder()

val hdrImageCapture = HdrImageCaptureExtender.create(builder)
if (hdrImageCapture.isExtensionAvailable(cameraSelector)) {
// Enable the extension if available.
hdrImageCapture.enableExtension(cameraSelector)
}
...
}
If ( YOU_LIKED_THIS_BLOG) {Hit the clap button.} else {Comment the area which you don't like. (Will try to improve that )}

Android Developer @BluSmart