C#二维码生成 ZXing例子.doc_第1页
C#二维码生成 ZXing例子.doc_第2页
C#二维码生成 ZXing例子.doc_第3页
C#二维码生成 ZXing例子.doc_第4页
C#二维码生成 ZXing例子.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

关于C#使用Zxing生成二维码以及对图片二维码解码的示例代码Zxing C#版的库在/ZXing.Net..zip最新版本 2013-11-4日最好可以看看完整的例子。ASPX网站代码在/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;using ZXing;using System.Text;using System.Collections;using System.Collections.Generic;using System.Drawing;using System.IO;public partial class _Default : System.Web.UI.Page public string CHARSET = UTF-8; public string CodeName get return ViewStatecodename as string; set ViewStatecodename = value; protected void Page_Load(object sender, EventArgs e) / / 创建二t维?码?并保存? / / 编码?内容Y,?字?符?串?(没?试?过y最?长有D多长,?不?能为a空?字?符?串?)? / protected void CreateQRCode(string content,string path) CodeHelper helper = new CodeHelper(); int width = 256; int height = 256; BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE; /下面是影响编码的一些参数 Dictionary hints = new Dictionary(); hints.Add(EncodeHintType.MARGIN, 1);/边距,知道HTML的话应该能理解它是什么,不能理解的话,改成4或者注释掉试试生成的效果。 hints.Add(EncodeHintType.CHARACTER_SET, CHARSET); hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);/设置容错等级,不设置这个,之后加了logo图片就不能识别了。可以改变试试查看ErrorCorrectionLevel枚举应该能知道它有什么值。 helper.SaveTo(helper.encode(content, width, height, barcodeFormat,hints),path,); protected void btnEncode_Click(object sender, EventArgs e) string randSeedStr = 中D文ABCDEFGHIJKLKDLKDKLDLK我你他AFJhibiebubiud他何必都id1241232341234; /string randSeedStr = ABCDEFGHIJKLKDLKDKLDLKAFJhibiebubiudid1241232341234; string path = DateTime.Now.ToString(yyyyMMddHHmmss) + .png; string content = GetContent(); if (string.IsNullOrEmpty(content) content = GetRandomStr(randSeedStr); CreateQRCode(content, Server.MapPath(/codes/) + path); /=图?片?中D间?加logo图片片?= System.Drawing.Image bitmap = System.Drawing.Image.FromFile(Server.MapPath(/codes/) + path); string logopath = Server.MapPath(/) + logo.png; if (File.Exists(logopath) try File.Delete(logopath); catch fileUpload.SaveAs(logopath); System.Drawing.Image logo = System.Drawing.Image.FromFile(logopath); Graphics g = Graphics.FromImage(bitmap); /logo图?片?的?大小?,?不?要a太?大,?参?考?网?上?50*50貌2似?很好? float logoWidth = 50; float logoHeight = 50; float centerX = (bitmap.Width -logoWidth) / 2; float centerY = (bitmap.Height - logoHeight) / 2; g.DrawImage(logo, centerX, centerY, logoWidth, logoHeight); g.Save(); bitmap.Save(Server.MapPath(/codes/) + logo.png); CodeName =logo.png; /= /默?认?保存?到?此?位?置? this.imgCode.ImageUrl = /codes/logo.png; this.lbContent.Text = content; protected void btnDecode_Click(object sender, EventArgs e) CodeHelper helper = new CodeHelper(); Result result = null; Dictionary hints = new Dictionary(); hints.Add(DecodeHintType.CHARACTER_SET, CHARSET); helper.decode(Server.MapPath(/codes/) + CodeName, hints, out result); if (result != null) this.lbResult.Text = result.Text; else this.lbResult.Text = 失败!?; protected string GetContent() return tbContent.Text; protected string GetRandomStr(string randString) char randChars = randString

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论