asp.net生成二维码的方法总结

上传人:bin****86 文档编号:59370345 上传时间:2018-11-06 格式:DOCX 页数:9 大小:18.09KB
返回 下载 相关 举报
asp.net生成二维码的方法总结_第1页
第1页 / 共9页
asp.net生成二维码的方法总结_第2页
第2页 / 共9页
asp.net生成二维码的方法总结_第3页
第3页 / 共9页
asp.net生成二维码的方法总结_第4页
第4页 / 共9页
asp.net生成二维码的方法总结_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《asp.net生成二维码的方法总结》由会员分享,可在线阅读,更多相关《asp.net生成二维码的方法总结(9页珍藏版)》请在金锄头文库上搜索。

1、我真正系统地接触和学习党的基本知识是在这次中级党校的培训班上。通过学习,了解了党的发展历程,对党的性质、宗旨、任务等基本知识有了进一步的了解ASP.NET生成二维码的方法总结本文实例总结了ASP.NET生成二维码的方法。分享给大家供大家参考,具体如下:分享一例c#生成二维码的代码,直接引用ThoughtWorks.QRCode.dll 类生成二维码,有需要的朋友参考下。方法1.直接引用ThoughtWorks.QRCode.dll 类,生成二维码。代码示例:ThoughtWorks.QRCode.Codec.QRCodeEncoder encoder = new QRCodeEncoder()

2、;encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;/编码方法(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字)encoder.QRCodeScale = 4;/大小encoder.QRCodeVersion = 0;/版本(注意:设置为0主要是防止编码的字符串太长时发生错误)encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;String qrdata = 二维码信息;System.Drawing.Bitmap bp = encod

3、er.Encode(qrdata.ToString(), Encoding.GetEncoding(GB2312);Image image = bp;Object oMissing = System.Reflection.Missing.Value;pictureBox1.Image = bp;保存二维码图片:代码示例:SaveFileDialog sf = new SaveFileDialog();sf.Title = 选择保存文件位置;sf.Filter = 保存图片(*.jpg) |*.jpg|所有文件(*.*) |*.*;/设置默认文件类型显示顺序sf.FilterIndex = 1;

4、/保存对话框是否记忆上次打开的目录sf.RestoreDirectory = true;if (sf.ShowDialog() = DialogResult.OK) Image im = this.pictureBox1.Image; /获得文件路径 string localFilePath = sf.FileName.ToString(); if (sf.FileName != ) string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf() + 1);/获取文件名,不带路径 / newFileName =

5、fileNameExt+DateTime.Now.ToString(yyyyMMdd) ;/给文件名后加上时间 string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf(.); /获取文件路径,带文件名,不带后缀 string fn = sf.FileName; pictureBox1.Image.Save(FilePath +-+ DateTime.Now.ToString(yyyyMMdd) + .jpg); /解析二维码信息/ QRCodeDecoder decoder = new QRCodeDecod

6、er();/ String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(pictureBox1.Image);/this.label3.Text = decodedString;方法2.引用ZXing类库。ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码。目标是能够对QR编码、Data Matrix、UPC的1D条形码进行解码。于此同时,它同样提供 cpp,ActionScript,android,iPhone,rim,j2me,j2se,jruby,C#等方式的类库。zxing类库的作用主

7、要是解码,是目前开源类库中解码能力比较强的(商业的另说,不过对于动辄成千上万的类库授权费用,的确很值)。到谷歌code下载相应的代码1.下载zxing最新的包到zxing的主页: http:/ const System.String DEFAULT_BYTE_MODE_ENCODING = ISO-8859-1;此处,将ISO-8859-1改为UTF-8其二:com.google.zxing.qrcode.decoder.DecodedBitStreamParser类的成员private const System.String UTF8 = UTF8;应将UTF8改为UTF-8代码示例:usi

8、ng com.google.zxing.qrcode;using com.google.zxing;using mon;using ByteMatrix = mon.ByteMatrix;using EAN13Writer = com.google.zxing.oned.EAN13Writer;using EAN8Writer = com.google.zxing.oned.EAN8Writer;using MultiFormatWriter = com.google.zxing.MultiFormatWriter;方法:string content = 二维码信息;ByteMatrix by

9、teMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);Bitmap bitmap = toBitmap(byteMatrix);pictureBox1.Image = bitmap;SaveFileDialog sFD = new SaveFileDialog();sFD.Filter = 保存图片(*.png) |*.png|所有文件(*.*) |*.*;sFD.DefaultExt = *.png|*.png;sFD.AddExtension = true;if (sFD.Sh

10、owDialog() = DialogResult.OK)if (sFD.FileName != ) writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);解析二维码:代码示例:if (this.openFileDialog1.ShowDialog() != DialogResult.OK)return;Image img = Image.FromFile(this.openFileDialog1.FileName);Bitmap bmap;trybmap = new Bitmap(img);

11、catch (System.IO.IOException ioe)MessageBox.Show(ioe.ToString();return;if (bmap = null)MessageBox.Show(Could not decode image);return;LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);com.google.zxing.BinaryBitmap bitmap1 = new com.google.zxing.BinaryBitmap(new HybridBin

12、arizer(source);Result result;tryresult = new MultiFormatReader().decode(bitmap1);catch (ReaderException re)MessageBox.Show(re.ToString();return;MessageBox.Show(result.Text);public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file) Bitmap bmap = toBitma

13、p(matrix); bmap.Save(file, format);public static Bitmap toBitmap(ByteMatrix matrix) int width = matrix.Width; int height = matrix.Height; Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); for (int x = 0; x for (int y = 0; y bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml(0xFF) : ColorTranslator.FromHtml(0xFFFFFFFF);return bmap;希望本文所述对大家程序设计有所帮助。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 总结/报告

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号