Android Camera in simple way.
Android Jetpack CameraX overview and deep dive at DevFest Ahmedabad.
#DevFestAhm #DevFest
Google slides: http://bit.ly/329EBsV
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
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
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" }
✔ ✔ ✔ ✔ ✔ 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. ✔ ✔ ✔ ✔
= 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:
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:
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:
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)
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:
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...