Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
82 slides
-
Camera Development can be difficult and frustrating • Supporting different
API versions • Providing consistency across devices -
Camera Development can be difficult and frustrating • Supporting different
API versions • Providing consistency across devices • Understanding the complex camera API -
Camera Development can be difficult and frustrating • Supporting different
API versions • Providing consistency across devices • Understanding the complex camera API CameraX support library aims to help solve these problems -
CameraX • Backwards compatible to L (90% of devices) •
Consistent behavior across devices -
CameraX • Backwards compatible to L (90% of devices) •
Consistent behavior across devices • Easy to use APIs -
// create preview val previewConfig = PreviewConfig.Builder().build() .setTargetRotation(Surface.ROTATION_180) val preview
= Preview(previewConfig)wner, preview) -
// attach output to view val previewConfig = PreviewConfig.Builder().build() .setTargetRotation(Surface.ROTATION_180)
val preview = Preview(previewConfig) preview.setOnPreviewOutputUpdateListener { previewOutput: Preview.PreviewOutput? -> //your code here. e.g. use previewOutput?.surfaceTexture //and post to a GL renderer. } CameraX.bindToLifecycle(this as LifecycleOwner, preview) -
// attach preview to lifecycle val previewConfig = PreviewConfig.Builder().build() .setTargetRotation(Surface.ROTATION_180)
val preview = Preview(previewConfig) preview.setOnPreviewOutputUpdateListener { previewOutput: Preview.PreviewOutput? -> //your code here. e.g. use previewOutput?.surfaceTexture //and post to a GL renderer. } CameraX.bindToLifecycle(this as LifecycleOwner, preview) -
// display preview on screen val previewConfig = PreviewConfig.Builder().build() .setTargetRotation(Surface.ROTATION_180)
val preview = Preview(previewConfig) preview.setOnPreviewOutputUpdateListener { previewOutput: Preview.PreviewOutput? -> //your code here. e.g. use previewOutput?.surfaceTexture //and post to a GL renderer. } CameraX.bindToLifecycle(this as LifecycleOwner, preview) -
// create image analysis val imageAnalysisConfig = ImageAnalysisConfig.Builder() .setTargetResolution(Size(1280, 720))
.build() val imageAnalysis = ImageAnalysis(imageAnalysisConfig) -
// attach output val imageAnalysisConfig = ImageAnalysisConfig.Builder() .setTargetResolution(Size(1280, 720)) .build()
val imageAnalysis = ImageAnalysis(imageAnalysisConfig) imageAnalysis.setAnalyzer({ image: ImageProxy, rotationDegrees: Int -> val cropRect = image.cropRect // do some analysis }) -
// attach image analysis & preview to lifecycle val imageAnalysisConfig
= ImageAnalysisConfig.Builder() .setTargetResolution(Size(1280, 720)) .build() val imageAnalysis = ImageAnalysis(imageAnalysisConfig) imageAnalysis.setAnalyzer({ image: ImageProxy, rotationDegrees: Int -> val cropRect = image.cropRect // do some analysis }) CameraX.bindToLifecycle(this as LifecycleOwner, imageAnalysis, preview) -
// full setup to process images val imageAnalysisConfig = ImageAnalysisConfig.Builder()
.setTargetResolution(Size(1280, 720)) .build() val imageAnalysis = ImageAnalysis(imageAnalysisConfig) imageAnalysis.setAnalyzer({ image: ImageProxy, rotationDegrees: Int -> val cropRect = image.cropRect // do some analysis }) CameraX.bindToLifecycle(this as LifecycleOwner, imageAnalysis, preview) -
// create image capture val imageCaptureConfig = ImageCaptureConfig.Builder() .setTargetResolution(Size(1280, 720))
.build() val imageCapture = ImageCapture(imageCaptureConfig) -
// bind all use cases val imageCaptureConfig = ImageCaptureConfig.Builder() .setTargetResolution(Size(1280,
720)) .build() val imageCapture = ImageCapture(imageCaptureConfig) CameraX.bindToLifecycle(this as LifecycleOwner, imageCapture, imageAnalysis, preview) -
// full setup to capture images val imageCaptureConfig = ImageCaptureConfig.Builder()
.setTargetResolution(Size(1280, 720)) .build() val imageCapture = ImageCapture(imageCaptureConfig) CameraX.bindToLifecycle(this as LifecycleOwner, imageCapture, imageAnalysis, preview) -
// on user action fun onClick() { val file =
File(..) imageCaptureUseCase.takePicture(file, object : ImageCaptureUseCase.OnImageSavedListener { override fun onError(error: ImageCaptureUseCase.UseCaseError, message: String, exc: Throwable?) { //insert your code here } override fun onImageSaved(file: File) { //insert your code here } }) } -
// on user action save a picture fun onClick() {
val file = File(..) imageCapture.takePicture(file, object : ImageCaptureUseCase.OnImageSavedListener { override fun onError(error: ImageCaptureUseCase.UseCaseError, message: String, exc: Throwable?) { //insert your code here } override fun onImageSaved(file: File) { //insert your code here } }) } -
// save a picture on user action fun onClick() {
val file = File(..) imageCapture.takePicture(file, object : ImageCapture.OnImageSavedListener { override fun onError(error: ImageCapture.UseCaseError, message: String, exc: Throwable?) { //insert your code here } override fun onImageSaved(file: File) { //insert your code here } }) } -
CameraX sample app g.co/camerax/sample CameraX dev mailing list g.co/camerax/developers CameraX
StackOverflow tag stackoverflow.com/tags/android-camerax -
CameraX sample app • Under Android camera samples GitHub repo
• Monitoring all repo issues and pull requests -
CameraX sample app • Under Android camera samples GitHub repo
• Monitoring all repo issues and pull requests • Sample issues != CameraX library issues -
CameraX sample app (cont) • Very basic indeed • Preview,
image capture and image analysis -
CameraX sample app (cont) • Very basic indeed • Preview,
image capture and image analysis • Future work: vendor extensions and video record -
CameraX sample app • Very basic indeed • Preview, image
capture and image analysis • Future work: vendor extensions and video record -
Handling state changes beyond Lifecycle • Preview output update (CameraX
internals) • Layout (TextureView lifecycle) -
Handling state changes beyond Lifecycle • Preview output update (CameraX
internals) • Layout (TextureView lifecycle) • Display (180 rotations, display removed / attached) -
Handling state changes beyond Lifecycle • Preview output update (CameraX
internals) • Layout (TextureView lifecycle) • Display (180 rotations, display removed / attached) • Camera hardware (USB, retractable) -
Handling state changes beyond Lifecycle • Preview output update (CameraX
internals) • Layout (TextureView lifecycle) • Display (180 rotations, display removed / attached) • Camera hardware (USB, retractable) -
// Preview output update listener val preview: Preview = ...
val viewFinder: TextureView = ... preview.setOnPreviewOutputUpdateListener { previewOutput: Preview.PreviewOutput? -> // Remove it and re-add view (important!) val parent = viewFinder.parent as ViewGroup parent.removeView(viewFinder) parent.addView(viewFinder, 0) // Update internal Surface object viewFinder.surfaceTexture = previewOutput.surfaceTexture // Apply preview output transformations // TODO: Use PreviewOutput textureSize and rotationDegrees here } -
// Preview output update listener val preview: Preview = ...
val viewFinder: TextureView = ... preview.setOnPreviewOutputUpdateListener { previewOutput: Preview.PreviewOutput? -> // Remove it and re-add view (important!) val parent = viewFinder.parent as ViewGroup parent.removeView(viewFinder) parent.addView(viewFinder, 0) // Update internal Surface object viewFinder.surfaceTexture = previewOutput.surfaceTexture // Apply preview output transformations // TODO: Use PreviewOutput textureSize and rotationDegrees here } -
// Preview output update listener val preview: Preview = ...
val viewFinder: TextureView = ... preview.setOnPreviewOutputUpdateListener { previewOutput: Preview.PreviewOutput? -> // Remove it and re-add view (important!) val parent = viewFinder.parent as ViewGroup parent.removeView(viewFinder) parent.addView(viewFinder, 0) // Update internal Surface object viewFinder.surfaceTexture = previewOutput.surfaceTexture // Apply preview output transformations // TODO: Use PreviewOutput textureSize and rotationDegrees here } -
// Preview output update listener val preview: Preview = ...
val viewFinder: TextureView = ... preview.setOnPreviewOutputUpdateListener { previewOutput: Preview.PreviewOutput? -> // Remove it and re-add view (important!) val parent = viewFinder.parent as ViewGroup parent.removeView(viewFinder) parent.addView(viewFinder, 0) // Update internal Surface object viewFinder.surfaceTexture = previewOutput.surfaceTexture // Apply preview output transformations // TODO: Use PreviewOutput textureSize and rotationDegrees here } -
// Preview output update listener val preview: Preview = ...
val viewFinder: TextureView = ... preview.setOnPreviewOutputUpdateListener { previewOutput: Preview.PreviewOutput? -> // Remove it and re-add view (important!) val parent = viewFinder.parent as ViewGroup parent.removeView(viewFinder) parent.addView(viewFinder, 0) // Update internal Surface object viewFinder.surfaceTexture = previewOutput.surfaceTexture // Apply preview output transformations // TODO: Use PreviewOutput textureSize and rotationDegrees here } -
Handling state changes beyond Lifecycle • Preview output update (CameraX
internals) • Layout (TextureView lifecycle) • Display (180 rotations, display removed / attached) • Camera hardware (USB, retractable) -
Handling state changes beyond Lifecycle • Preview output update (CameraX
internals) • Layout (TextureView lifecycle) • Display (180 rotations, display removed / attached) • Camera hardware (USB, retractable) -
// Layout change listener val viewFinder: TextureView = ... viewFinder.addOnLayoutChangeListener
{ view, left, top, right, bottom, _, _, _, _ -> val newDimens = Size(right - left, bottom - top) // Apply preview output transformations // TODO: Use `newDimens` here } -
// Layout change listener val viewFinder: TextureView = ... viewFinder.addOnLayoutChangeListener
{ view, left, top, right, bottom, _, _, _, _ -> val newDimens = Size(right - left, bottom - top) // Apply preview output transformations // TODO: Use `newDimens` here } -
// Layout change listener val viewFinder: TextureView = ... viewFinder.addOnLayoutChangeListener
{ view, left, top, right, bottom, _, _, _, _ -> val newDimens = Size(right - left, bottom - top) // Apply preview output transformations // TODO: Use `newDimens` here } -
Handling state changes beyond Lifecycle • Preview output update (CameraX
internals) • Layout (TextureView lifecycle) • Display (180 rotations, display removed / attached) • Camera hardware (USB, retractable) -
Handling state changes beyond Lifecycle • Preview output update (CameraX
internals) • Layout (TextureView lifecycle) • Display (180 rotations, display removed / attached) • Camera hardware (USB, retractable) -
// Display listener val viewFinder: TextureView = ... val viewFinderDisplayId:
Int = viewFinder.display.displayId -
// Display listener val viewFinder: TextureView = ... val viewFinderDisplayId:
Int = viewFinder.display.displayId val displayListener = object : DisplayManager.DisplayListener { override fun onDisplayAdded(displayId: Int) = Unit override fun onDisplayRemoved(displayId: Int) = Unit override fun onDisplayChanged(displayId: Int) { if (displayId == viewFinderDisplayId) { val display = viewFinder.display // Apply preview output transformations // TODO: Use `display.rotation` here } } } -
// Display listener val viewFinder: TextureView = ... val viewFinderDisplayId:
Int = viewFinder.display.displayId val displayListener = object : DisplayManager.DisplayListener { override fun onDisplayAdded(displayId: Int) = Unit override fun onDisplayRemoved(displayId: Int) = Unit override fun onDisplayChanged(displayId: Int) { if (displayId == viewFinderDisplayId) { val display = viewFinder.display // Apply preview output transformations // TODO: Use `display.rotation` here } } } -
// Display listener val viewFinder: TextureView = ... val viewFinderDisplayId:
Int = viewFinder.display.displayId val displayListener = object : DisplayManager.DisplayListener { override fun onDisplayAdded(displayId: Int) = Unit override fun onDisplayRemoved(displayId: Int) = Unit override fun onDisplayChanged(displayId: Int) { if (displayId == viewFinderDisplayId) { val display = viewFinder.display // Apply preview output transformations // TODO: Use `display.rotation` here } } } -
// Preview output transformation // Option 1: apply rotation to
view finder (on top of previous transformations) val matrix: Matrix = ... matrix.postRotate(-rotationDegrees, centerX, centerY) -
// Preview output transformation // Option 1: apply rotation to
view finder (on top of previous transformations) val matrix: Matrix = ... matrix.postRotate(-rotationDegrees, centerX, centerY) // Option 2: apply rotation to preview use case, which will trigger // Preview.OnPreviewOutputUpdateListener val preview: Preview = ... preview.setTargetRotation(display.rotation) -
// Preview output transformation // Option 1: apply rotation to
view finder (on top of previous transformations) val matrix: Matrix = ... matrix.postRotate(-rotationDegrees, centerX, centerY) // Option 2: apply rotation to preview use case, which will trigger // Preview.OnPreviewOutputUpdateListener val preview: Preview = ... preview.setTargetRotation(display.rotation) // Use PreviewOutput.rotationDegrees in Preview.OnPreviewOutputUpdateListener // Alternatively, get the view's display and always use display.rotation -
Handling state changes beyond Lifecycle • Preview output update (CameraX
internals) • Layout (TextureView lifecycle) • Display (180 rotations, display removed / attached) • Camera hardware (USB, retractable) -
Handling state changes beyond Lifecycle • Preview output update (CameraX
internals) • Layout (TextureView lifecycle) • Display (180 rotations, display removed / attached) • Camera hardware (USB, retractable) -
Handling state changes beyond Lifecycle • Preview output update (CameraX
internals) • Layout (TextureView lifecycle) • Display (180 rotations, display removed / attached) • Camera hardware (USB, retractable)