Hello everyone,
Goal: I'm trying to implement a dynamic cropping feature for the CameraXViewfinder in a Jetpack Compose project. My goal is to achieve an effect similar to the auto-framing feature in apps like Google Duo, where the camera digitally pans and zooms to keep a person's face centered. To do this, I want to define a normalized Rect (e.g., for the right half of the image) and have the CameraXViewfinder render only that specific section, scaled up to fill the component's bounds.
What I've tried:
ViewPort: I've looked into using a ViewPort, but its ScaleType options (FILL_CENTER, FILL_START, etc.) seem designed for cropping from the center or edges. My use case requires cropping to an arbitrary and dynamic Rect, so this doesn't seem flexible enough.
Modifier.graphicsLayer: My main attempt was to apply scaling and translation transformations to the CameraXViewfinder using Modifier.graphicsLayer. However, as soon as the transformations are applied, the camera preview turns black. My hypothesis is that the underlying SurfaceView used by CameraXViewfinder is not fully compatible with these kinds of graphics transformations.
My Question:
What is the recommended and most efficient way to achieve this kind of custom, dynamic cropping on a live camera preview? I am particularly looking for a hardware-accelerated (GPU-based) solution to maintain a high framerate and good performance.
Is using the ImageAnalysis use case—getting each frame, cropping the Bitmap manually, and displaying it in an Image composable—the only reliable way to do this? Or is there a more direct method to manipulate the Preview stream itself?
Thank you for any advice or insights!
Daniel