利用QRCode生成二维码

时间:2022-11-17 21:37:24

         二维码(Two-dimensional code),它是用特定的几何图形按一定规律在平面(二维方向)上分布的黑白相间的图形,是所有信息数据的一把钥匙。在现代商业活动中,可实现的应用十分广泛,UC浏览器以及微信扫一扫都有广泛应用。如今智能手机扫一扫(简称313)功能的应用使得二维码更加普遍。

      既然二维码如此的活跃,笔者就在网上查找怎么用程序生成二维码,找到一个名叫QRCode的类库。QR Code码,是由Denso公司于1994年9月研制的一种矩阵二维码符号,它具有一维条码及其它二维条码所具有的信息容量大、可靠性高、可表示汉字及图象多种文字信息、保密防伪性强等优点。

        废话少说,下面就介绍怎么利用该类库生成二维码。

         1. 打开VS2013新建一个网站,在Default.aspx添加三个控件,代码如下:

        <pre name="code" class="html"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>kobe</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Image ID="Image1" runat="server" Width="153px" />

</div>
<asp:TextBox ID="TextBox1" runat="server" Width="144px"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" Width="148px" OnClick="Button1_Click1" />
</form>
</body>
</html>

2.添加QRCode引用,在image文件下添加图片文件,用于生成带图片的二维码

3.在后台代码利用QRCode生成二维码

 

</pre><pre code_snippet_id="447324" snippet_file_name="blog_20140810_3_485876" name="code" class="html"></pre><p></p><p><pre name="code" class="csharp">using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Text;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Drawing;using System.Drawing.Drawing2D;using ThoughtWorks.QRCode.Codec;using ThoughtWorks.QRCode.ExceptionHandler;using ThoughtWorks.QRCode.Geom;using ThoughtWorks.QRCode.Codec.Data;using ThoughtWorks.QRCode.Codec.Util;using System.IO;public partial class _Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {    }    protected void Button1_Click1(object sender, EventArgs e)    {        create_two(this.TextBox1.Text);    }    private void create_two(string nr)    {      <span style="color:#ff0000;">  QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;        qrCodeEncoder.QRCodeScale = 4;        qrCodeEncoder.QRCodeVersion = 8;        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;</span>        System.Drawing.Image image = qrCodeEncoder.Encode(nr);        string filename = string.Format(DateTime.Now.ToString(), "yyyymmddhhmmss");        filename = filename.Replace(" ", "");        filename = filename.Replace(":", "");        filename = filename.Replace("-", "");        filename = filename.Replace(".", "");        filename = filename.Replace("/", "");        CombinImage(image, Server.MapPath("~/image/psu.jpg")).Save(Server.MapPath("~/image/") + filename + ".jpg");        this.Image1.ImageUrl = "~/image/" + filename + ".jpg";    }    /// <summary>        /// 调用此函数后使此两种图片合并,类似相册,有个        /// 背景图,中间贴自己的目标图片        /// </summary>        /// <param name="imgBack">粘贴的源图片</param>        /// <param name="destImg">粘贴的目标图片</param>        public static System.Drawing.Image CombinImage(System.Drawing.Image imgBack, string destImg)    {        System.Drawing.Image img = System.Drawing.Image.FromFile(destImg);        //照片图片              if (img.Height != 45 || img.Width != 45)        {            img = KiResizeImage(img, 45, 45, 0);        }        Graphics g = Graphics.FromImage(imgBack);        g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);      //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);             //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框            //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);            g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);        GC.Collect();        return imgBack;    }    /// <summary>        /// Resize图片        /// </summary>        /// <param name="bmp">原始Bitmap</param>        /// <param name="newW">新的宽度</param>        /// <param name="newH">新的高度</param>        /// <param name="Mode">保留着,暂时未用</param>        /// <returns>处理以后的图片</returns>        public static System.Drawing.Image KiResizeImage(System.Drawing.Image bmp, int newW, int newH, int Mode)    {        try        {            System.Drawing.Image b = new Bitmap(newW, newH);            Graphics g = Graphics.FromImage(b);            // 插值算法的质量                g.InterpolationMode = InterpolationMode.HighQualityBicubic;            g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);            g.Dispose();            return b;        }        catch        {            return null;        }    }    public bool IsReusable    {        get        {            return false;        }    }    }
4. 效果:

利用QRCode生成二维码

用微信扫一扫能够扫出kobe,输入百度网址,微信扫一扫能够跳转到百度首页。


5.补充说明: qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;代表纠错能力,四个等级如下:

L级:约可纠错7%的数据码字

M级:约可纠错15%的数据码字

Q级:约可纠错25%的数据码字

H级:约可纠错30%的数据码字