溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

[ASP.NET]二維碼的創(chuàng)建

發(fā)布時(shí)間:2020-06-17 09:10:02 來(lái)源:網(wǎng)絡(luò) 閱讀:959 作者:蓬萊仙羽 欄目:編程語(yǔ)言

又好一段時(shí)間沒(méi)有寫(xiě)寫(xiě)東西了,繼續(xù)回歸原來(lái)的模式,多做記錄,最近要實(shí)現(xiàn)個(gè)unity的二維碼方面的功能,首先就要解決生成二維碼的問(wèn)題,這個(gè)倒是有這方面的組件,然后我通過(guò)強(qiáng)大的反編譯工具Reflector(想必.NET程序都知道的神器),來(lái)插件內(nèi)部實(shí)現(xiàn)的原理。廢話不多說(shuō),先看效果二維碼在線生成工具,附帶一句這里是QR碼,當(dāng)然是比較簡(jiǎn)單的,純屬新手入門(mén)級(jí)看的

在線測(cè)試:http://114.92.225.191:5006/

更多精彩教程請(qǐng)關(guān)注我的微博

效果圖

[ASP.NET]二維碼的創(chuàng)建

[ASP.NET]二維碼的創(chuàng)建

了解QR碼

[ASP.NET]二維碼的創(chuàng)建


項(xiàng)目解析

1.用反編譯神器來(lái)查看組件的內(nèi)部實(shí)現(xiàn)原理

a)生成圖片方法

主要的就是這個(gè)方法是用來(lái)生成一張二維碼圖片的,其中this.calQrcode(encoding.GetBytes(content))這個(gè)方法是生成二維碼對(duì)應(yīng)圖片的一個(gè)bool數(shù)組,然后就是通過(guò)這個(gè)0,1數(shù)組來(lái)繪制成二維碼

[ASP.NET]二維碼的創(chuàng)建


[csharp]view plaincopyprint?[ASP.NET]二維碼的創(chuàng)建[ASP.NET]二維碼的創(chuàng)建
  1. publicvirtual Bitmap Encode(string content, Encoding encoding)  

  2. {  

  3. //計(jì)算得到bool值數(shù)組

  4. bool[][] flagArray = this.calQrcode(encoding.GetBytes(content));  

  5. //定義畫(huà)刷

  6.    SolidBrush brush = new SolidBrush(this.qrCodeBackgroundColor);  

  7.    Bitmap p_w_picpath = new Bitmap((flagArray.Length * this.qrCodeScale) + 1, (flagArray.Length * this.qrCodeScale) + 1);  

  8. //繪圖

  9.    Graphics graphics = Graphics.FromImage(p_w_picpath);  

  10. //繪制二維碼背景

  11.    graphics.FillRectangle(brush, new Rectangle(0, 0, p_w_picpath.Width, p_w_picpath.Height));  

  12. //設(shè)置背景顏色

  13.    brush.Color = this.qrCodeForegroundColor;  

  14. //根據(jù)bool數(shù)組繪制前端二維碼

  15. for (int i = 0; i < flagArray.Length; i++)  

  16.    {  

  17. for (int j = 0; j < flagArray.Length; j++)  

  18.        {  

  19. if (flagArray[j][i])  

  20.            {  

  21.                graphics.FillRectangle(brush, j * this.qrCodeScale, i * this.qrCodeScale, this.qrCodeScale, this.qrCodeScale);  

  22.            }  

  23.        }  

  24.    }  

  25. return p_w_picpath;  

  26. }  



b)生成bool數(shù)組方法以及原理


[ASP.NET]二維碼的創(chuàng)建

這里主要是用到


創(chuàng)建.NET應(yīng)用程序

Default.aspx


[html]view plaincopyprint?[ASP.NET]二維碼的創(chuàng)建[ASP.NET]二維碼的創(chuàng)建
  1. <%@ Page Language="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default" %>

  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  3. <htmlxmlns="http://www.w3.org/1999/xhtml">

  4. <headrunat="server">

  5. <title>無(wú)標(biāo)題頁(yè)</title>

  6. </head>

  7. <body>

  8. <formid="form1"runat="server">

  9. <div>

  10.        請(qǐng)輸入內(nèi)容:<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>

  11. <br/>

  12.        生成的文件夾名稱:<asp:TextBoxID="TextBox2"runat="server"></asp:TextBox>

  13. <br/>

  14.        生成的二維碼名稱:<asp:TextBoxID="TextBox3"runat="server"></asp:TextBox>

  15. <br/>

  16. <asp:LabelID="Label1"runat="server"Text=""></asp:Label>

  17. <br/>

  18. <asp:ButtonID="Button1"runat="server"Text="生成二維碼"onclick="Button1_Click"/>

  19. </div>

  20. <div>

  21. <ahref="http://blog.csdn.net/dingxiaowei2013">項(xiàng)目解析</a>

  22. </div>

  23. </form>

  24. </body>

  25. </html>


Default.aspx.cs



[csharp]view plaincopyprint?[ASP.NET]二維碼的創(chuàng)建[ASP.NET]二維碼的創(chuàng)建
  1. using System;  

  2. using ThoughtWorks.QRCode.Codec;  

  3. using System.IO;  

  4. using System.Text;  

  5. using System.Drawing;  

  6. public partial class _Default : System.Web.UI.Page  

  7. {  

  8. protectedvoid Page_Load(object sender, EventArgs e)  

  9.    {  

  10.    }  

  11. protectedvoid Button1_Click(object sender, EventArgs e)  

  12.    {  

  13.        QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();  

  14. //設(shè)置背景顏色

  15. //qrCodeEncoder.QRCodeBackgroundColor = Color.FromArgb(255, 255, 0);

  16. //設(shè)置前景色

  17. //qrCodeEncoder.QRCodeForegroundColor = Color.GreenYellow;

  18. //編碼格式

  19.        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;  

  20. //設(shè)置每個(gè)二維碼像素點(diǎn)的大小

  21.        qrCodeEncoder.QRCodeScale = 4;  

  22. //QR碼版本

  23. //QR碼所允許規(guī)格系列為21×21模塊(版本1)~177×177模塊(版本40)

  24.        qrCodeEncoder.QRCodeVersion = 8;  

  25. //糾錯(cuò)等級(jí)

  26. //level L : 最大 7% 的錯(cuò)誤能夠被糾正;

  27. //level M : 最大 15% 的錯(cuò)誤能夠被糾正;  

  28. //level Q : 最大 25% 的錯(cuò)誤能夠被糾正;  

  29. //level H : 最大 30% 的錯(cuò)誤能夠被糾正;

  30.        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;  

  31. //自定義的二維碼數(shù)據(jù)

  32.        String data = TextBox1.Text.ToString();  

  33. //Response.Write(data);

  34. //畫(huà)圖

  35.        System.Drawing.Bitmap p_w_picpath = qrCodeEncoder.Encode(data);  

  36.        System.IO.MemoryStream MStream = new System.IO.MemoryStream();  

  37.        p_w_picpath.Save(MStream, System.Drawing.Imaging.ImageFormat.Png);  

  38.        Response.ClearContent();  

  39.        Response.ContentType = "p_w_picpath/Png";  

  40. //寫(xiě)圖片

  41.        Response.BinaryWrite(MStream.ToArray());  

  42. //文件路徑

  43. //FileInfo f = new FileInfo(@"d:\zl.png");

  44. //Directory.CreateDirectory("d:\\"+TextBox2.Text.Trim());

  45. //當(dāng)前項(xiàng)目路徑

  46.        Directory.CreateDirectory(Server.MapPath("~/")+"p_w_picpaths"+"http://"+TextBox2.Text.Trim());  

  47. if (!File.Exists(Server.MapPath("~/") + "p_w_picpaths" + "http://" + TextBox2.Text.Trim() + "http://" + TextBox3.Text.Trim() + ".png"))  

  48.        {  

  49. //FileStream fs = new FileStream("d:\\" + TextBox2.Text.Trim() + "\\" + TextBox3.Text.Trim() + ".png", FileMode.CreateNew, FileAccess.ReadWrite);

  50.            FileStream fs = new FileStream(Server.MapPath("~/") + "p_w_picpaths" + "http://" + TextBox2.Text.Trim() + "http://" + TextBox3.Text.Trim() + ".png", FileMode.CreateNew, FileAccess.ReadWrite);  

  51.            BinaryWriter bw = new BinaryWriter(fs, UTF8Encoding.UTF8);  

  52. byte[] by = MStream.ToArray();  

  53. for (int i = 0; i < MStream.ToArray().Length; i++)  

  54.                bw.Write(by[i]);  

  55.            fs.Close();  

  56.        }  

  57. else

  58.        {  

  59.            Label1.Text = "該圖片已經(jīng)存在";  

  60.        }  

  61.    }  

  62. }

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI