Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Mastering CameraX API

Mastering CameraX API

Himanshu Singh

October 13, 2019
Tweet

More Decks by Himanshu Singh

Other Decks in Technology

Transcript

  1. CameraActivity override fun onCreate(savedInstanceState: Bundle?) { ... if (allPermissionsGranted()) {

    textureView.post { startCamera() } } else { ActivityCompat.requestPermissions( this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS) } viewFinder.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> updateTransform() } }
  2. CameraActivity private fun startCamera() { val previewConfig = PreviewConfig.Builder().apply {

    setTargetAspectRatio(Rational(1, 1)) setTargetResolution(Size(640, 640)) }.build() val preview = Preview(previewConfig) preview.setOnPreviewOutputUpdateListener { val parent = textureView.parent as ViewGroup parent.removeView(textureView) parent.addView(textureView, 0) textureView.surfaceTexture = it.surfaceTexture updateTransform() } CameraX.bindToLifecycle(this, preview) }
  3. CameraActivity private fun startCamera() { val previewConfig = PreviewConfig.Builder().apply {

    setTargetAspectRatio(Rational(1, 1)) setTargetResolution(Size(640, 640)) }.build() val preview = Preview(previewConfig) preview.setOnPreviewOutputUpdateListener { val parent = textureView.parent as ViewGroup parent.removeView(textureView) parent.addView(textureView, 0) textureView.surfaceTexture = it.surfaceTexture updateTransform() } CameraX.bindToLifecycle(this, preview) }
  4. CameraActivity private fun startCamera() { val previewConfig = PreviewConfig.Builder().apply {

    setTargetAspectRatio(Rational(1, 1)) setTargetResolution(Size(640, 640)) }.build() val preview = Preview(previewConfig) preview.setOnPreviewOutputUpdateListener { val parent = textureView.parent as ViewGroup parent.removeView(textureView) parent.addView(textureView, 0) textureView.surfaceTexture = it.surfaceTexture updateTransform() } CameraX.bindToLifecycle(this, preview) }
  5. CameraActivity private fun updateTransform() { val matrix = Matrix() val

    centerX = textureView.width / 2f val centerY = textureView.height / 2f val rotationDegrees = when(textureView.display.rotation) { Surface.ROTATION_0 -> 0 Surface.ROTATION_90 -> 90 Surface.ROTATION_180 -> 180 Surface.ROTATION_270 -> 270 else -> return } matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY) // Finally, apply transformations to our TextureView textureView.setTransform(matrix) }
  6. CameraActivity private fun updateTransform() { val matrix = Matrix() val

    centerX = textureView.width / 2f val centerY = textureView.height / 2f val rotationDegrees = when(textureView.display.rotation) { Surface.ROTATION_0 -> 0 Surface.ROTATION_90 -> 90 Surface.ROTATION_180 -> 180 Surface.ROTATION_270 -> 270 else -> return } matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY) textureView.setTransform(matrix) }
  7. private fun startCamera() { buttonCaptureImage.setOnClickListener { val file = File(externalMediaDirs.first(),

    "camerax_demo_${System.currentTimeMillis()}.jpg") imageCapture.takePicture(file, object : ImageCapture.OnImageSavedListener { override fun onError(error: ImageCapture.UseCaseError,message: String, exc: Throwable?) { //handle Error } override fun onImageSaved(file: File) { //image saved } }) } CameraActivity
  8. fun startCamera() { val analysisConfig = ImageAnalysisConfig.Builder() .setTargetAspectRatio(aspectRatio) .setTargetRotation(rotation) .setTargetResolution(resolution)

    .setImageReaderMode(readerMode) .setImageQueueDepth(queueDepth) .build() val analysis = ImageAnalysis(analysisConfig) analysis.setAnalyzer { image, rotationDegrees -> //handle your task } CameraX.bindToLifecycle(this,preview,imageCapture,analysis) } CameraActivity
  9. fun startCamera() { val analysisConfig = ImageAnalysisConfig.Builder() .setTargetAspectRatio(aspectRatio) .setTargetRotation(rotation) .setTargetResolution(resolution)

    .setImageReaderMode(readerMode) .setImageQueueDepth(queueDepth) .build() val analysis = ImageAnalysis(analysisConfig) analysis.setAnalyzer { image, rotationDegrees -> //handle your task } CameraX.bindToLifecycle(this,preview,imageCapture,analysis) } CameraActivity
  10. fun startCamera() { val analysisConfig = ImageAnalysisConfig.Builder() ..... .build() val

    analysis = ImageAnalysis(analysisConfig) analysis.setAnalyzer { image, rotationDegrees -> //handle your task } CameraX.bindToLifecycle(this,preview,imageCapture,analysis) } CameraActivity
  11. Bokeh, Night-Mode, HDR fun startCamera(){ val builder = ImageCaptureConfig.Builder() val

    bokehImageCapture = BokehImageCaptureExtender.create(builder) // Query if extension is available (optional). if (bokehImageCapture.isExtensionAvailable()) { // Enable the extension if available. bokehImageCapture.enableExtension() } val config = builder.build() val imageCapture = ImageCapture(config) CameraX.bindToLifecycle(this, imageCapture) }