无法在 UWP 上将类型 'WriteableBitmap' 转换为 'LuminanceSource' 以进行条形码解码

Cannot convert type 'WriteableBitmap' to 'LuminanceSource' on UWP for barcode decoding

我正在尝试在 UWP 项目上使用 ZXing,我已经阅读了很多教程,但我无法开始工作。上一个教程说我应该使用 WritableBitmap,因为 Bitmap 在 UWP 中不可用。

然而它对我说

Cannot convert type 'Windows.UI.Xaml.Media.Imaging.WriteableBitmap' to 'ZXing.LuminanceSource'

public class QrCodeHelpers
{
    public static void ReadQrCodeFromBitmap(WriteableBitmap image)
    {
        IBarcodeReader reader = new BarcodeReader();
        var generic = new BarcodeReaderGeneric<WriteableBitmap>();

        // detect and decode the barcode inside the bitmap
        var result = reader.Decode((ZXing.LuminanceSource)image);
        // do something with the result
    }
}

我怎样才能得到这份工作?我有一张来自 MediaCapture 的图像,可以使用它并获取 QR 码的数据。有什么解决办法吗?

首先,我同意 Peter Duniho 的观点。

那么,如果要使用reader.Decode()方法,参数其实就是SoftwareBitmapLuminanceSource.

可以先把WriteableBitmap转成SoftwareBitmap,再转成SoftwareBitmapLuminanceSource。在您的代码 IBarcodeReader reader = new BarcodeReader(); 中,这是一个错字吗? IBarcodeReader 是一个接口。

无论如何,你可以像这样编写代码:

SoftwareBitmap sbmp = SoftwareBitmap.CreateCopyFromBuffer(wbmp.PixelBuffer,
    BitmapPixelFormat.Bgra8,
    wbmp.PixelWidth,
    wbmp.PixelHeight); //converter WriteableBitmap to SoftwareBitmap, wbmp represents the WriteableBitmap


//convert SoftwareBitmap to SoftwareBitmapLuminanceSource
SoftwareBitmapLuminanceSource luminanceSource = new SoftwareBitmapLuminanceSource(sbmp);

BarcodeReader reader = new BarcodeReader(); //change IBarcodeReader to BarcodeReader
var generic = new BarcodeReaderGeneric<WriteableBitmap>(); //This code for what?
var result = reader.Decode(luminanceSource);