简体   繁体   中英

How can I generate QR code in UWP application?

I have tried to install QrCode.net nugget package for an UWP application, but it writes the error for me:

using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

are not not exists.

Does anyone know any nugget package for UWP application that helps me to create and show a QR code that generated from a string? (UWP/C#)

Does anyone know any nugget package for UWP application that helps me to create and show a QR code that generated from a string? (UWP/C#)

Since you are developing an UWP app, you can use the Zxing.Net.Mobile package. After installed this package, to generate a barcode, you can refer to the following example:

<Image x:Name="qrcodeImg" Stretch="None" />

code behind:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var write = new BarcodeWriter();
    write.Format = ZXing.BarcodeFormat.QR_CODE;
    var wb = write.Write("BarCode Content");
    this.qrcodeImg.Source = wb;
}

This is how you do using the MVVM pattern ( based on Grace Feng's approach )

XAML

<Image Source="{Binding QRImage, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Viewmodel

// Property
public WriteableBitmap QRImage { get; set; }

// Function to call
private void SetQR()
{
    var options = new QrCodeEncodingOptions()
    {
        DisableECI = true,
        CharacterSet = "UTF-8",
        Width = 1000,
        Height = 1000
    };

    BarcodeWriter writer = new BarcodeWriter();
    writer.Format = BarcodeFormat.QR_CODE;
    writer.Options = options;
    QRImage= writer.Write(SelectedEvent.GetQrString());
}

2020 Answer

Another option is to use QRCoder. Some example setup from the github .

QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM