ThoughtWorks.QRCode使用示例

it2023-01-13  58

QRCodeHelper.cs

using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using ThoughtWorks.QRCode; using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; using System.IO; using System.Drawing.Imaging; namespace God.Tools { public class QRCodeHelper { public async Task<byte[]> SetQRAsync(string str) { QRCodeEncoder encoder = new QRCodeEncoder(); encoder.QRCodeEncodeMode = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字) encoder.QRCodeScale = 10;//大小(值越大生成的二维码图片像素越高)使用QRCodeDecoder最大能解码25像素的,手机扫描多大都无所谓 encoder.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误) encoder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M;//错误效验、错误更正(有4个等级) string qrdata = str; Bitmap bp = encoder.Encode(qrdata.ToString());//, Encoding.GetEncoding("GB2312") using (MemoryStream ms = new MemoryStream()) { bp.Save(ms, ImageFormat.Jpeg); byte[] bytes = new byte[ms.Length]; ms.Position = 0; await ms.ReadAsync(bytes, 0, bytes.Length); return bytes; } } } } /// <summary> /// 生成二维码 /// </summary> /// <param name="str"></param> /// <returns></returns> public async Task<Byte[]> GetQRCodeAsync(string str) { QRCodeHelper qrh = new QRCodeHelper(); return await qrh.SetQRAsync(str); }
最新回复(0)