《C#二维码生成 ZXing例子.doc》由会员分享,可在线阅读,更多相关《C#二维码生成 ZXing例子.doc(15页珍藏版)》请在金锄头文库上搜索。
1、关于C#使用Zxing生成二维码以及对图片二维码解码的示例代码Zxing C#版的库在http:/ 2013-11-4日最好可以看看完整的例子。ASPX网站代码在http:/ 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 Cod
2、ing 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
3、; 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,
4、 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;
5、 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
6、(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 BitmapL
7、uminanceSource(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 编码?
8、内容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;