配色: 字号:
如何用.NET生成二维码?
2016-10-29 | 阅:  转:  |  分享 
  
如何用.NET生成二维码?

二维码是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,国外对二维码技术的研究始于20世纪80年代末,在二维码符号表示技术研究方面已研制出多种码制,常见的有PDF417、QRCode、Code49、Code16K、CodeOne等。二维码技术已经被广泛应用于公安、外交、军事等部门对各类证件的管理,而且也将二维码应用于海关、税务等部门对各类报表和票据的管理,商业、交通运输等部门对商品及货物运输的管理、邮政部门对邮政包裹的管理、工业生产领域对工业生产线的自动化管理。



1、新建一个window应用程序,然后引入.NET二维码类库(开源的类库,可从网上下载):



2、构建UI界面,代码如下:



1namespaceWinQRCode2{3partialclassForm14{5///6///必需的设计器变量。7///8privateSystem.ComponentModel.IContainercomponents=null;910///11///清理所有正在使用的资源。12///13///如果应释放托管资源,为true;否则为false。14protectedoverridevoidDispose(booldisposing)15{16if(disposing&&(components!=null))17{18components.Dispose();19}20base.Dispose(disposing);21}2223#regionWindows窗体设计器生成的代码2425///26///设计器支持所需的方法-不要27///使用代码编辑器修改此方法的内容。28///29privatevoidInitializeComponent()30{31this.button1=newSystem.Windows.Forms.Button();32this.button2=newSystem.Windows.Forms.Button();33this.pictureBox1=newSystem.Windows.Forms.PictureBox();34this.label1=newSystem.Windows.Forms.Label();35this.textBox1=newSystem.Windows.Forms.TextBox();36this.label2=newSystem.Windows.Forms.Label();37this.lbl解码信息=newSystem.Windows.Forms.Label();38((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();39this.SuspendLayout();40//41//button142//43this.button1.Location=newSystem.Drawing.Point(446,31);44this.button1.Margin=newSystem.Windows.Forms.Padding(4,5,4,5);45this.button1.Name="button1";46this.button1.Size=newSystem.Drawing.Size(103,37);47this.button1.TabIndex=0;48this.button1.Text="编码";49this.button1.UseVisualStyleBackColor=true;50this.button1.Click+=newSystem.EventHandler(this.button1_Click);51//52//button253//54this.button2.Location=newSystem.Drawing.Point(446,81);55this.button2.Margin=newSystem.Windows.Forms.Padding(4,5,4,5);56this.button2.Name="button2";57this.button2.Size=newSystem.Drawing.Size(103,37);58this.button2.TabIndex=1;59this.button2.Text="解码";60this.button2.UseVisualStyleBackColor=true;61this.button2.Click+=newSystem.EventHandler(this.button2_Click);62//63//pictureBox164//65this.pictureBox1.Location=newSystem.Drawing.Point(23,117);66this.pictureBox1.Margin=newSystem.Windows.Forms.Padding(4,5,4,5);67this.pictureBox1.Name="pictureBox1";68this.pictureBox1.Size=newSystem.Drawing.Size(279,253);69this.pictureBox1.SizeMode=System.Windows.Forms.PictureBoxSizeMode.StretchImage;70this.pictureBox1.TabIndex=2;71this.pictureBox1.TabStop=false;72//73//label174//75this.label1.AutoSize=true;76this.label1.Location=newSystem.Drawing.Point(18,39);77this.label1.Margin=newSystem.Windows.Forms.Padding(4,0,4,0);78this.label1.Name="label1";79this.label1.Size=newSystem.Drawing.Size(69,25);80this.label1.TabIndex=3;81this.label1.Text="信息:";82//83//textBox184//85this.textBox1.Location=newSystem.Drawing.Point(95,37);86this.textBox1.Margin=newSystem.Windows.Forms.Padding(4,5,4,5);87this.textBox1.Name="textBox1";88this.textBox1.Size=newSystem.Drawing.Size(333,31);89this.textBox1.TabIndex=4;90//91//label292//93this.label2.AutoSize=true;94this.label2.Location=newSystem.Drawing.Point(18,81);95this.label2.Margin=newSystem.Windows.Forms.Padding(4,0,4,0);96this.label2.Name="label2";97this.label2.Size=newSystem.Drawing.Size(88,25);98this.label2.TabIndex=5;99this.label2.Text="二维码:";100//101//lbl解码信息102//103this.lbl解码信息.AutoSize=true;104this.lbl解码信息.Location=newSystem.Drawing.Point(101,81);105this.lbl解码信息.Margin=newSystem.Windows.Forms.Padding(4,0,4,0);106this.lbl解码信息.Name="lbl解码信息";107this.lbl解码信息.Size=newSystem.Drawing.Size(0,25);108this.lbl解码信息.TabIndex=6;109//110//Form1111//112this.AutoScaleDimensions=newSystem.Drawing.SizeF(11F,24F);113this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;114this.ClientSize=newSystem.Drawing.Size(572,384);115this.Controls.Add(this.lbl解码信息);116this.Controls.Add(this.label2);117this.Controls.Add(this.textBox1);118this.Controls.Add(this.label1);119this.Controls.Add(this.pictureBox1);120this.Controls.Add(this.button2);121this.Controls.Add(this.button1);122this.Font=newSystem.Drawing.Font("微软雅黑",10.8F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(134)));123this.Margin=newSystem.Windows.Forms.Padding(4,5,4,5);124this.Name="Form1";125this.Text="二维码";126((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();127this.ResumeLayout(false);128this.PerformLayout();129130}131132#endregion133134privateSystem.Windows.Forms.Buttonbutton1;135privateSystem.Windows.Forms.Buttonbutton2;136privateSystem.Windows.Forms.PictureBoxpictureBox1;137privateSystem.Windows.Forms.Labellabel1;138privateSystem.Windows.Forms.TextBoxtextBox1;139privateSystem.Windows.Forms.Labellabel2;140privateSystem.Windows.Forms.Labellbl解码信息;141}142}



图形如下所示:

3、编写编码和解码按钮事件,进行编码和解码处理:



1usingSystem;2usingSystem.Collections.Generic;3usingSystem.ComponentModel;4usingSystem.Data;5usingSystem.Drawing;6usingSystem.Linq;7usingSystem.Text;8usingSystem.Windows.Forms;910namespaceWinQRCode11{12usingThoughtWorks.QRCode.Codec;13usingThoughtWorks.QRCode.Codec.www.wang027.comData;14usingThoughtWorks.QRCode.Codec.Util;15usingSystem.IO;16usingPdfToImage;17publicpartialclassForm1:Form18{19publicForm1()20{21InitializeComponent();22}23///24///编码25///26///27///28privatevoidbutton1_Click(objectsender,EventArgse)29{30if(this.textBox1.Text!="")31{32Cursor.Current=Cursors.WaitCursor;33if(textBox1.Text.Trim()==String.Empty)34{35MessageBox.Show("不能为空.");36return;37}38QRCodeEncoderqrCodeEncoder=newQRCodeEncoder();39qrCodeEncoder.QRCodeEncodeMode=QRCodeEncoder.ENCODE_MODE.BYTE;40qrCodeEncoder.QRCodewww.baiyuewang.netScale=4;41qrCodeEncoder.QRCodeVersion=7;42qrCodeEncoder.QRCodeErrorCorrect=QRCodeEncoder.ERROR_CORRECTION.M;43System.Drawing.Imageimage;44Stringdata=textBox1.Text;45//编码46image=qrCodeEncoder.Encode(data);47//图片显示48this.pictureBox1.Image=image;49Cursor.Current=Cursors.Default;5051}52}53///54///解码55///56///57///58privatevoidbutton2_Click(objectsender,EventArgse)59{60Cursor.Current=Cursors.WaitCursor;61Bitmapb=this.pictureBox1.ImageasBitmap;62try63{64QRCodeDecoderdecoder=newQRCodeDecoder();65//解码66StringdecodedString=decoder.decode(newQRCodeBitmapImage(b));67//显示解码信息68this.lbl解码信息.Text+=decodedString;69}70catch(Exceptionex)71{72MessageBox.Show(ex.Message);73}74Cursor.Current=Cursors.Default;75}7677privatevoidForm1_Load(objectsender,EventArgse)78{7980}81}82}



4、编译运行,如下图:

?



献花(0)
+1
(本文系thedust79首藏)