侧边栏壁纸
  • 累计撰写 62 篇文章
  • 累计收到 17 条评论

C#zxing插件处理条码与二维码

一缕清风
2021-01-16 / 0 评论 / 40 阅读 / 正在检测是否收录...

简介

Zxing.net是一个开放的源码,是基于的C#条码处理库,支持一维码、二维码生成与识别。

GitHub

开源地址:https://github.com/micjahn/ZXing.Net

使用教程

生成条码

    class Program
    {
        static void Main(string[] args)
        {
            GenerateBarCode("123456789",500,80);
            Console.ReadKey();
        }
        /// <summary>
        /// 生成一维条形码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static Bitmap GenerateBarCode(string text, int width, int height)
        {
            BarcodeWriter writer = new BarcodeWriter();
            //条码类型
            writer.Format = BarcodeFormat.CODE_39;
            EncodingOptions options = new EncodingOptions()
            {
                Width = width,
                Height = height,
                Margin = 2
            };
            writer.Options = options;
            Bitmap map = writer.Write(text);
            map.Save("1.bmp");
            return map;
        }
    }

001.png

生成二维码

    class Program
    {
        static void Main(string[] args)
        {
            GenerateQrCode("这是一个有码挺好测试程序", 500, 500);
            Console.ReadKey();
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static Bitmap GenerateQrCode(string text, int width, int height)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            QrCodeEncodingOptions options = new QrCodeEncodingOptions()
            {
                DisableECI = true,//设置内容编码
                CharacterSet = "UTF-8",  //设置二维码的宽度和高度
                Width = width,
                Height = height,
                Margin = 1//设置二维码的边距,单位不是固定像素
            };

            writer.Options = options;
            Bitmap map = writer.Write(text);
            map.Save("1.bmp");
            
            return map;
        }
    }

002.png

LOGO二维码

    class Program
    {
        static void Main(string[] args)
        {
            GenerateLogoQrCode("这是一个有码挺好测试程序", 500, 500);
            Console.ReadKey();
        }

        /// <summary>
        /// 生成带Logo的二维码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public static Bitmap GenerateLogoQrCode(string text, int width, int height)
        {
            //Logo 图片
            string logoPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\logo0.png";
            Bitmap logo = new Bitmap(logoPath);
            //构造二维码写码器
            MultiFormatWriter writer = new MultiFormatWriter();
            Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
            hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
 
            //生成二维码 
            BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hint);
            BarcodeWriter barcodeWriter = new BarcodeWriter();
            Bitmap map = barcodeWriter.Write(bm);

            //获取二维码实际尺寸,去掉二维码两边空白后的实际尺寸
            int[] rectangle = bm.getEnclosingRectangle();

            //计算插入图片的大小和位置
            int middleW = Math.Min((int)(rectangle[2] / 3), logo.Width);
            int middleH = Math.Min((int)(rectangle[3] / 3), logo.Height);
            int middleL = (map.Width - middleW) / 2;
            int middleT = (map.Height - middleH) / 2;

            Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
            using (Graphics g = Graphics.FromImage(bmpimg))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawImage(map, 0, 0, width, height);
                g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
                g.DrawImage(logo, middleL, middleT, middleW, middleH);
            }
            return bmpimg;
        }
    }

003.png

识别二维码

    class Program
    {
        static void Main(string[] args)
        {
            ReaderCode();
            Console.ReadKey();
        }

        /// <summary>
        /// 识别二维码/条码
        /// </summary>
        /// <param name="filename"></param>
        public static void ReaderCode(string fileName="2.png")
        {
            BarcodeReader reader = new BarcodeReader();
            //设置读取的格式(一般为UTF-8)  
            reader.Options.CharacterSet = "UTF-8";
            //加载二维码图片
            Bitmap p = new Bitmap("1.bmp");
            Result result = reader.Decode(p);
            //二维码内容
            var text = result.Text;
            Console.WriteLine(text);
        }
    }

004.png

1

评论 (0)

取消