Skip to content

Commit

Permalink
[WebApplicationSample] 使用 QRCoder 生产二维码SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
Roc committed Dec 20, 2018
1 parent f5f3c8f commit 4ef1272
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
21 changes: 21 additions & 0 deletions samples/WebApplicationSample/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Diagnostics;
using System.Drawing;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using QRCoder;
using WebApplicationSample.Models;

namespace WebApplicationSample.Controllers
Expand All @@ -24,6 +27,24 @@ public IActionResult Gratuity()
return View();
}

/// <summary>
/// 生成二维码SVG
/// </summary>
/// <param name="data"></param>
/// <param name="size"></param>
/// <returns></returns>
[HttpGet]
public IActionResult QrCode(string data, int size = 168)
{
using (var qrGenerator = new QRCodeGenerator())
using (var qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.L))
using (var svgQrCode = new SvgQRCode(qrCodeData))
{
var svgText = svgQrCode.GetGraphic(new Size(size, size));
return File(Encoding.UTF8.GetBytes(svgText), "text/xml");
}
}

public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
Expand Down
2 changes: 1 addition & 1 deletion samples/WebApplicationSample/Views/Alipay/PreCreate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<label>QrCode:</label>
@if (!string.IsNullOrEmpty(ViewData["qrcode"] as string))
{
<img src="https://api.qrserver.com/v1/create-qr-code/?size=168x168&data=@ViewData["qrcode"]" />
<embed src="../Home/QrCode?size=168&data=@ViewData["qrcode"]" width="168" height="168" type="image/svg+xml" />
}
</div>
<div class="form-group">
Expand Down
6 changes: 3 additions & 3 deletions samples/WebApplicationSample/Views/JDPay/QrCodePay.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
<div class="form-group">
<label>QrCode:</label>
@if (!string.IsNullOrEmpty(ViewData["qrcode"] as string))
{
<img src="https://api.qrserver.com/v1/create-qr-code/?size=168x168&data=@ViewData["qrcode"]" />
}
{
<embed src="../Home/QrCode?size=168&data=@ViewData["qrcode"]" width="168" height="168" type="image/svg+xml" />
}
</div>
<div class="form-group">
<label>Response:</label>
Expand Down
2 changes: 1 addition & 1 deletion samples/WebApplicationSample/Views/QPay/QrCodePay.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<label>QrCode:</label>
@if (!string.IsNullOrEmpty(ViewData["qrcode"] as string))
{
<img src="https://api.qrserver.com/v1/create-qr-code/?size=168x168&data=@ViewData["qrcode"]" />
<embed src="../Home/QrCode?size=168&data=@ViewData["qrcode"]" width="168" height="168" type="image/svg+xml" />
}
</div>
<div class="form-group">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<label>QrCode:</label>
@if (!string.IsNullOrEmpty(ViewData["qrcode"] as string))
{
<img src="https://api.qrserver.com/v1/create-qr-code/?size=168x168&data=@ViewData["qrcode"]" />
<embed src="../Home/QrCode?size=168&data=@ViewData["qrcode"]" width="168" height="168" type="image/svg+xml" />
}
</div>
<div class="form-group">
Expand Down
1 change: 1 addition & 0 deletions samples/WebApplicationSample/WebApplicationSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
<PackageReference Include="QRCoder" Version="1.3.5" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 4ef1272

Please sign in to comment.