<img src="@Url.Action("ShowQRCode","SafetyEquipment", new { id = Model.Id, codeNum = Model.Number })" style="padding-top:20px;" />
public ActionResult ShowQRCode(Guid id, string codeNum)
{
MemoryStream sm = CreateCode(id, codeNum);
byte[] bytes = new byte[sm.Length];
sm.Seek(0, SeekOrigin.Begin);
sm.Read(bytes, 0, Convert.ToInt32(sm.Length)); return File(bytes, @"image/png");
}
public MemoryStream CreateCode(Guid id, string codeNum)
{
//moa链接地址
string moaPath = AppSetting.GetString(Constant.APPSET_EQUIPMENT_QRCODE_MOAURL);
//logo和扫描图标地址
string logoPath = Server.MapPath("~/Content/PersonalCenter/img/中国移动.png");
string smPath = Server.MapPath("~/Content/PersonalCenter/img/scanning.gif"); //生成二维码
var service = this.GetService<ISafetyEquipmentService>(); return service.CreateCode(codeNum, moaPath + "?id=" + id, logoPath, smPath);
}
/// <summary>
/// 单个二维码
/// </summary>
/// <param name="number">设备编码</param>
/// <param name="moaPath">moa链接</param>
/// <param name="logoPath">logo图标地址</param>
/// <param name="smPath">扫描图标地址</param>
/// <returns></returns>
public MemoryStream CreateCode(string number, string moaPath, string logoPath, string smPath)
{
//生成二维码
var stream = QRCodeHelp.CreateQRCode(moaPath, number.PadLeft(8, '0'), logoPath, smPath); return stream;
}
public static MemoryStream CreateQRCode(string alink, string number, string logoPath, string smPath)
{
QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
Bitmap logoImg = new Bitmap(logoPath);
Bitmap smImg = new Bitmap(smPath); return CreateQRCodeStream(alink, number, qrGenerator, logoImg, smImg);
}
/// <summary>
/// 二维码合成图片
/// </summary>
/// <param name="alink">文本/链接</param>
/// <param name="number">编号</param>
/// <param name="QRCodeGenerator">qr类</param>
/// <param name="logoImg">logo</param>
/// <param name="smImg">扫描图标</param>
/// <returns>字节</returns>
public static MemoryStream CreateQRCodeStream(string alink, string number, QRCodeGenerator qrGenerator, Bitmap logoImg, Bitmap smImg)
{
//qrcode
QRCode qrcode = new QRCode(qrGenerator.CreateQrCode(alink, QRCodeGenerator.ECCLevel.Q)); //qrBitmap
Bitmap qrImg = qrcode.GetGraphic(3, Color.Black, Color.White, null, 15, 6, false); //将logo合并在二维码中
Bitmap resultImg = QRCodeHelp.MergeQrImg(qrImg, logoImg, smImg, number); MemoryStream stream = new MemoryStream(); resultImg.Save(stream, ImageFormat.Png); stream.Flush();
stream.Position = 0; return stream;
}
#region 图形处理 /// <summary>
/// 合并用户QR图片和用户头像
/// </summary>
/// <param name="qrImg">QR图片</param>
/// <param name="logoImg">用户头像</param>
/// <param name="sm">扫描logo</param>
/// <param name="number">编码</param>
/// <returns></returns>
public static Bitmap MergeQrImg(Bitmap qrImg, Bitmap logoImg, Bitmap smImg, string number, double n = 0.23)
{
//logo外边距
int margin = 5;
float dpix = qrImg.HorizontalResolution;
float dpiy = qrImg.VerticalResolution; //logo缩放比例
var _newWidth = (7 * qrImg.Width - 36 * margin) * 1.0f / 36;
var _headerImg = QRCodeHelp.ZoomPic(logoImg, _newWidth / logoImg.Width);
//处理头像
int newImgWidth = _headerImg.Width + margin;
Bitmap headerBgImg = new Bitmap(newImgWidth, newImgWidth);
headerBgImg.MakeTransparent();
Graphics g = Graphics.FromImage(headerBgImg);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
Pen p = new Pen(new SolidBrush(Color.White));
Rectangle rect = new Rectangle(0, 0, newImgWidth - 1, newImgWidth - 1);
using (GraphicsPath path = QRCodeHelp.CreateRoundedRectanglePath(rect, 1))
{
g.DrawPath(p, path);
g.FillPath(new SolidBrush(Color.White), path);
}
//画头像
Bitmap img1 = new Bitmap(_headerImg.Width, _headerImg.Width);
Graphics g1 = Graphics.FromImage(img1);
g1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g1.Clear(Color.Transparent);
Pen p1 = new Pen(new SolidBrush(Color.Gray));
Rectangle rect1 = new Rectangle(0, 0, _headerImg.Width - 1, _headerImg.Width - 1);
using (GraphicsPath path1 = QRCodeHelp.CreateRoundedRectanglePath(rect1, 1))
{
g1.DrawPath(p1, path1);
TextureBrush brush = new TextureBrush(_headerImg);
g1.FillPath(brush, path1);
}
g1.Dispose();
PointF center = new PointF((newImgWidth - _headerImg.Width) / 2, (newImgWidth - _headerImg.Height) / 2);
g.DrawImage(img1, center.X, center.Y, _headerImg.Width, _headerImg.Height);
g.Dispose(); //背景图整体高度60
Bitmap backgroudImg = new Bitmap(185, 235);
backgroudImg.MakeTransparent();
backgroudImg.SetResolution(dpix, dpiy);
headerBgImg.SetResolution(dpix, dpiy);
Graphics g2 = Graphics.FromImage(backgroudImg);
g2.Clear(Color.White);
//背景图上部高度+25
g2.DrawImage(qrImg, 7, 25);
//logo下移+25
PointF center2 = new PointF((qrImg.Width - headerBgImg.Width) / 2 + 7, (qrImg.Height - headerBgImg.Height) / 2 + 25);
g2.DrawImage(headerBgImg, center2);
g2.Dispose(); Graphics g3 = Graphics.FromImage(backgroudImg);
Font font = new Font("宋体", 13, FontStyle.Regular);
SolidBrush sbrush = new SolidBrush(Color.DarkGray);
//编码位置
g3.DrawString(number, font, sbrush, new PointF(52, 2));
//提示语位置
g3.DrawString("请使用MOA端扫描", font, sbrush, new PointF(32, 205));
//扫描图标
g3.DrawImage(smImg, 20, 206); return backgroudImg;
} /// <summary>
/// 创建圆角矩形
/// </summary>
/// <param name="rect">区域</param>
/// <param name="cornerRadius">圆角角度</param>
/// <returns></returns>
private static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
{
//下午重新整理下,圆角矩形
GraphicsPath roundedRect = new GraphicsPath();
roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
roundedRect.CloseFigure();
return roundedRect;
} /// <summary>
/// 图片按比例缩放
/// </summary>
private static Image ZoomPic(Image initImage, double n)
{
//缩略图宽、高计算
double newWidth = initImage.Width;
double newHeight = initImage.Height + 100;
newWidth = n * initImage.Width;
newHeight = n * initImage.Height;
//生成新图
//新建一个bmp图片
System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
//新建一个画板
System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);
//设置质量
newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//置背景色
newG.Clear(Color.Transparent);
//画图
newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel); newG.Dispose();
return newImage;
} #endregion
public ActionResult ShowQRCode(Guid id, string codeNum)
{
MemoryStream sm = CreateCode(id, codeNum);
byte[] bytes = new byte[sm.Length];
sm.Seek(0, SeekOrigin.Begin);
sm.Read(bytes, 0, Convert.ToInt32(sm.Length)); return File(bytes, @"image/png");
}
public MemoryStream CreateCode(Guid id, string codeNum)
{
//moa链接地址
string moaPath = AppSetting.GetString(Constant.APPSET_EQUIPMENT_QRCODE_MOAURL);
//logo和扫描图标地址
string logoPath = Server.MapPath("~/Content/PersonalCenter/img/中国移动.png");
string smPath = Server.MapPath("~/Content/PersonalCenter/img/scanning.gif"); //生成二维码
var service = this.GetService<ISafetyEquipmentService>(); return service.CreateCode(codeNum, moaPath + "?id=" + id, logoPath, smPath);
}
/// <summary>
/// 单个二维码
/// </summary>
/// <param name="number">设备编码</param>
/// <param name="moaPath">moa链接</param>
/// <param name="logoPath">logo图标地址</param>
/// <param name="smPath">扫描图标地址</param>
/// <returns></returns>
public MemoryStream CreateCode(string number, string moaPath, string logoPath, string smPath)
{
//生成二维码
var stream = QRCodeHelp.CreateQRCode(moaPath, number.PadLeft(8, '0'), logoPath, smPath); return stream;
}
public static MemoryStream CreateQRCode(string alink, string number, string logoPath, string smPath)
{
QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
Bitmap logoImg = new Bitmap(logoPath);
Bitmap smImg = new Bitmap(smPath); return CreateQRCodeStream(alink, number, qrGenerator, logoImg, smImg);
}
/// <summary>
/// 二维码合成图片
/// </summary>
/// <param name="alink">文本/链接</param>
/// <param name="number">编号</param>
/// <param name="QRCodeGenerator">qr类</param>
/// <param name="logoImg">logo</param>
/// <param name="smImg">扫描图标</param>
/// <returns>字节</returns>
public static MemoryStream CreateQRCodeStream(string alink, string number, QRCodeGenerator qrGenerator, Bitmap logoImg, Bitmap smImg)
{
//qrcode
QRCode qrcode = new QRCode(qrGenerator.CreateQrCode(alink, QRCodeGenerator.ECCLevel.Q)); //qrBitmap
Bitmap qrImg = qrcode.GetGraphic(3, Color.Black, Color.White, null, 15, 6, false); //将logo合并在二维码中
Bitmap resultImg = QRCodeHelp.MergeQrImg(qrImg, logoImg, smImg, number); MemoryStream stream = new MemoryStream(); resultImg.Save(stream, ImageFormat.Png); stream.Flush();
stream.Position = 0; return stream;
}
#region 图形处理 /// <summary>
/// 合并用户QR图片和用户头像
/// </summary>
/// <param name="qrImg">QR图片</param>
/// <param name="logoImg">用户头像</param>
/// <param name="sm">扫描logo</param>
/// <param name="number">编码</param>
/// <returns></returns>
public static Bitmap MergeQrImg(Bitmap qrImg, Bitmap logoImg, Bitmap smImg, string number, double n = 0.23)
{
//logo外边距
int margin = 5;
float dpix = qrImg.HorizontalResolution;
float dpiy = qrImg.VerticalResolution; //logo缩放比例
var _newWidth = (7 * qrImg.Width - 36 * margin) * 1.0f / 36;
var _headerImg = QRCodeHelp.ZoomPic(logoImg, _newWidth / logoImg.Width);
//处理头像
int newImgWidth = _headerImg.Width + margin;
Bitmap headerBgImg = new Bitmap(newImgWidth, newImgWidth);
headerBgImg.MakeTransparent();
Graphics g = Graphics.FromImage(headerBgImg);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
Pen p = new Pen(new SolidBrush(Color.White));
Rectangle rect = new Rectangle(0, 0, newImgWidth - 1, newImgWidth - 1);
using (GraphicsPath path = QRCodeHelp.CreateRoundedRectanglePath(rect, 1))
{
g.DrawPath(p, path);
g.FillPath(new SolidBrush(Color.White), path);
}
//画头像
Bitmap img1 = new Bitmap(_headerImg.Width, _headerImg.Width);
Graphics g1 = Graphics.FromImage(img1);
g1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g1.Clear(Color.Transparent);
Pen p1 = new Pen(new SolidBrush(Color.Gray));
Rectangle rect1 = new Rectangle(0, 0, _headerImg.Width - 1, _headerImg.Width - 1);
using (GraphicsPath path1 = QRCodeHelp.CreateRoundedRectanglePath(rect1, 1))
{
g1.DrawPath(p1, path1);
TextureBrush brush = new TextureBrush(_headerImg);
g1.FillPath(brush, path1);
}
g1.Dispose();
PointF center = new PointF((newImgWidth - _headerImg.Width) / 2, (newImgWidth - _headerImg.Height) / 2);
g.DrawImage(img1, center.X, center.Y, _headerImg.Width, _headerImg.Height);
g.Dispose(); //背景图整体高度60
Bitmap backgroudImg = new Bitmap(185, 235);
backgroudImg.MakeTransparent();
backgroudImg.SetResolution(dpix, dpiy);
headerBgImg.SetResolution(dpix, dpiy);
Graphics g2 = Graphics.FromImage(backgroudImg);
g2.Clear(Color.White);
//背景图上部高度+25
g2.DrawImage(qrImg, 7, 25);
//logo下移+25
PointF center2 = new PointF((qrImg.Width - headerBgImg.Width) / 2 + 7, (qrImg.Height - headerBgImg.Height) / 2 + 25);
g2.DrawImage(headerBgImg, center2);
g2.Dispose(); Graphics g3 = Graphics.FromImage(backgroudImg);
Font font = new Font("宋体", 13, FontStyle.Regular);
SolidBrush sbrush = new SolidBrush(Color.DarkGray);
//编码位置
g3.DrawString(number, font, sbrush, new PointF(52, 2));
//提示语位置
g3.DrawString("请使用MOA端扫描", font, sbrush, new PointF(32, 205));
//扫描图标
g3.DrawImage(smImg, 20, 206); return backgroudImg;
} /// <summary>
/// 创建圆角矩形
/// </summary>
/// <param name="rect">区域</param>
/// <param name="cornerRadius">圆角角度</param>
/// <returns></returns>
private static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
{
//下午重新整理下,圆角矩形
GraphicsPath roundedRect = new GraphicsPath();
roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
roundedRect.CloseFigure();
return roundedRect;
} /// <summary>
/// 图片按比例缩放
/// </summary>
private static Image ZoomPic(Image initImage, double n)
{
//缩略图宽、高计算
double newWidth = initImage.Width;
double newHeight = initImage.Height + 100;
newWidth = n * initImage.Width;
newHeight = n * initImage.Height;
//生成新图
//新建一个bmp图片
System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
//新建一个画板
System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);
//设置质量
newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//置背景色
newG.Clear(Color.Transparent);
//画图
newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel); newG.Dispose();
return newImage;
} #endregion
凑巧,之前我也记录过,用的zxing
https://blog.csdn.net/hanjun0612/article/details/53504998