如何读取 Xamarin 表单中的条码图像

How to Read barcode image in Xamarin forms

我正在尝试从我的移动应用程序上的二维码图像中读取文本。我正在使用 Xamarin.Forms 和 ZXing NuGet 包。

我已经能够使用 Xamarin.Essentials FilePicker 获取文件。但我不知道如何实际读取条形码。我看过一些 Whosebug 解决方案,它们似乎都是基于 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;
}

https://github.com/Redth/ZXing.Net.Mobile/issues/981。这个线程为我解决了它。此回复归功于@jason。