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
- ZXing生成qrCode
- 使用ZXing生成QRcode二维码
- Java生成二维码(zxing、qrCode)
- zxing生成QRCODE的一个问题
- 使用Zxing生成和解析QRCODE
- Java使用zxing生成解读QRcode二维码
- zxing QRcode
- 利用qrcode.jar生成和解析二维码(非zxing)
- C# 生成二维码 两种方式ThoughtWorks.QRCode ZXing
- java生成二维码使用QRCode和ZXing两种方式
- java利用zxing开源工具生成二维码QRCode
- zxing QRcode生成jar包,并使项目正常运行
- java生成二维码使用QRCode和ZXing两种方式
- java生成二维码使用QRCode和ZXing两种方式
- java生成二维码使用QRCode和ZXing两种方式
- QRCode生成
- 使用zxing生成QRCode二维码,在Android的webview中调用条形码扫描
- Android及Java使用google.zxing生成和解码(解析)二维码(QRCode)
- Java数据库连接池封装与用法
- ROM、RAM、DRAM、SRAM和FLASH的区别 .
- update-script脚本语法说明
- algorithm_chapter1
- 而在Jquery中则使用$.map()、$.each()来操作数组
- ZXing生成qrCode
- Java内存的理解
- mysql的timestamp类型在django中使用
- sencha touch list css(样式) 详解
- codeforces 318 A.Even Odds B.Sereja and Array
- Maven入门之依赖-第三节
- 在windows系统中如何使用内存映射来读写文件
- 恩智浦率先推出超宽带Doherty放大器
- 带无线网卡的电脑共享网络wifi的方法