Asp.Net Core在线生成二维码

前言:

原先用zxing Code写过基于Winfrom的批量生成二维码工具,以及单个生成二维码工具;批量生成二维码Gihub源代码

今天尝试用QRCoder 加 Asp.Net Core 写了一个在线生成二维码的例子,并且保存图片到Ubuntu系统;

代码:

生成二维码所需要用到的包:QRCoder

根据Github上的源代码,引用NuGet包

PM> Install-Package QRCoder 

 

根据传入的参数,生成二维码,并且保存图片

        public static void QRCode(string str)
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(OpenId, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);
            
            Bitmap qrCodeImage = qrCode.GetGraphic(20);
            
            qrCodeImage.Save(@"/root/public/images/" + str + ".png", ImageFormat.Png);
            Logger.Info("二维码生成路径:" + @"/root/public/images/" + str + ".png");
            qrCodeImage.Dispose();
        }

 

注意事项:

部署到Ubuntu服务器上时,出现错误:

 The type initializer for 'Gdip' threw an exception.

 这是因为Ubuntu系统中未安装gdi库造成的,需要安装 libgdiplus

 apt-get install libgdiplus 

 

最后成功生成二维码

 

参考资料:

  • https://q.cnblogs.com/q/103863/
  • https://www.cnblogs.com/Robbery/p/10115234.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章