ZXing生成qrCode

来源:互联网 发布:木木聊天软件 编辑:程序博客网 时间:2024/06/05 05:46
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using com.google.zxing.common;using System.Drawing;using com.google.zxing;namespace LinSpace{/// <summary>///qrCode 的摘要说明/// </summary>public class QrCode{public QrCode(){}public Color bkColor = ColorTranslator.FromHtml("0xFFFFFFFF");public Color BkColor{get { return this.bkColor; }set { this.bkColor = value; }}public Color codeColor = ColorTranslator.FromHtml("0xFF000000");public Color CodeColor{get { return this.codeColor; }set { this.codeColor = value; }}public int qrCodeMaxWidth=200;public int QrCodeMaxWidth{get { return this.qrCodeMaxWidth; }set { this.qrCodeMaxWidth = value; }}public int qrCodeMaxHeight=200;public int QrCodeMaxHeight{get { return this.qrCodeMaxHeight; }set { this.qrCodeMaxHeight = value; }}public Bitmap logoBmp = null;public Bitmap LogoBmp{get { return this.logoBmp; }set { this.logoBmp = value; }}public Bitmap qrCodeBmp;public Bitmap QrCodeBmp{get { return this.qrCodeBmp; }}public Point orderPosition=new Point();public Point OrderPosition{get { return this.orderPosition; }}/// <summary>/// 把内容装换成Matrix/// </summary>/// <param name="content"></param>/// <returns></returns>public ByteMatrix ToByteMatrix(string content){    return new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrCodeMaxWidth, qrCodeMaxHeight);}/// <summary>/// 把Matrix转换成Bitmap/// </summary>/// <param name="matrix"></param>public void ToBitmap(ByteMatrix matrix){int width = matrix.Width;int height = matrix.Height;qrCodeBmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);int borderTop=-1, borderLeft=-1;for (int x = 0; x < width; x++){for (int y = 0; y < height; y++){//取得生成后白边的起始位置if (borderLeft == -1 && borderTop == -1 && matrix.get_Renamed(x, y)!=-1){borderTop = y;borderLeft= x;}qrCodeBmp.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? codeColor : bkColor);}}orderPosition.X = borderTop;orderPosition.Y = borderLeft;   }/// <summary>/// 添加logo至二维码正中央/// </summary>int sadness = 5;public void AddLogoToQrCode(){Graphics g = Graphics.FromImage(this.qrCodeBmp);int actualWidht = this.qrCodeBmp.Width - this.orderPosition.X * 2;int actualHeight = this.qrCodeBmp.Height - this.orderPosition.Y * 2;loadGenerateLogo(actualWidht / sadness, actualHeight / sadness);//g.DrawImage(this.logoBmp, (this.qrCodeBmp.Width - maxThumbnailSide) / 2, (this.qrCodeBmp.Height - maxThumbnailSide) / 2);g.DrawImage(this.logoBmp, (this.qrCodeBmp.Width - actualWidht / sadness) / 2, (this.qrCodeBmp.Height - actualHeight / sadness) / 2);g.Dispose();}public void LoadQrCode(string content){ByteMatrix byteMatrix = toByteMatrix(content);toBitmap(byteMatrix);if (this.logoBmp != null){addLogoToQrCode();}}/// <summary>/// 缩放logo。根据二维码的宽度的1/3/// </summary>/// <param name="maxThumbnailWidth"></param>/// <param name="maxThumbnailHeight"></param>public void LoadGenerateLogo(int maxThumbnailWidth, int maxThumbnailHeight){Bitmap oldBitmap = this.logoBmp;Bitmap newBitmap = new Bitmap(maxThumbnailWidth, maxThumbnailHeight);int PdtPicWidth = oldBitmap.Width;int PdtPicHeight = oldBitmap.Height;int generateWidth = maxThumbnailWidth;if (PdtPicWidth < PdtPicHeight){//缩放至高度相符的宽度generateWidth = (int)((float)PdtPicWidth / PdtPicHeight * maxThumbnailHeight);}Graphics g = Graphics.FromImage(newBitmap);//g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, newBitmap.Width, newBitmap.Height));Bitmap drawBitmap = zoomPic(oldBitmap, generateWidth);g.DrawImage(drawBitmap, (newBitmap.Width - drawBitmap.Width) / 2, (newBitmap.Height - drawBitmap.Height) / 2);drawBitmap.Dispose();oldBitmap.Dispose();g.Dispose();this.logoBmp = newBitmap;}/// <summary>/// 按比例缩小图片,自动计算高度/// </summary>/// <param name="strOldPic">源图文件)</param>/// <param name="intWidth">缩小至宽度</param>public static Bitmap ZoomPic(Bitmap objPic, int intWidth){System.Drawing.Bitmap objNewPic;int intHeight = (int)(((float)intWidth / objPic.Width) * objPic.Height);objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);return objNewPic;}}}

  大嘴巴:http://www.bigzb.com