当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.Net Core 生成二维码(NuGet使用QRCoder)

Asp.Net Core 生成二维码(NuGet使用QRCoder)

2019年08月17日  | 移动技术网IT编程  | 我要评论

2010年春节联欢晚会下载,2013125,折磨小美美

前言

功能:调用web api 接口

1.获取 jpeg 格式的二维码 

2.获取中间带有logo 的二维码

3. 下载 jpeg,svg 格式的二维码

需要的nuget 包:

> qrcoder(v1.3.6)

> system.drawing.common(v4.5.1)

 

正文

1. 准备项目

创建asp.net core web api 应用程序,添加上边说的两个包,并创建services 文件夹,services 文件夹中的类如下:

 

2. 功能:生成jpeg 格式 二维码,通过api 来请求

在 iqrcodeservice 中添加定义的方法,返回的类型为bitmap,引用stytem.drawing

using system.drawing;

namespace qrcode.api.services.interfaces
{
public interface iqrcodeservice
{
  bitmap getqrcode(string url, int pixel);

}
}

在qrcodeservice 中继承 iqrcodeservice接口,实现 getqrcode 方法。

using qrcode.api.services.interfaces;
using qrcoder;
using system.drawing;

namespace qrcode.api.services
{
    public class qrcodeservice : iqrcodeservice
    {
        #region  qrcode

        public bitmap getqrcode(string plaintext, int pixel)
        {
            var generator = new qrcodegenerator();
            var qrcodedata = generator.createqrcode(plaintext, qrcodegenerator.ecclevel.q);
            var qrcode = new qrcoder.qrcode(qrcodedata);

            var bitmap = qrcode.getgraphic(pixel);

            return bitmap;
        }
        #endregion
    }
}    

上图: plaintext 参数指的是 扫描二维码时显示的文本内容,pixel 参数指的是 像素

ecclevel.q 参数是 指:纠错程度,(the error correction level. either l (7%), m (15%), q (25%) or h (30%). tells how much of the qr code can get corrupted before the code isn't readable any longer.)

在startup 中的configuraservices 注入依赖

public void configureservices(iservicecollection services)
        {
            services.addtransient<iqrcodeservice, qrcodeservice>();

            services.addmvc().setcompatibilityversion(compatibilityversion.version_2_2);
        }

在controller 类中注入qrcodeservice 依赖,使用get 的请求方式,请求参数为plaintext, pixel

     private readonly iqrcodeservice _qrcode;

     public valuescontroller(iqrcodeservice qrcode) { _qrcode = qrcode; } [httpget("qrcode")] public iactionresult get(string plaintext, int pixel) { if (string.isnullorempty(plaintext)) { return badrequest("parameter is null"); } if (pixel <= 0) { return badrequest("pixel <= 0"); } var bitmap = _qrcode.getqrcode(plaintext, pixel); var ms = new memorystream(); bitmap.save(ms, imageformat.jpeg); return file(ms.getbuffer(), "image/jpeg"); }

现在 运行代码 请求url:https://localhost:44313/api/values/qrcode?plaintext=there%20is%20qrcode&pixel=10

使用微信扫一扫的结果:显示的效果就是纯文字,如果plaintext =https://www.········是一个网址,会自动打开这个网址

 

 

如下图,从元数据中可以看出createqrcode方法 有多个重载,而实现payload参数有多个载体,比如说bookmark,url,phonenumber,sms,wifi 等等 还有更多载体

 

如下图:现在来使用wifi 的载体应用一下,看一下效果

在controller 类中添加getwifiqrcode() , 我们还是调用qrcodeservice类中的getqrcode方法,因为上图中wifi类重写了tostring方法,我们直接使用tostring() 转换成plaintext 这个参数

 

        [httpget("wifi")]
         public iactionresult getwifiqrcode(int pixel)
        {
            if (pixel <= 0)
            {
                return badrequest("pixel <= 0");
            }

            var payload = new wifi("ssid","password",wifi.authentication.wpa);
            var bitmap = _qrcode.getqrcode(payload.tostring(), pixel); // 还是调用qrcodeservice 中的getqrcode方法,把 payload 载体换成string类型。
            var ms = new memorystream();
            bitmap.save(ms, imageformat.jpeg);

            return file(ms.getbuffer(), "image/jpeg");
        }    

直接运行代码,二维码就不贴出来了,直接看扫描出来的截图:很明显,还真是tostring(), emmmm

 

 

3 功能:在二维码中间加入头像(logo/image)

跟上边步骤差不多,我直接贴代码,,,

在iqrcodeservice 接口类中添加getqrcodewithlogo方法定义,如下代码

bitmap getqrcodewithlogo(string plaintext, int pixel, string logopath);

在qrcodeservice类中实现这个方法,这里多了一个logopath参数,指的是添加的这个头像的路径

        public bitmap getqrcodewithlogo(string plaintext, int pixel, string logopath)
        {
            var generator = new qrcodegenerator();
            var qrcodedata = generator.createqrcode(plaintext, qrcodegenerator.ecclevel.q);
            var qrcode = new qrcoder.qrcode(qrcodedata);

            var bitmap = qrcode.getgraphic(pixel, color.black, color.white, (bitmap)image.fromfile(logopath), 15, 8);

            return bitmap;
        }    

上图中getgraphic方法中有许多的参数, 

pixel 指的是像素,(color.black, color.white 这两个参数看上边二维码图片就能知道 两个参数代表哪个区域的颜色),下一个参数就是logo 图片 格式是bitmap类型,后边两个参数分别指的是logo占二维码的百分比,范围是1-99,默认15,最后一个参数是 logo 边框宽度,整数类型,默认为6

当然这个getgraphic方法还有很多重载,可以f12看元定义,也可以在这里查看更多重载定义

 

接下来在controller 类中添加get请求,内容跟之前大致一样,我使用的图片是直接读取的物理路径。

         [httpget("logo")]
        public iactionresult getqrcodewithlogo(string plaintext, int pixel)
        {

            if (string.isnullorempty(plaintext))
            {
                return badrequest("parameter is null");
            }
            if (pixel <= 0)
            {
                return badrequest("pixel <= 0");
            }

            var logopath = @"e:\efcore\qrcode.api\qrcode.api\0000_2.jpg";
            var bitmap = _qrcode.getqrcodewithlogo(plaintext, pixel, logopath);
            var ms = new memorystream();
            bitmap.save(ms, imageformat.jpeg);

            return file(ms.getbuffer(), "image/jpeg");
        }    

运行代码,请求url:https://localhost:44313/api/values/logo?plaintext=%20there%20is%20qrcode&pixel=20

 

qrcoder 还提供了很多不同用途的接口,可以生成不同用途的二维码,比如说svg格式,为postscript打印机使用,打印出pdf等等,了解更多用途

 

4 功能:生成svg格式的矢量二维码,并下载下来

代码跟上边步骤相同,在iqrcodeservice接口类中定义方法,在qrcodeservice中实现getsvgqrcode方法,参数相同,不同的是用的svgqrcode实例,返回的是string类型。

        public string getsvgqrcode(string plaintext, int pixel)
        {
            var generator = new qrcodegenerator();
            var qrcodedata = generator.createqrcode(plaintext, qrcodegenerator.ecclevel.q);
            var qrcode = new svgqrcode(qrcodedata);

            return qrcode.getgraphic(pixel);
        }    

在controller 中添加get请求,相同的参数,保存svg到项目中,然后提供svg格式的下载

        [httpget("svg")]
        public iactionresult getsvgqrcode(string plaintext, int pixel)
        {
            if (string.isnullorempty(plaintext))
            {
                return badrequest("parameter is null");
            }
            if (pixel <= 0)
            {
                return badrequest("pixel <= 0");
            }

            var svgqrcode = _qrcode.getsvgqrcode(plaintext, pixel);

            var rootpath = _hostingenvironment.contentrootpath;
            var svgname = "svgqrcode.svg";
            system.io.file.writealltext($@"{rootpath}\{svgname}", svgqrcode);

            var readbyte = system.io.file.readallbytes($@"{rootpath}\{svgname}");

            return file(readbyte, "image/svg", svgname);
        }

运行代码,请求url, 可以看到浏览器已经下载下来, 通过浏览器是可以打开这个svg 格式二维码。

 

我这里就写了这两三个例子,看着也很简单,这个qrcoder包使用轻便,还有很多不同的用途的,不同格式的用法,更多还请查看他们的使用文档:https://github.com/codebude/qrcoder/wiki

我写的例子源码:https://github.com/ninetwoeight/qrcode.api

转载请标明出处!

本随笔链接:https://www.cnblogs.com/onemanstep/p/11365701.html

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网