资源预览内容
第1页 / 共15页
第2页 / 共15页
第3页 / 共15页
第4页 / 共15页
第5页 / 共15页
第6页 / 共15页
第7页 / 共15页
第8页 / 共15页
第9页 / 共15页
第10页 / 共15页
亲,该文档总共15页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
关于C#使用Zxing生成二维码以及对图片二维码解码的示例代码Zxing C#版的库在http:/zxingnet.codeplex.com/ZXing.Net.0.12.0.0.zip最新版本 2013-11-4日最好可以看看完整的例子。ASPX网站代码在http:/pan.baidu.com/share/link?shareid=3129567452&uk=721874100原来用VS2005的,VS2010也可以打开的。其它版本没试过,应该都可以,毕竟里面用的东西几乎没有什么东西。下面是我写的一个CodeHelper类,很简单只是对ZXing的部分封装而已。参考了网上的java代码的教程,C#很少。貌似版本也不对啊,有几个类没有。using System;using System.Collections.Generic;using System.Text;using ZXing;using ZXing.QrCode;using ZXing.Common;using System.Drawing;using System.IO;using System.Drawing.Imaging;using System.Collections;namespace Coding public class CodeHelper private string _charset = UTF-8;/默认的貌似中文不能识别,解码后会是?号。你可以试试改成其它编码 public string CHARSET get return _charset; set _charset = value; public int SaveTo(Bitmap bitmap, string filepath, string filename) try bitmap.Save(filepath + filename); return 0; catch (Exception) return -1; public Bitmap encode(string content, int width, int height, BarcodeFormat barcodeFormat, IDictionary hints) BitMatrix byteMatrix = new MultiFormatWriter().encode(content, barcodeFormat, width, height, hints); Bitmap bitmap = toBitmap(byteMatrix); return bitmap; public Bitmap encode(string content, int width, int height,BarcodeFormat barcodeFormat) BitMatrix byteMatrix = new MultiFormatWriter().encode(content, barcodeFormat, width, height); Bitmap bitmap = toBitmap(byteMatrix); return bitmap; public static Bitmap toBitmap(BitMatrix 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 width; x+) for (int y = 0; y height; y+) bmap.SetPixel(x, y, matrixx, y ? ColorTranslator.FromHtml(0xFF000000) : ColorTranslator.FromHtml(0xFFFFFFFF); return bmap; public int encodeAndSave(string content, string path, int width, int height,BarcodeFormat barcodeFormat) return SaveTo(encode(content, width, height, barcodeFormat), path, ); public int decode(string bmppath, IDictionary hints, out Result result) Bitmap bmap = new Bitmap(bmppath); BitmapLuminanceSource source = new BitmapLuminanceSource(bmap); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source); result = new MultiFormatReader().decode(bitmap, hints); return 0; 然后下面是一个调用例子,里面有些乱码需要删掉的,我复制到word时出现的。ASPX前台页面 二t维?码?生成与?解a码? .container width:960px; margin:0 auto; border:1px solid #ff0000; .container div 编码?内容Y:o为a空?时将?随?机生成。 logo图?片?:o 编码?内容Y:o 二t维?码?图?片?:o 二t维?码?解a码?结果?:o ASPX的后台代码, using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using Coding;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号