C# 生成和识别二维码

生成和识别二维码

本来认为这功能在网上一搜一大堆,结果确实如此。但没有一个能用的,要么就是要收费,要么不能使用。 于是乎一顿搜索,经测试代码稳定,无论是纯二维码还是复杂图像嵌入二维码,均可识别



第一步:

Install-Package ZXing.Net -Version 0.16.6


第二步:

class Program
    {
        static void Main(string[] args)
        {
            var data = "http://programmingnotes.org/";

            // Encode data to a QR code byte array
            var bytes = CreateQRCode(data);

            //File.WriteAllBytes(@"c:\a.png", bytes);

            Console.WriteLine($"Length: {bytes.Length}");

            // Decode QR code to a string
            var result = ReadQRCode(bytes);

            var result2 = ReadQRCode(File.ReadAllBytes(@"E:\Desktop\erweima\微信图片_20210721150957.jpg"));

            Console.WriteLine($"Result: {result}");
        }

        /// <summary>
        /// Converts a string and encodes it to a QR code byte array
        /// </summary>
        /// <param name="data">The data to encode</param>
        /// <param name="height">The height of the QR code</param>
        /// <param name="width">The width of the QR code</param>
        /// <param name="margin">The margin around the QR code</param>
        /// <returns>The byte array of the data encoded into a QR code</returns>
        public static byte[] CreateQRCode(string data, int height = 100
                        , int width = 100, int margin = 0)
        {
            byte[] bytes = null;
            var barcodeWriter = new ZXing.BarcodeWriter()
            {
                Format = ZXing.BarcodeFormat.QR_CODE,
                Options = new ZXing.QrCode.QrCodeEncodingOptions()
                {
                    Height = height,
                    Width = width,
                    Margin = margin
                }
            };
            using (var image = barcodeWriter.Write(data))
            {
                using (var stream = new System.IO.MemoryStream())
                {
                    image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    bytes = stream.ToArray();
                }
            }
            return bytes;
        }

        /// <summary>
        /// Converts a QR code and decodes it to its string data 
        /// </summary>
        /// <param name="bytes">The QR code byte array</param> 
        /// <returns>The string data decoded from the QR code</returns>
        public static string ReadQRCode(byte[] bytes)
        {
            var result = string.Empty;
            using (var stream = new System.IO.MemoryStream(bytes))
            {
                using (var image = System.Drawing.Image.FromStream(stream))
                {
                    var barcodeReader = new ZXing.BarcodeReader();
                    var decoded = barcodeReader.Decode((System.Drawing.Bitmap)image);
                    if (decoded != null)
                    {
                        result = decoded.Text;
                    }
                }
            }
            return result;
        }
    }

参考引用

生成识别二维码

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值