Search code examples
c#uwpzxing

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


I'm trying to use ZXing on UWP project and I have read a lots of tutorials, that I can't get to work. The last tutorial said that I should use WritableBitmap, 'cos Bitmap is not awailable in UWP.

However it says to me

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
    }
}

How could I get this work? I have an image from MediaCapture and it would be fine to use that and get the QR code's data. Any solution?


Solution

  • First of all, I agree with Peter Duniho.

    Then, if you want to use reader.Decode() method, the parameter is actually SoftwareBitmapLuminanceSource.

    You can convert the WriteableBitmap to SoftwareBitmap at first and then convert it to SoftwareBitmapLuminanceSource. And in your code IBarcodeReader reader = new BarcodeReader();, is this a typo? IBarcodeReader is an interface.

    Any way, you can code for example like this:

    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);