How to set zoom level for the photo being captured in CameraX?

1,112 views
Skip to first unread message

Mohit Shetty

unread,
Sep 15, 2021, 5:31:54 AM9/15/21
to Android CameraX Discussion Group
I have created an CameraX application (which is currently written in Kotlin). I have managed to implement double tap to zoom and pinch to to zoom in my app. However, when I actually capture a photo and later view it later in the gallery, the zoom level seems to be unchanged (default full zoom). I had manually implemented targetRotation when the orientation of the device changes (with portrait lock), but couldn't find anything similar for zoom level. Could someone please help me with this issue?

Here's the source code of the app: https://github.com/MHShetty/grapheneos-cameraa

Scott Nien

unread,
Sep 15, 2021, 12:04:38 PM9/15/21
to Mohit Shetty, Android CameraX Discussion Group
Hi Mohit, 

Are you using CameraControl.setZoomRatio API ?    BTW, your link is 404 not found. maybe there is some typo in the url. 

On Wed, Sep 15, 2021 at 5:31 PM Mohit Shetty <shetty...@gmail.com> wrote:
I have created an CameraX application (which is currently written in Kotlin). I have managed to implement double tap to zoom and pinch to to zoom in my app. However, when I actually capture a photo and later view it later in the gallery, the zoom level seems to be unchanged (default full zoom). I had manually implemented targetRotation when the orientation of the device changes (with portrait lock), but couldn't find anything similar for zoom level. Could someone please help me with this issue?

Here's the source code of the app: https://github.com/MHShetty/grapheneos-cameraa

--
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/7802a5eb-457d-4980-b56a-9222c333e99dn%40android.com.

Mohit Shetty

unread,
Sep 16, 2021, 11:30:09 AM9/16/21
to Android CameraX Discussion Group, Scott Nien, Android CameraX Discussion Group, Mohit Shetty
Oh...I might have accidentally typed an extra 'a' while sending the message. Sorry for the inconvenience. 

Here's the link to the repository: https://github.com/MHShetty/grapheneos-camera


Scott Nien

unread,
Sep 17, 2021, 3:59:41 AM9/17/21
to Mohit Shetty, Android CameraX Discussion Group
Hi Mohit. 

I just ran your app.  I think it is probably the preview is not fully displayed in the view (it is cropped).  And so when you zoom-in,  you see the captured photo has a different FOV than the preview and think the zoom is not applied.  But in fact,  these are just because the preview is cropped.    You can set the PreviewView scaleType to fitCenter (this will not crop the preview) and see if this is the case. 

Scott

Mohit Shetty

unread,
Sep 21, 2021, 4:11:21 PM9/21/21
to Android CameraX Discussion Group, Scott Nien, Android CameraX Discussion Group, Mohit Shetty
Hey Scott,

Yes, you were right. Setting the scaleType to fitCenter resolves the issue. But is there any way we can solve this while filing the whole screen..maybe by getting the cropped frame. Also, if I go with the fitCenter method how can I ensure they always get snapped vertically without getting cropped?

Thanks in advance.

Scott Nien

unread,
Sep 21, 2021, 10:36:16 PM9/21/21
to Mohit Shetty, Android CameraX Discussion Group
The definition is fitCenter is  "Scale the preview, maintaining the source aspect ratio, so it is entirely contained
        within the PreviewView, and center it inside the view.".   So it could be snapped vertically or horizontally but it is definitely not cropped. 

If you want to get the cropped frame just identical to what you saw in the preview,  then you can use the ViewPort API or use CameraController 

Hope this helps. 
Scott

Reply all
Reply to author
Forward
0 new messages