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

Android CameraX - DevFestAhm-2019

pRaNaY
October 12, 2019

Android CameraX - DevFestAhm-2019

Android Camera in simple way.
Android Jetpack CameraX overview and deep dive at DevFest Ahmedabad.
#DevFestAhm #DevFest
Google slides: http://bit.ly/329EBsV

pRaNaY

October 12, 2019
Tweet

More Decks by pRaNaY

Other Decks in Programming

Transcript

  1. What we are going to learn • Current camera API

    • Intro to CameraX • CameraX API overview • Discuss CameraX use-cases
  2. • Camera Api or Camera2 Api? • Difficulties - Switching

    between front & back camera - Support for different Api levels - Support for devices from different Vendors - How to access / release / reset Camera?
  3. Make developer’s life easy with… • Camera2 API compatibility (90%

    of devices). • Lifecycle aware(). • Avoid device-specific code. • CameraX extensions like Portrait, HDR, Night, and Beauty • Readable and minimal line of code. • Fixed Front/Back camera switch crashes. • API 21+ support
  4. Easy usage: Allow you to focus on the task you

    need to get done. Preview: Get an image on the display Image analysis: Access a buffer seamlessly for use in your algorithms, such as to pass into MLKit Image capture: Save high-quality images
  5. Consistency with differents devices : CameraX basic behaviors just work

    with all use cases ... New camera experiences : CameraX has an Extensions
  6. Let's look into CameraX API… Let’s discuss how CameraX will

    help developers to make Camera development easy.
  7. Declaring dependencies: dependencies { // CameraX core library version def

    camerax_version = "1.0.0-alpha06" // CameraX view library version def camerax_view_version = "1.0.0-alpha03" // CameraX extensions library version def camerax_ext_version = "1.0.0-alpha03" implementation "androidx.camera:camera-core:$camerax_version" // If you want to use Camera2 extensions implementation "androidx.camera:camera-camera2:$camerax_version" // If you want to use the Camera View class implementation "androidx.camera:camera-view:$camerax_view_version" // If you want to use Camera Extensions implementation "androidx.camera:camera-extensions:$camerax_ext_version" }
  8. Supported configuration combination Preview Analysis Image Capture Combining use cases

    ✔ ✔ ✔ ✔ ✔ Provide user a preview, take a photo, and analyze the image stream. Take a photo and analyze the image stream. Provide a preview with visual effects applied based on analysis of the images being streamed. Provide a preview with visual effects applied based on analysis of the images being streamed. ✔ ✔ ✔ ✔
  9. The image preview is streamed to this SurfaceTexture when the

    camera becomes active. The SurfaceTexture can be connected to a TextureView or a GLSurfaceView.
  10. // Create configuration object for the Preview use case val

    previewConfig=PreviewConfig.Builder().apply { setTargetAspectRatio(screenAspectRatio) setTargetResolution(screenSize) setLensFacing(CameraX.LensFacing.BACK) }.build() 1. Set Up Preview Config:
  11. // Build the Preview use case using “previewConfig” val preview

    = Preview(previewConfig) preview.setOnPreviewOutputUpdateListener { output: Preview.PreviewOutput -> // To update the SurfaceTexture, we have to remove it and re-add it val parent = textureView.parent as ViewGroup parent.removeView(textureView) parent.addView(textureView,0) textureView.surfaceTexture=previewOutput.surfaceTexture } CameraX.bindToLifecycle(this,preview) // For Preview 2. Display preview:
  12. // Setup image analysis config to detect text from preview

    val analyzerConfig = ImageAnalysisConfig.Builder().apply { // Use a worker thread for image analysis to prevent glitches val analyzerThread = HandlerThread("OCR").apply { start() } setCallbackHandler(Handler(analyzerThread.looper)) setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATE ST_IMAGE)//Latest image in buffer setTargetResolution(Size(1280, 720)) }.build() 1. Set Up Image Analysis Config:
  13. val valueToFind="CameraX" // value To Find while analysing image frame

    val analyzerUseCase = ImageAnalysis(analyzerConfig) analyzerUseCase.analyzer = TextAnalyzer(valueToFind){ //get the callback when text found on preview analysis and capture it. val file = File(externalMediaDirs.first(),"${System.currentTimeMillis()}.jpg") imageCapture.takePicture(file, object :ImageCapture.OnImageSavedListener{ override fun onImageSaved(file: File) { val msg = "Photo capture succeeded: ${file.absolutePath}" } override fun onError(useCaseError: ImageCapture.UseCaseError, message: String, cause: Throwable?) { val msg = "Photo capture failed: $message" } } ) } 2. Perform analysis and save File:
  14. internal class TextAnalyzer( private val identifier: String, private val identifierDetectedCallback:

    () -> Unit ) : ImageAnalysis.Analyzer { override fun analyze(image: ImageProxy?, rotationDegrees: Int) { if (image?.image == null || image.image == null) return // Do you image analysis stuff here } } } 3. Analyzer class:
  15. • Image capture is much simpler using CameraX. User has

    to decide how they need image. • CameraX provides Capture file and in-buffer Captured image.
  16. // Build the image capture use case val imageCapture =

    ImageCapture(imageCaptureConfig) capture_button.setOnClickListener { val file = File(externalMediaDirs.first(),"${System.currentTimeMillis()}.jpg") imageCapture.takePicture(file,object :ImageCapture.OnImageSavedListener{ override fun onImageSaved(file: File) { // File is ready to user } override fun onError(useCaseError: ImageCapture.UseCaseError, message: String, cause: Throwable?) { //Photo capture failed } }) } CameraX.bindToLifecycle(this,preview,imageCapture,analyzerUseCase) // Bind all cases together 2. Capture Image:
  17. Enable extensions: import androidx.camera.extensions.BokehExtender // Create a Builder same as

    in normal workflow. val builder = ImageCaptureConfig.Builder() // Create a Extender object which can be used to apply extension configurations. val bokehImageCapture = BokehImageCaptureExtender.create(builder) // Query if extension is available (optional). if (bokehImageCapture.isExtensionAvailable()) { bokehImageCapture.enableExtension() // Enable the extension if available. } val config = builder.build() val useCase = ImageCapture(config) CameraX.bindToLifecycle(this as LifecycleOwner, useCase)
  18. 1. How we can integrate CameraX library in our project

    2. How we can use camera preview by using Preview and SurfaceTexture 3. How we can Capture Image and save the captured image 4. How we can analyze Camera output frame using ImageAnalysis Summary of what we have learned:
  19. 1. CameraX samples https://github.com/pranaypatel512/CameraXDemo https://github.com/android/camera/tree/master/CameraXBasic https://github.com/hotstu/QRCodeCameraX 2. CameraX Overview: http://bit.ly/2ne0nwu

    3. Understand the CameraX Camera-Support Library (Google I/O'19) http://bit.ly/2Ija7wX 4. Codelab: Getting started with CameraX http://bit.ly/2MdEHZR Further learning...
  20. Pranay Patel Android @ Simform Solutions @pranaypatel_ Thank you! Simform

    Solutions http://devfest.gdgahmedabad.com/ https://www.simform.com
  21. CameraX Android camera in simple way! A Jetpack support library,

    built to help you make camera app development easier