HomeiOS Developmentios - .NET MAUI – Drive rear digital camera by default (MediaPicker...

ios – .NET MAUI – Drive rear digital camera by default (MediaPicker / Digicam.MAUI / ZXing / OCR)


I am constructing a cross-platform .NET MAUI app that makes use of the digital camera to take a photograph and run OCR (by way of Amazon Rekognition or Plugin.Maui.OCR). Whatever the platform (Android or iOS), each API I’ve tried all the time opens the entrance/selfie digital camera, regardless that I need the rear digital camera by default.

I’ve tried the next:

  • MediaPicker.Default.CapturePhotoAsync()
  • Digicam.MAUI with Digicam = "Rear"
  • ZXing.Internet.MAUI with CameraLocation.Rear
  • Plugin.Maui.OCR seize integration

On all of them, the entrance digital camera opens by default, and there’s no apparent setting or override to pick out the rear digital camera. Even setting digital camera preferences (e.g., CameraDevice = Rear) has no impact—particularly on Android.

This simplified instance makes use of MediaPicker + Plugin.Maui.OCR, however the situation is identical with the others:

utilizing System;
utilizing System.IO;
utilizing Microsoft.Maui.Controls;
utilizing Microsoft.Maui.Storage;    // MediaPicker
utilizing Plugin.Maui.OCR;           // OcrPlugin

namespace RearCameraOCR;

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    personal async void OnCaptureClicked(object sender, EventArgs e)
    {
        strive
        {
            if (!MediaPicker.Default.IsCaptureSupported)
            {
                await DisplayAlert("Error", "Digicam not supported.", "OK");
                return;
            }

            // At all times opens the entrance digital camera as a substitute of rear
            var photoFile = await MediaPicker.Default.CapturePhotoAsync();
            if (photoFile == null)
                return;

            utilizing var stream = await photoFile.OpenReadAsync();
            utilizing var ms = new MemoryStream();
            await stream.CopyToAsync(ms);
            var bytes = ms.ToArray();
            photoImage.Supply = ImageSource.FromStream(() => new MemoryStream(bytes));

            var ocrResult = await OcrPlugin.Default.RecognizeTextAsync(bytes);
            var textual content = ocrResult.AllText;

            resultLabel.Textual content = string.IsNullOrWhiteSpace(textual content)
                ? "No textual content detected."
                : textual content;
        }
        catch (Exception ex)
        {
            await DisplayAlert("Error", ex.Message, "OK");
        }
    }
}



    

        

Query:
How can I pressure .NET MAUI (on Android and iOS) to all the time use the rear digital camera by default when capturing pictures? Is that this limitation inherent to MediaPicker (i.e., it all the time makes use of the system UI), or is there a workaround/API that enables choosing the rear digital camera?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments