Fix Android camera selection using index-based stable IDs#279
Fix Android camera selection using index-based stable IDs#279jfversluis merged 8 commits intomainfrom
Conversation
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
…pes and debug logging Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
|
@copilot build error: D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\Platforms\Android\CameraManager.android.cs(13,31): error CS0234: The type or namespace name 'Interop' does not exist in the namespace 'AndroidX.Camera.Camera2' (are you missing an assembly reference?) [D:\a\ZXing.Net.Maui\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj::TargetFramework=net9.0-android] Isnt there a better way to do this without using interop? |
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
You're right - Camera2 interop isn't available in the current AndroidX.Camera.Camera2 package version. I've changed the approach to use index-based stable IDs instead (format: "front-0", "rear-0", "rear-1", etc.) which only requires the existing CameraX API. This is simpler, has no dependency issues, and the IDs remain stable as long as camera enumeration order is consistent (which it is in CameraX). Fixed in commit 0b6f428. |
|
Hi @jfversluis, I needed this functionality for a project I am working on, and I have found part of the issue, and part of the solution. After cloning the branch, I still had the same issue as was reported initially - on a Samsung A35 with three rear cameras and one front camera, it reported two rear and two front cameras. When these cameras are selected, the two front facing options both activate the front camera, but the two rear facing options activate the "main" camera and wide angle camera. After investigating this further, it seems Samsung only exposes 2 of the 3 rear cameras, (normal and wide angle, not the telephoto), and for some reason there are two entries of the front facing camera? I did manage to fix the camera selection not working, by changing |
|
@copilot please look at #279 (comment) and apply that to this PR |
I'm unable to access the referenced comment at #279 (comment) due to network restrictions. Could you please share the specific changes or code that needs to be applied from that comment? Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot here is the comment inline from a user on this PR, cn you please apply this suggestion to this PR?
|
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Applied the fix to use a local |
|
@hwmoolmanDM Could you try the latest artifacts please? |
|
@hwmoolmanDM if you could let me know that would be amazing! |
There was a problem hiding this comment.
Pull request overview
This PR fixes camera selection on Android by implementing stable, index-based camera IDs instead of relying on unstable hash-based identifiers from CameraSelector.ToString(). The fix ensures that selected cameras persist correctly across sessions and properly handles devices with multiple cameras of the same facing direction.
Key Changes:
- Introduced stable camera ID format (
"front-{index}","rear-{index}") based on lens facing and enumeration order - Fixed state corruption issue by using a local variable before updating the
_cameraSelectorfield - Updated camera selection logic to parse stable IDs and match cameras by facing direction and index
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs | Implements stable camera ID generation and fixes camera selection logic with local variable pattern |
| CAMERA_SELECTION.md | Updates documentation to reflect the new index-based stable ID approach |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I think we should merge this in next version. With this code atleast switching cameras are working. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Camera Selection Fix for Android
Problem
On Android, camera selection fails because
CameraSelector.ToString()returns non-stable hash-based identifiers. This prevents cameras from being matched correctly when selected, causing all selections to revert to the default rear camera.Solution - Index-Based Stable IDs (No Interop Required)
Instead of using Camera2 interop (which has dependency issues), this fix uses a simpler approach:
"front-0","rear-0","rear-1","rear-2", etc.Latest Fix
Fixed camera selection not working by using a local
selectedCameraSelectorvariable instead of directly modifying the_cameraSelectorfield. This ensures the camera selector is only updated after successfully finding the target camera, preventing state corruption when camera selection fails.Changes
CameraManager.android.cs:
GetAvailableCameras(): Creates stable camera IDs in format"front-{index}"or"rear-{index}"and generates descriptive namesUpdateCamera():selectedCameraSelectorto build camera selection logic_cameraSelectorfield after successfully finding the target cameraCAMERA_SELECTION.md:
Expected Behavior
Before (Galaxy S25 with 3 rear + 1 front camera):
After:
Selected cameras now persist correctly across sessions and restore from saved preferences.
Technical Notes
Original prompt
This section details on the original issue you should resolve
<issue_title>0.6.0 – On Android, selecting a camera always defaults to the rear camera, while it works correctly on iOS.</issue_title>
<issue_description>I’m using a barcode scanner inside a popup from CommunityToolkit.Maui. In the center of the popup, I’ve added a button that allows the user to manually select a camera. The app lists all available cameras, and the user can choose one and save it as their default camera.
This works perfectly on iOS, but on Android I’m getting an incorrect list of cameras. For example, on my Galaxy S25, which has three rear cameras and one front camera, the library reports two front and two back cameras instead. Additionally, no matter which camera I select, it always switches back to the rear camera.
Below is the list of cameras returned, followed by my implementation:
MauiBarcodeScannerPage.xamlMauiBarcodeScannerPage.xaml.cs