using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using ZXing;
using System.IO;
namespace LotteryCustomerService.Common
{
public class QRCodeHelper
{
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="msg">信息</param>
/// <param name="px">长、宽长度</param>
/// <returns>Img</returns>
public static Bitmap GetQRCodeImg(string msg, int px = 250)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//编码问题
writer.Options.Hints.Add(
EncodeHintType.ERROR_CORRECTION,
ZXing.QrCode.Internal.ErrorCorrectionLevel.L //容错率,越高像素越大,图片越密
);
int codeSizeInPixels = px; //设置图片长宽
writer.Options.Height = writer.Options.Width = codeSizeInPixels;
writer.Options.Margin = 0;//设置边框
ZXing.Common.BitMatrix bm = writer.Encode(msg);
Bitmap img = writer.Write(bm);
return img;
}
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="msg">信息</param>
/// <param name="px">长、宽长度</param>
/// <returns>二进制流</returns>
public static Stream GetQRCodeStream(string msg, int px)
{
Bitmap img = GetQRCodeImg(msg, px);
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, ImageFormat.Jpeg);
return ms;
}
}
public static byte[] GetQRCodeArray(string msg, int px)
{
Bitmap img = GetQRCodeImg(msg, px);
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, ImageFormat.Jpeg);
byte[] data = new byte[ms.Length];
data = ms.ToArray();
return data;
}
}
}
}