ASP.NET生成二维码的方法总结
本文实例总结了ASP.NET生成二维码的方法。分享给大家供大家参考,具体如下:
分享一例c#生成二维码的代码,直接引用ThoughtWorks.QRCode.dll类生成二维码,有需要的朋友参考下。
方法1.直接引用ThoughtWorks.QRCode.dll类,生成二维码。
代码示例:
ThoughtWorks.QRCode.Codec.QRCodeEncoderencoder=newQRCodeEncoder(); encoder.QRCodeEncodeMode=QRCodeEncoder.ENCODE_MODE.BYTE;//编码方法(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字) encoder.QRCodeScale=4;//大小 encoder.QRCodeVersion=0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误) encoder.QRCodeErrorCorrect=QRCodeEncoder.ERROR_CORRECTION.M; Stringqrdata="二维码信息"; System.Drawing.Bitmapbp=encoder.Encode(qrdata.ToString(),Encoding.GetEncoding("GB2312")); Imageimage=bp; ObjectoMissing=System.Reflection.Missing.Value; pictureBox1.Image=bp;
保存二维码图片:
代码示例:
SaveFileDialogsf=newSaveFileDialog(); sf.Title="选择保存文件位置"; sf.Filter="保存图片(*.jpg)|*.jpg|所有文件(*.*)|*.*"; //设置默认文件类型显示顺序 sf.FilterIndex=1; //保存对话框是否记忆上次打开的目录 sf.RestoreDirectory=true; if(sf.ShowDialog()==DialogResult.OK) { Imageim=this.pictureBox1.Image; //获得文件路径 stringlocalFilePath=sf.FileName.ToString(); if(sf.FileName!="") { stringfileNameExt=localFilePath.Substring(localFilePath.LastIndexOf("\\")+1);//获取文件名,不带路径 //newFileName=fileNameExt+DateTime.Now.ToString("yyyyMMdd");//给文件名后加上时间 stringFilePath=localFilePath.Substring(0,localFilePath.LastIndexOf("."));//获取文件路径,带文件名,不带后缀 stringfn=sf.FileName; pictureBox1.Image.Save(FilePath+"-"+DateTime.Now.ToString("yyyyMMdd")+".jpg"); } } //解析二维码信息 //QRCodeDecoderdecoder=newQRCodeDecoder(); //StringdecodedString=decoder.decode(newQRCodeBitmapImage(newBitmap(pictureBox1.Image))); //this.label3.Text=decodedString;
方法2.引用ZXing类库。
ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码。目标是能够对QR编码、DataMatrix、UPC的1D条形码进行解码。于此同时,它同样提供cpp,ActionScript,android,iPhone,rim,j2me,j2se,jruby,C#等方式的类库。zxing类库的作用主要是解码,是目前开源类库中解码能力比较强的(商业的另说,不过对于动辄成千上万的类库授权费用,的确很值)。
到谷歌code下载相应的代码
1.下载zxing最新的包
到zxing的主页:http://code.google.com/p/zxing/
找到其中的CSharp文件夹,在vs中打开并编译,将obj下debug中的zxing.dll复制并粘帖到你的项目中的bin文件目录下,
右击添加项目引用。将zxing.dll引用到项目中,就可以在需要的地方使用了。
源代码中有两处UTF-8的问题,会导致中文出现乱码(编译.dll之前修改)
其一:com.google.zxing.qrcode.encoder.encoder类中的
internalconstSystem.StringDEFAULT_BYTE_MODE_ENCODING="ISO-8859-1";
此处,将ISO-8859-1改为UTF-8
其二:com.google.zxing.qrcode.decoder.DecodedBitStreamParser类的成员
privateconstSystem.StringUTF8="UTF8";
应将UTF8改为UTF-8
代码示例:
usingcom.google.zxing.qrcode; usingcom.google.zxing; usingcom.google.zxing.common; usingByteMatrix=com.google.zxing.common.ByteMatrix; usingEAN13Writer=com.google.zxing.oned.EAN13Writer; usingEAN8Writer=com.google.zxing.oned.EAN8Writer; usingMultiFormatWriter=com.google.zxing.MultiFormatWriter;
方法:
stringcontent="二维码信息"; ByteMatrixbyteMatrix=newMultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,300,300); Bitmapbitmap=toBitmap(byteMatrix); pictureBox1.Image=bitmap; SaveFileDialogsFD=newSaveFileDialog(); sFD.Filter="保存图片(*.png)|*.png|所有文件(*.*)|*.*"; sFD.DefaultExt="*.png|*.png"; sFD.AddExtension=true; if(sFD.ShowDialog()==DialogResult.OK) { if(sFD.FileName!="") { writeToFile(byteMatrix,System.Drawing.Imaging.ImageFormat.Png,sFD.FileName); } }
解析二维码:
代码示例:
if(this.openFileDialog1.ShowDialog()!=DialogResult.OK) { return; } Imageimg=Image.FromFile(this.openFileDialog1.FileName); Bitmapbmap; try { bmap=newBitmap(img); } catch(System.IO.IOExceptionioe) { MessageBox.Show(ioe.ToString()); return; } if(bmap==null) { MessageBox.Show("Couldnotdecodeimage"); return; } LuminanceSourcesource=newRGBLuminanceSource(bmap,bmap.Width,bmap.Height); com.google.zxing.BinaryBitmapbitmap1=newcom.google.zxing.BinaryBitmap(newHybridBinarizer(source)); Resultresult; try { result=newMultiFormatReader().decode(bitmap1); } catch(ReaderExceptionre) { MessageBox.Show(re.ToString()); return; } MessageBox.Show(result.Text); publicstaticvoidwriteToFile(ByteMatrixmatrix,System.Drawing.Imaging.ImageFormatformat,stringfile) { Bitmapbmap=toBitmap(matrix); bmap.Save(file,format); } publicstaticBitmaptoBitmap(ByteMatrixmatrix) { intwidth=matrix.Width; intheight=matrix.Height; Bitmapbmap=newBitmap(width,height,System.Drawing.Imaging.PixelFormat.Format32bppArgb); for(intx=0;x<width;x++) { for(inty=0;y<height;y++) { bmap.SetPixel(x,y,matrix.get_Renamed(x,y)!=-1?ColorTranslator.FromHtml("0xFF000000"):ColorTranslator.FromHtml("0xFFFFFFFF")); } } returnbmap; }
PS:这里再为大家推荐一款功能非常强悍的二维码在线生成工具,免费供大家使用:
在线生成二维码工具(加强版):
http://tools.jb51.net/transcoding/jb51qrcode
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.netajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。