Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CameraX - Click of a camera becomes super easy!

CameraX - Click of a camera becomes super easy!

Camera development has been quite a painful task. But now comes the new Jetpack library : CameraX. With a use case based lifecycle aware approach, it provides a consistent & easy to use API. In the talk, I will talk about: Overview & architecture Library structure with 3 main abstracted handles Binding the use cases to the Lifecycle Primary benefits Sample app code to demonstrate how to get started The goal is to help everyone understand how this library aims to strike a balance between abstracting away the difficult bits of managing the camera while allowing flexibility and customization. The “Click of a camera” has just become easier!

Divya Jain

April 21, 2020
Tweet

More Decks by Divya Jain

Other Decks in Technology

Transcript

  1. Variety of apps that use Camera Basic Photos SOCIAL INTERACTION

    IMAGE ANALYSIS SCANNING Filters and Special E ects
  2. WHY CAMERAX • Jetpack support library • Backward compatibility •

    Use-case based approach • Lifecycle aware • No device specific code • CameraX Extensions
  3. Image Capture Example //Configure val imageCapture = ImageCapture.Builder() .setTargetResolution(Size(600, 800)).build()

    //Lifecycle Binding CameraX.bindToLifecycle(lifecycleOwner, cameraSelector, imageCapture) //Use-case with Output Listener imageCapture.takePicture(file, executor, object : ImageCapture.OnImageSavedListener {...})
  4. GETTING STARTED WITH CAMERAX Min Version Requirements: • Android API

    level 21 • Android Architecture Components 1.1.1 Build.gradle: repositories { google() jcenter() } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } dependencies { def camerax_version = "1.0.0-beta03" implementation "androidx.camera:camera-camera2:${camerax_version}" implementation "androidx.camera:camera-lifecycle:${camerax_version" implementation "androidx.camera:camera-view:1.0.0-alpha10" implementation "androidx.camera:camera-extensions:1.0.0-alpha10" }
  5. USE CASE PREVIEW fun buildPreviewUseCase(): Preview { return Preview.Builder() .setTargetResolution(resolution).

    .setTargetRotation(rotation) .build() } val surfaceProvider = previewView.createSurfaceProvider(cameraInfo) preview.setSurfaceProvider(surfaceProvider) var camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)
  6. USE CASE IMAGE ANALYSIS fun buildImageAnalysisUseCase(): ImageAnalysis { return ImageAnalysis.Builder()

    .setTargetAspectRatio(aspectRatio) .setTargetRotation(rotation) .setBackpressureStrategy(strategy) .setImageQueueDepth(queueDepth) .build() } private class LuminosityAnalyzer : ImageAnalysis.Analyzer { override fun analyze(image: ImageProxy) { } }
  7. USE CASE IMAGE CAPTURE val imageCapture = ImageCapture.Builder() setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY) .build()

    fun onClick() { val outputFileOptions = ImageCapture.OutputFileOptions.Builder(File(...)) imageCapture.takePicture(outputFileOptions, cameraExecutor, object : ImageCapture.OnImageSavedCallback { override fun onError(error: ImageCaptureException) {...} override fun onImageSaved(outputFileResults:ImageCapture.OutputFileResults) {...} }) } ImageCapture.Builder.setIoExecuter(executor)
  8. EXTENSIONS import androidx.camera.extensions.BokehExtender fun onCreate() { val builder = ImageCapture.Builder()

    // Create an Extender object which can be used to apply extension val bokehImageCapture = BokehImageCaptureExtender.create(builder) val cameraSelector = CameraSelector.Builder) .requireLensFacing(CameraX.LensFacing.BACK) .build() // Query if extension is available (optional). if (bokehImageCapture.isExtensionAvailable()) { // Enable the extension if available. bokehImageCapture.enableExtension() } // Finish constructing configuration with the same flow as when not using extensions. val useCase = ImageCapture.Builder().build() cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, useCase) }