WriteableBitmap'& # 39; LuminanceSource& # 39;在UWP上进行条码解

本文关键字:条码 LuminanceSource WriteableBitmap UWP | 更新日期: 2023-09-27 18:12:15

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

然而它对我说

无法转换类型"Windows.UI.Xaml.Media.Imaging"。WriteableBitmap"的zx。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码的数据。有解决方案吗?

WriteableBitmap'& # 39; LuminanceSource& # 39;在UWP上进行条码解

首先,我同意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);