概述
一、项目添加ThoughtWorks.QRCode.dll和System.Drawing.dll的引用
二、创建二维码公共处理类(QRCodeHandler.cs)
/// <summary> /// 二维码处理类 /// </summary> public class QRCodeHandler { /// <summary> /// 创建二维码 /// </summary> /// <param name="QRString">二维码字符串</param> /// <param name="QRCodeEncodeMode">二维码编码(Byte、AlphaNumeric、Numeric)</param> /// <param name="QRCodeScale">二维码尺寸(Version为0时,1:26x26,每加1宽和高各加25</param> /// <param name="QRCodeVersion">二维码密集度0-40</param> /// <param name="QRCodeErrorCorrect">二维码纠错能力(L:7% M:15% Q:25% H:30%)</param> /// <param name="filePath">保存路径</param> /// <param name="hasLogo">是否有logo(logo尺寸50x50,QRCodeScale>=5,QRCodeErrorCorrect为H级)</param> /// <param name="logoFilePath">logo路径</param> /// <returns></returns> public bool CreateQRCode(string QRString, string QRCodeEncodeMode, short QRCodeScale, int QRCodeVersion, string QRCodeErrorCorrect, string filePath, bool hasLogo, string logoFilePath) { bool result = true; QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); switch (QRCodeEncodeMode) { case "Byte": qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; break; case "AlphaNumeric": qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC; break; case "Numeric": qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC; break; default: qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; break; } qrCodeEncoder.QRCodeScale = QRCodeScale; qrCodeEncoder.QRCodeVersion = QRCodeVersion; switch (QRCodeErrorCorrect) { case "L": qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L; break; case "M": qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; break; case "Q": qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q; break; case "H": qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; break; default: qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; break; } try { Image image = qrCodeEncoder.Encode(QRString, System.Text.Encoding.UTF8); System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); fs.Close(); if (hasLogo) { Image copyImage = System.Drawing.Image.FromFile(logoFilePath); Graphics g = Graphics.FromImage(image); int x = image.Width / 2 - copyImage.Width / 2; int y = image.Height / 2 - copyImage.Height / 2; g.DrawImage(copyImage, new Rectangle(x, y, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); g.Dispose(); image.Save(filePath); copyImage.Dispose(); } image.Dispose(); } catch (Exception ex) { result = false; } return result; } }
调用
QRCodeHandler qr = new QRCodeHandler(); string path = Path.Combine("C:\", "Down\"); string qrString = "http://blog.csdn.net/pan_junbiao"; //二维码字符串 string logoFilePath = path + "myLogo.jpg"; //Logo路径50*50 string filePath = path + "myCode.jpg"; //二维码文件名 qr.CreateQRCode(qrString, "Byte", 5, 0, "H", filePath, true, logoFilePath); //生成
转载于:https://www.cnblogs.com/macT/p/11395234.html
最后
以上就是合适手机为你收集整理的利用ThoughtWorks.QRCode生成二维码的全部内容,希望文章能够帮你解决利用ThoughtWorks.QRCode生成二维码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复