In fact, there are many components that generate QR codes, such as QrcodeNet and zkweb Fork. QRCoder, QRCoder, etc
I chose QRCoder because it is small and easy to use, supports large concurrent requests, and does not rely on any library and network services.
Since it is net core of course, dependency injection should be used to inject into the controller through the constructor.
Software version
Asp.net Core:2.0
QRCoder:1.3. 3 (latest at the time of development)
Project structure
Snai.QRCode.Api Asp.net core 2.0 Api website
Project realization
New snai QRcode solution, create a new name snai under the solution QRCode. Api Asp. Net core 2.0 API website
Right click on the dependency management NuGet package and browse to find QRCoder version 1.3 3 download and install
Because dependency injection is used, dependency abstraction does not depend on implementation, so an interface to implement QR code should be built
Add the Common folder in the project and the IQRCode QR code interface in the folder. The interface defines the GetQRCode QR code method. The code is as follows
1 using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks; namespace Snai.QRCode.Api.Common
{ public interface IQRCode
{ Bitmap GetQRCode(string url, int pixel); }
}
Add the RaffQRCode class in the Common directory, inherit the IQRCode interface and implement the GetQRCode class. The code is as follows
using QRCoder;using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
namespace Snai.QRCode.Api.Common
{
public class RaffQRCode : IQRCode
{
/// <summary>
/// </summary>
///< param name = "URL" > store content < / param >
///< param name = "pixel" > pixel size < / param >
/// <returns></returns>
public Bitmap GetQRCode(string url, int pixel)
{
QRCodeGenerator generator = new QRCodeGenerator();
QRCodeData codeData = generator.CreateQrCode(url, QRCodeGenerator.ECCLevel.M, true);
QRCoder.QRCode qrcode = new QRCoder.QRCode(codeData);
Bitmap qrImage = qrcode.GetGraphic(pixel, Color.Black, Color.White, true);
return qrImage;
}
}
}
Modify startup CS code, inject the RaffQRCode class into the container
The code is as follows:
Copy code using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Snai.QRCode.Api.Common; namespace Snai.QRCode.Api { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IQRCode, RaffQRCode>(); services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); } } }
Add QRCodeController Api null controller under Controllers, adopt constructor dependency, and introduce the RaffQRCode class
Add the GetQRCode(string url, int pixel) method, add the HttpGet("/api/qrcode") routing address, and use it in the method_ iQRCode.GetQRCode(url, pixel) generates QR code and then outputs it
The code is as follows:
using System; using System.Collections.Generic; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Snai.QRCode.Api.Common; namespace Snai.QRCode.Api.Controllers { public class QRCodeController : Controller { private IQRCode _iQRCode; public QRCodeController(IQRCode iQRCode) { _iQRCode = iQRCode; } /// <summary> /// Get QR code /// </summary> /// <param name="url">Storage content</param> /// <param name="pixel">Pixel size</param> /// <returns></returns> [HttpGet("/api/qrcode")] public void GetQRCode(string url, int pixel) { Response.ContentType = "image/jpeg"; var bitmap = _iQRCode.GetQRCode(url, pixel); MemoryStream ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Jpeg); Response.Body.WriteAsync(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length)); Response.Body.Close(); } } }
At this point, all the code has been written
Start and run the project and open it in the browser http://localhost:5000//api/qrcode?url=http://www.baidu.com&pixel=4 Address to get the QR code of the URL parameter domain name
/ * GetGraphic method parameter description
public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon = null, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true)
*
int pixelsPerModule: the pixel size of the generated QR code picture, which I set here is 5
*
Color darkColor: dark color} is generally set to color Black black
*
Color lightColor: bright color is generally set to color White white
*
Bitmap icon: QR code watermark icon, for example: Bitmap icon = new Bitmap(context.Server.MapPath("~/images/zs.png"); The default value is NULL. Plus this QR code, an icon will be displayed in the middle
*
int iconSizePercent: the size scale of the watermark icon, which can be set according to your preferences
*
int iconBorderWidth: the border of the watermark icon
*
bool drawQuietZones: a static area, a blank boundary on one side of the QR code, which is used to prevent readers from obtaining information irrelevant to the QR code being browsed, that is, whether to draw the blank border area of the QR code. The default is true
*/