feat: Support focus depth/distance
This PR add support for focus depth/distance on android and iOS.
This is based on this work: https://github.com/Fabi019/hid-barcode-scanner/blob/a38601ee94941304eaf515baa89cd5dbfc9ee092/app/src/main/java/dev/fabik/bluetoothhid/ui/model/CameraViewModel.kt#L60
Changes
- New method on the CameraSession to set the focus depth. It also change the autofocus to off, which is convenient bacause the end user can use the app with autofocus, then decide to focus manually. The focus max need to be aligned with
device.minFocusDistancealready available.
Tested on
Samsung S23. The feature was made for the Pl@ntNet plant identification app, and was used by without issues by a user base of around 5 millions on android. iOS equivalent was tested on an iPhone SE 2022, and will be tested in production in September 2025.
Related issues
Relates to #1809
Demo
https://github.com/mrousavy/react-native-vision-camera/assets/662377/be5066d9-2265-47f7-a5cf-a22faed90e1d
The latest updates on your projects. Learn more about Vercel for Git ↗︎
| Name | Status | Preview | Comments | Updated (UTC) |
|---|---|---|---|---|
| react-native-vision-camera | ✅ Ready (Inspect) | Visit Preview | 💬 Add feedback | Jun 4, 2024 3:58pm |
Tested on multiple devices (pixel 6,cheap readmi, s23) and it worked without issues!
Hey - thank you for your contribution this looks really cool! ❤️
I do think however that focusDepth should not be a function, but instead a view property on <Camera>. That way it can be animated using Reanimated/Native Props and you can create cool sliders or similar.
Also I can only merge this if it has both iOS and Android implementations.
Can you make those changes? Thanks!
ok for moving it as a prop, I've duplicated the logic initialy from the focus method, so I thought it made sense to do it that way. I'll work on the iOS equivalent locally and update the PR later next week.
Quick question, do you think I should adjust the auto contrast stuff? It seems that on some device, the PR disable the auto exposure (AWB?), but not on all devices. I may add a button to toggle between a few exposure settings in Pl@ntNet but let me know what you think.
Ah yeah I can see how it is very imperative, maybe a prop isn't the right idea after all. Is there no way to set focus distance via CameraX? I kinda want to avoid such Camera2 interop features...
It seems that on some device, the PR disable the auto exposure (AWB?), but not on all devices
That's why I want to avoid Camera2 interop 😅
I went through a lot of bullshit in VisionCamera V3 when I used Camera2 directly. It's such a wanky API that I want to stay away from it and handle everything through CameraX. If there's no way to use CameraX for it I don't know if I really want to merge this into main...
I looked hard into cameraX and found no similar method, only having to use the interop methods. I understand how you feel with visionV3, i thought it could help us too, went it prod, and had a lot of feedback. But v4 seems to work great so far (already in prod, no issue)
I really like the feature and I really want to have this in VisionCamera, but I am honestly not sure if it's a good idea to use Camera2.. sorry...
Maybe we can create a feature request at the CameraX issue tracker and hope they add it to CameraX, then easily add it to VisionCamera?
I'll think about this a bit. Maybe I can merge it, after all it's entirely optional anyways.
From a user perspective, a prop sounds more logical. From an implementation perspective, it's an imperative function.
Either way thank you for the contribution, I'll take a few days to think about this and then maybe pick this up again - maybe I'll also just do the iOS implementation then.
Btw.; great news hearing that VisionCamera powers your app on thousands of devices out there. 💪
Ticket created: https://issuetracker.google.com/issues/346918497
Awesome, thank you! Let's hear what they say
Hey guys Any updates on this?
I'll try to update the PR and add iOS support in the summer, the patch is still working fine for our users.