I'm using Camera.MAUI.ZXing and Camera.MAUI in my .Net MAUI project. On my xaml.cs I have the following code:
private async void InitializeCamera()
{
// barcode detection
cameraView.BarcodeDetected += Camera_OnDetected;
// Inicializar ZXing Barcode Decoder
cameraView.BarCodeDecoder = new ZXingBarcodeDecoder();
// Configurar las opciones del decodificador de códigos de barras
cameraView.BarCodeOptions = new BarcodeDecodeOptions
{
AutoRotate = true,
PossibleFormats = { BarcodeFormat.QR_CODE },
ReadMultipleCodes = false,
TryHarder = true,
TryInverted = true
};
// Configurar otras opciones de detección
cameraView.BarCodeDetectionFrameRate = 10;
cameraView.BarCodeDetectionMaxThreads = 5;
cameraView.ControlBarcodeResultDuplicate = true;
cameraView.BarCodeDetectionEnabled = true;
// Iniciar la cámara de forma asíncrona
if (cameraView.NumCamerasDetected > 0)
{
//if (cameraView.NumMicrophonesDetected > 0)
// cameraView.Microphone = cameraView.Microphones.First();
cameraView.Camera = cameraView.Cameras.FirstOrDefault();
MainThread.BeginInvokeOnMainThread(async () =>
{
if (await cameraView.StartCameraAsync() == CameraResult.Success)
{
playing = true;
}
});
}
else
await DisplayAlert("Warning", "No cameras detected.", "OK");
}
protected override async void OnAppearing()
{
base.OnAppearing();
// Verificar el estado del permiso de la cámara
var cameraStatus = await Permissions.CheckStatusAsync<Permissions.Camera>();
if (cameraStatus != PermissionStatus.Granted)
{
var results = await Permissions.RequestAsync<Permissions.Camera>();
if (results != PermissionStatus.Granted)
{
await Application.Current.MainPage.DisplayAlert("Permisos de cámara", "Los permisos de la cámara han sido denegados, active manualmente los permisos para usar esta funcionalidad", "OK");
return;
}
}
// Inicializar la cámara después de obtener los permisos
InitializeCamera();
}
private async void Camera_OnDetected(object sender, Camera.MAUI.ZXingHelper.BarcodeEventArgs e)
{
string result = e.Result[0].Text;
if (result is null) return;
if (viewModel.ValidaCodigo(result))
{
// Navegar a la página de inicio
Shell.Current.GoToAsync("//HomePage");
//await StopCameraAsync();
}
}
The problem I have is the InitializeCamera, when I make the condition to detect the cameras (NumCamerasDetected) the first time I enter the view and accept the permissions, it tells me that my device has 4 cameras and that is fine, but when I enter for the second time it appears that I have 0.
It's as if the app will stop detecting them.
Is there any way to refresh or not lose the 4 cameras?
I can reproduce your problem. The cameraView.NumCamerasDetected
will always be 0 when you enter it second. So you can use the cameraView.CamerasLoaded
to get the camera.
cameraView.BarcodeDetected += Camera_OnDetected;
// Inicializar ZXing Barcode Decoder
cameraView.BarCodeDecoder = new ZXingBarcodeDecoder();
cameraView.CamerasLoaded += (s, e) =>
{
if (cameraView.Cameras.Count > 0)
{
//if (cameraView.NumMicrophonesDetected > 0)
// the cameraView.NumMicrophonesDetected is still 0 here
cameraView.Camera = cameraView.Cameras.FirstOrDefault();
MainThread.BeginInvokeOnMainThread(async () =>
{
if (await cameraView.StartCameraAsync() == CameraResult.Success)
{
// playing = true;
}
});
}
};