How to check supported aspect ratio and resolution before binding use cases

296 views
Skip to first unread message

Ayush Yadav

unread,
Aug 13, 2023, 11:47:44 AM8/13/23
to Android CameraX Discussion Group, scot...@google.com, Xi Zhang (张熹)
I am trying to create a camera where aspect ratio of imageAnalysis , imageCapture and imagePreview should be same and quality of preview and capture  should be high

In some device preview quality is getting downgraded while binding at 16:9 aspect ratio,so need to check first or create a fall back

heres my code ,where i m currently assign aspect ratio based on devise screen aspect ratio:

 @Suppress("DEPRECATION")
    private fun bindCameraUseCases() {
        // Get screen metrics used to setup camera for full screen resolution
        val metrics = WindowMetricsCalculator.getOrCreate()
            .computeCurrentWindowMetrics(requireActivity()).bounds
        Log.d(TAG, "Screen metrics: ${metrics.width()} x ${metrics.height()}")

        val screenAspectRatio = aspectRatio(metrics.width(), metrics.height())
        Log.d(TAG, "Preview aspect ratio: $screenAspectRatio")
        if (screenAspectRatio == AspectRatio.RATIO_16_9) {
            mViewModel.aspectRatio = "9:16"
            (binding.cameraPreviewLayout.layoutParams as ConstraintLayout.LayoutParams).dimensionRatio =
                "9:16"
        } else {
            mViewModel.aspectRatio = "3:4"
            (binding.cameraPreviewLayout.layoutParams as ConstraintLayout.LayoutParams).dimensionRatio =
                "3:4"
        }

        val rotation = binding.cameraPreview.display.rotation
        Log.d(TAG, "Rotation: $rotation")

        // CameraProvider
        val cameraProvider =
            mCameraProvider ?: throw IllegalStateException("Camera initialization failed.")

        // CameraSelector
        val cameraSelector = CameraSelector.Builder().apply {
            requireLensFacing(mLensFacing)
        }.build()

        // Preview
        mPreview = Preview.Builder().apply {
            setTargetAspectRatio(screenAspectRatio)
            setTargetRotation(rotation)
        }.build()

        // ImageAnalysis
        mImageAnalyzer = ImageAnalysis.Builder().apply {
            setTargetAspectRatio(screenAspectRatio)
            setTargetRotation(rotation)
            setBackpressureStrategy(STRATEGY_KEEP_ONLY_LATEST)
        }.build()

        // ImageCapture
        mImageCapture = ImageCapture.Builder().apply {
            setCaptureMode(CAPTURE_MODE_MINIMIZE_LATENCY)
            setTargetAspectRatio(screenAspectRatio)
            setTargetRotation(rotation)
            if (mFlashMode != NO_FLASH)
                setFlashMode(mFlashMode)
        }.build()

        // Must unbind the use-cases before rebinding them
        cameraProvider.unbindAll()

        try {
            mCamera = cameraProvider.bindToLifecycle(
                this,
                cameraSelector,
                mPreview,
                mImageCapture,
                mImageAnalyzer
            )

            mCameraControl = mCamera?.cameraControl
            // Attach the viewfinder's surface provider to preview use case

            mPreview?.setSurfaceProvider(binding.cameraPreview.surfaceProvider)
            logCameraUseCases()
        } catch (exc: Exception) {
            Log.e(TAG, "Use case binding failed", exc)
        }
    }

Charcoal Chen

unread,
Aug 13, 2023, 10:39:43 PM8/13/23
to Ayush Yadav, Android CameraX Discussion Group, scot...@google.com, Xi Zhang (张熹)
Hi,

Thanks for your question. Determining the target aspect ratio for full screen apps by the window metrics should be fine. I have some questions about the problem you mentioned - "In some device preview quality is getting downgraded while binding at 16:9 aspect ratio,so need to check first or create a fall back".

1. What is the device and what is the selected preview resolution for this device?
2. What is the created fallback that could improve the preview quality issue on the specific device. What is the selected preview resolution after applying your fallback rule?
3. Could you provide some screenshots to explain the "preview quality is getting downgraded" problem? (may comparing with the results after applying the fallback mechanism)

BTW, CameraX supports the new ResolutionSelector API in 1.3.0 and deprecated the legacy setTargetAspectRatio and setTargetResolution API. I'll recommend you to use the new API which can support more settings and can also avoid the migration efforts in the future when the legacy API is removed.

Thanks.

--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/cc51da4d-91e7-451b-a260-5ee12f200451n%40android.com.
Reply all
Reply to author
Forward
0 new messages