Asp.net core 學習筆記 QR code and Barcode


QR code 和 Barcode 經常會使用到。

Java 陣營有著名的 zxing

https://github.com/zxing/zxing

.Net 有對接它的 port

https://github.com/micjahn/ZXing.Net

調用很簡單

var qrWriter = new BarcodeWriterPixelData
{
    Format = BarcodeFormat.QR_CODE,
    Options = new EncodingOptions { Height = 1000, Width = 1000, Margin = 10 }
};
var pixelData = qrWriter.Write("i love you");
using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
using (var ms = new MemoryStream())
{
    // lock the data area for fast access
    var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height),
        System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
    try
    {
        // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
        System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0,
            pixelData.Pixels.Length);
    }
    finally
    {
        bitmap.UnlockBits(bitmapData);
    }
    // save to stream as PNG
    //bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    bitmap.Save(@"C:\keatkeat\my projects\asp.net core\2.2\html-to-pdf\Project\123.png", System.Drawing.Imaging.ImageFormat.Png);
}

Barcode Format 有很多種,可以自己選。

這里用到了 bitmap, 是從官網的 demo 里抄來的. 

早期 core 不支持 system draw, 所以 demo 里說依賴 CoreCompat.System.Drawing, 

但后來 .net core 推出了 System.Drawing.Common 所以是跨平台的了.

demo : https://github.com/micjahn/ZXing.Net/tree/master/Clients/ASP.NetCoreDemo

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM