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!

Ac5b39a4b12e8ca2d6e25b9e7cc142ae?s=128

Divya Jain

April 21, 2020
Tweet

More Decks by Divya Jain

Other Decks in Technology

Transcript

  1. CameraX Divya Jain Mobile@Gametime divyajain2405 Ease of camera utility in

    your apps
  2. Variety of apps that use Camera Basic Photos SOCIAL INTERACTION

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

    Use-case based approach • Lifecycle aware • No device specific code • CameraX Extensions
  4. Use Case BAsed Approach • Preview • Image analysis •

    Image Capture
  5. LIBRARY API Configuration • Use Case with configurations • Listeners

    for output data • Lifecycle aware flow
  6. 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 {...})
  7. 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" }
  8. 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)
  9. 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) { } }
  10. BACKPRESSURESTRATEGY • STRATEGY_BLOCK_PRODUCER • STRATEGY_KEEP_ONLY_LATEST

  11. 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)
  12. ADVANCED FEATURES • Tap to Focus • Pinch to Zoom

    • Linear Zoom Slider
  13. 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) }
  14. Live DEMO https://github.com/android/camera-sam ples/tree/master/CameraXBasic https://developer.android.com/training/camerax

  15. Thank you! Divya Jain Mobile@Gametime divyajain2405 Questions?