Tablet-Details: 2018, Android Version 10, API 29, Java VM ART 2.1.0, Kernel-Architektur armv8l, armeabi-7a/armeabi.
Ich habe installiert Camera.MAUI.ZXing, um einen Barcode zu lesen.
In der App zeige ich den gelesenen Barcode an, wenn der Barcodeleser mindestens zweimal denselben Wert liest, auch wenn diese nicht aufeinanderfolgend sind.
Das Problem ist, dass oft nichts angezeigt wird, oder wenn ein Barcode angezeigt wird, dauert es lange, aber ich kann nicht so lange warten, also muss ich den Barcode schneller lesen.
Auf einem Android-Handy der neueren Generation, das Lesen ist schnell und konsistent.
Ich würde diese Tablets gerne verwenden, auch wenn sie ziemlich alt sind.
Ich habe die Informationen zur Kamera angehängt.
Dies ist der Code:
Code: Select all
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");
}
}
}
Mobile version