自学内容网 自学内容网

Avalonia中使用Zxing.Net生成二维码,一维码

Nuget引两个包

在这里插入图片描述

  <Image Name="image"/>
Avalonia.Media.Imaging.Bitmap bitmap = null;
if (item.Barcode == BarcodeType.QR_Code)
{
    var stream = CreateQRCode(item.Text, (int)width, (int)height);
    bitmap = new Avalonia.Media.Imaging.Bitmap(stream);
    image.Source = bitmap;
}
    public static Stream CreateQRCode(string sContent, int width, int height,string encoding = "UTF-8")
    {
        var renderer = new SKBitmapRenderer();
        renderer.Background = SKColors.Transparent;
        renderer.Foreground = SKColors.Black;
        
        BarcodeWriter<SKBitmap> writer = new BarcodeWriter<SKBitmap>
        { 
            Renderer = renderer,
            Format = BarcodeFormat.QR_CODE,
            Options = new QrCodeEncodingOptions
            {
                CharacterSet = encoding,
                Margin = 1,
                DisableECI = true,
                Height = height,
                Width = width,
                ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.L,
            }
        };
        var skBitmap = writer.Write(sContent);
        Stream stream = SKImage.FromBitmap(skBitmap).Encode(SKEncodedImageFormat.Png, 100).AsStream();
        return stream;
    }


原文地址:https://blog.csdn.net/qq_40127027/article/details/143482904

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!