Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Creating a Custom QR Code with an Image

$
0
0

I'm trying to create a QR Code with an Image inside it using ZXing.

It seems people have managed to do it within .Net

I've used a dependency service to create a Stream that can be loaded into forms for iOS:

public Stream QRToImageStream(string text, int width = 400, int height = 400) {

    var barcodeWriter = new BarcodeWriter {
        Format = ZXing.BarcodeFormat.QR_CODE,
        Options = new ZXing.Common.EncodingOptions {
            Width = width,
            Height = height,
            Margin = 0,
        }
    };

    // IMPORTANT: Allows Higher Error Checking Level so some of the QR code can be covered 
    barcodeWriter.Options.Hints.Add(ZXing.EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

    barcodeWriter.Renderer = new BitmapRenderer();

    var image2 = UIImage.FromFile("logo.png");
    var image1 = barcodeWriter.Write(text);

    UIGraphics.BeginImageContext(new CoreGraphics.CGSize(width, height));

    image1.Draw(new CoreGraphics.CGRect(
        0, 0, width, height));

    image2.Draw(new CoreGraphics.CGRect(
        width 3 / 8,
        height 3 / 8,
        width / 4,
        height / 4));

    var NewImage = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext();

    var stream = NewImage.AsPNG().AsStream();
    stream.Position = 0;
    return stream;
}

Seems to work for iOS however having trouble with the Android implementation.

So far I have this:

public Stream QRToImageStream(string text, int width = 400, int height = 400) {
    var barcodeWriter = new ZXing.Mobile.BarcodeWriter {
        Format = ZXing.BarcodeFormat.QR_CODE,
        Options = new ZXing.Common.EncodingOptions {
            Width = width,
            Height = height,
            Margin = 0
        },
        Renderer = new ZXing.Mobile.BitmapRenderer()
    };

    barcodeWriter.Options.Hints.Add(ZXing.EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

    var bitmap = barcodeWriter.Write(text);
    var stream = new MemoryStream();
    bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
    stream.Position = 0;
    return stream;
}

How I create the bitmap with an image overlay for android?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>