danny
danny

Reputation: 21

.NET MAUI - How to get QR code from image?

I'm having trouble decoding a QR code from an image selected from the gallery. Here's what I've tried so far:

public async Task<string?> GetQRCode()
{
    var fileResult = await MediaPicker.Default.PickPhotoAsync();
    if (fileResult != null && (fileResult.ContentType == "image/png" || fileResult.ContentType == "image/jpeg"))
    {
        byte[] bytes;
        using (var stream = await fileResult.OpenReadAsync())
        {
            using (MemoryStream ms = new MemoryStream())
            {
                stream.CopyTo(ms);
                bytes = ms.ToArray();
            }
        }

        var reader = new ZXing.BarcodeReaderGeneric()
        {
            Options = new ZXing.Common.DecodingOptions()
            {
                PossibleFormats = new List<ZXing.BarcodeFormat> { ZXing.BarcodeFormat.QR_CODE },
                TryHarder = true,
                PureBarcode = false,
                TryInverted = true
            },
            AutoRotate = true
        };

        // how to get imageWidth and imageHeight?
        var source = new RGBLuminanceSource(bytes, imageWidth, imageHeight);
        var result = reader.Decode(source);

        return result?.Text;
    }
    return null;
}

I’m unsure about how to get the image size to pass to RGBLuminanceSource, and I’m not certain if this is the correct approach. Any help would be greatly appreciated.

Upvotes: 0

Views: 188

Answers (0)

Related Questions