• 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:keatkeatmy projectsasp.net core2.2html-to-pdfProject123.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

  • 相关阅读:
    vue中minxin---小记
    微信认证
    Fatal error Using $this when not in object context in
    $.ajax()方法详解
    $.post
    jquery中的each
    jquery的$.extend和$.fn.extend作用及区别
    javascript字符串函数
    serializeArray()与serialize()的区别
    一个登陆界面
  • 原文地址:https://www.cnblogs.com/keatkeat/p/11079292.html
Copyright © 2020-2023  润新知