【发布时间】:2022-01-11 20:44:29
【问题描述】:
我正在尝试从我的移动应用程序上的二维码图像中读取文本。我正在使用带有 ZXing NuGet 包的 Xamarin.Forms。
我已经能够使用Xamarin.Essentials FilePicker 获取文件。但我不知道如何实际阅读条形码。我查看了一些 stackoverflow 解决方案,它们似乎都是基于 Xamarin.Android 的(使用 BinaryBitmap 对象)。我需要一个同样适用于 iOS 和 UWP 的解决方案。这是我目前所拥有的:
string file = "";
var filePickerOptions = new PickOptions
{
PickerTitle = "Select Barcode Image",
FileTypes = FilePickerFileType.Images
};
var result = await FilePicker.PickAsync(filePickerOptions);
if (result != null)
{
file = result.FullPath;
var res = Decode(file, BarcodeFormat.QR_CODE);
Console.WriteLine(res.Text);
}
public Result Decode(string file, BarcodeFormat? format = null, KeyValuePair<DecodeHintType, object>[] aditionalHints = null)
{
var r = GetReader(format, aditionalHints);
/* I need some function here that will allow me to get the BinaryBitmap from the image file path or something along those lines.*/
var image = GetBinaryBitmap(file);
var result = r.decode(image);
return result;
}
MultiFormatReader GetReader(BarcodeFormat? format, KeyValuePair<DecodeHintType, object>[] aditionalHints)
{
var reader = new MultiFormatReader();
var hints = new Dictionary<DecodeHintType, object>();
if (format.HasValue)
{
hints.Add(DecodeHintType.POSSIBLE_FORMATS, new List<BarcodeFormat>() { format.Value });
}
if (aditionalHints != null)
{
foreach (var ah in aditionalHints)
{
hints.Add(ah.Key, ah.Value);
}
}
reader.Hints = hints;
return reader;
}
【问题讨论】:
-
这个链接很有帮助!谢谢,我找到了如何为 UWP 做到这一点,并添加到 Github 上的讨论中。请将此作为答案发布,以便我接受。
标签: xamarin.forms barcode zxing