-
Notifications
You must be signed in to change notification settings - Fork 463
Fixed camera preview for Xiaomi devices #1780
Conversation
Further investigation required but the xiaomi has no "optimal sizes" for preview and is attempting to set the preview size to 4224x3136, which as a guess I would say is a raw resolution not accounting the dpi
@Axemasta thank you for this submission! The changes look good to me especially given they don't effect the existing checks for a matching size (I do wonder if we could improve upon that when this shifts to the Maui Community Toolkit but that is outside of the scope of this change). It will be nice to see if the 2 people that you tagged on the issue are able to confirm the fix, I will aim to test on some other devices I have here tomorrow to double check all is fine, I don't possess a Xiaomi device though. |
To address your comments... in reverse order 😊:
I'm fortunate to have access to an affected device, mine is a Xiaomi Note 7 running Android 9. I potentially have access to more affected devices through a fork which I will add the change & observe if devices can access the camera correctly. I've tested on 3 devices:
Both the samsungs return valid surface mappings: The Xiaomi does return mappings but the aspect ratios are off, causing the checking code to completely fallthrough. I can't confirm whether its a Xiaomi or an Android flavour issue but it seems the fallback at least works for affected devices.
When looking into this issue I did some research into how the Camera2 api should be implemented and it looks like the lastest google sample implements it in a different way. The current implementation matches the following: Both of these use the The current google samples repo does not contain this method and whilst I've not completely wrapped my head around the differences between old & new, it appears to be using a method called Since changing the current implementation would affect more devices, perhaps it would be something that could be revisited in another pr to update the android renderers to use the latest & greatest from the google sample? |
@Axemasta thanks for the guidance on the differences between this implementation and googles docs. I will add a note on the MAUI Community Toolkit proposal so we certainly don't miss this in the new implementation (CommunityToolkit/Maui#106) Given the rest appears to work and testing my devices here (Goole Pixel 3a, One Plus Nord N100 and some cheap Umidigi device) has been fine I suspect we might not worry about changing things in this repo. |
@Axemasta thanks again for this effort. It really is appreciated! |
Here is a Camera2 preview control that uses the preview builder to set the preview window. This is possibly the implementation we'd want to use for the MAUI version of the control ref. |
Description of Bug
CameraView
preview does not work correctly for front facing camera on Xiaomi Note 7/8 devices.This appears to be a niche issue on Xiaomi devices of a suitable age (the newer ones don't appear to show the issue) where the front camera cannot be used to preview / capture media. Instead of displaying a preview, the media capture failed event is raised & the camera cannot be used.
Below is the XCT sample running on a Xiaomi Redmi Note 7 & a Samsung A21S. The Samsung has no issues displaying the front camera and the Xiaomi cannot display it.
Xiaomi with issue
Redmi.Note_7.2022-01-02.14-50-26.mp4
Samsung without issue
SM.A217F.2022-01-02.14-52-33.mp4
Upon debugging the source code with the affected device, I found that the
ChooseOptimalSize
method was responsible for returning a resolution that could not be displayed.The following line on the affected device returns a size of
4224x3136
and when the camera is initialized the following errors are logged to the debug console:After some reading up on the
Camera2
api it looks like this lib is following the example set by Google. Stepping through the issue comes with the aspect ratio check which always seems to fail on this particular device. Maybe it is an Android flavour conflict?To sort of the issue I've added a check to see if the fallback value used is a valid size (smaller than the max preview), which will be used instead of the invalid value. This code change shouldn't affect working devices since it only alters the fallback logic and my Xiaomi is now happily giving me both cameras!
Redmi.Note_7.2022-01-02.15-08-08.mp4
Issues Fixed
Behavioral Changes
CameraView
should work on Xiaomi Redmi Note 7/8 devices 🥳I removed the else clause on this line & made the failed to retrieve camera state return early, sorry guys I love to reduce nesting in methods 😉
PR Checklist
approved