I have to create andoid app with maui .net 9.0 and I have to use old tablet with cpu 32bit.
Tablet details: 2018, Android Version 10, API 29, Java VM ART 2.1.0, Kernel architecture armv8l, armeabi-7a/armeabi.
I have installed Camera.MAUI.ZXing to read a barcode.
In the app, I display the barcode read if the barcode reader reads the same value at least twice, even if they're not consecutive.
The problem is that often nothing is displayed, or if a barcode is displayed, it takes a long time, but I can't wait that long, so I need to read the barcode faster.
On a newer generation Android phone, reading is fast and consistent.
I would like to use these tablets even if they are quite old.
I attached camera's info.
This is the code:
using System.Diagnostics;
using ZXing;
namespace GestMagazzino;
public partial class CameraView : ContentPage
{
bool isHandlingResult = false;
string _lastValue;
int _sameCount;
Dictionary _readCounts = new();
DateTime _windowStart = DateTime.MinValue;
const int REQUIRED_MATCHES = 2;
const int WINDOW_MS = 1500;
bool _accepted;
public CameraView()
{
InitializeComponent();
barcodeReader.Options = new ZXing.Net.Maui.BarcodeReaderOptions
{
Formats = ZXing.Net.Maui.BarcodeFormat.Code128 | ZXing.Net.Maui.BarcodeFormat.QrCode | ZXing.Net.Maui.BarcodeFormat.Ean13 | ZXing.Net.Maui.BarcodeFormat.Ean8 |
ZXing.Net.Maui.BarcodeFormat.Code39,
AutoRotate = true,
Multiple = false
};
if(Global.typeCode == enTypeCode.barcode)
{
gridInquadratura.WidthRequest = 300;
gridInquadratura.HeightRequest = 150;
borderInquadratura.WidthRequest = 300;
borderInquadratura.HeightRequest = 150;
}
}
protected override void OnAppearing()
{
base.OnAppearing();
barcodeReader.IsDetecting = true;
_accepted = false;
_sameCount = 0;
_lastValue = null;
}
protected override void OnDisappearing()
{
base.OnDisappearing();
barcodeReader.IsDetecting = false;
}
private async void barcodeReader_BarcodesDetected(object sender, ZXing.Net.Maui.BarcodeDetectionEventArgs e)
{
try
{
if (_accepted)
return;
var now = DateTime.Now;
if (_windowStart == DateTime.MinValue ||
(now - _windowStart).TotalMilliseconds > WINDOW_MS)
{
_windowStart = now;
_readCounts.Clear();
}
var result = e.Results
.Where(r => !string.IsNullOrWhiteSpace(r.Value))
.OrderByDescending(r => r.Value.Length)
.FirstOrDefault();
if (result == null)
return;
var value = result.Value;
if (!_readCounts.ContainsKey(value))
_readCounts[value] = 0;
_readCounts[value]++;
Debug.WriteLine($"Letto: {value} ({_readCounts[value]})");
if (_readCounts[value] < REQUIRED_MATCHES)
return;
_accepted = true;
MainThread.BeginInvokeOnMainThread(async () =>
{
try
{
barcodeReader.IsDetecting = false;
resultLabel.Text = $"Codice: {value}";
Global.resultBarcode = value;
await Task.Delay(300);
await Navigation.PopAsync();
}
catch (Exception ex)
{
await DisplayAlert("UI ERROR", ex.Message, "OK");
}
});
}
catch (Exception ex)
{
await App.Current.MainPage.DisplayAlert("ERROR CAMERA VIEW", ex.Message, "OK");
}
}
}