Detailed examples of the use of the ZXing (C #)

Introduction

ZXing is an open source, 1D / 2D barcode image processing library implemented in Java in a variety of formats, and it contains links to other languages ​​ports. Zxing can be achieved using the built-in camera phone to complete the scan and decode barcodes. (Quoted from Baidu Encyclopedia)

use

Generating a one-dimensional code, two-dimensional code, to support a variety of formats (for example: Datamatrix, QR, Code39, etc.)
parsing one-dimensional code, two-dimensional code, to support a variety of formats (for example: Datamatrix, QR, Code39, etc.)

Source

Storehouse

 /// <summary>
        /// 解码二维码
        /// </summary>
        /// <param name="barcodeBitmap">待解码的二维码图片</param>
        /// <returns>扫码结果</returns>
        public static string DecodeQrCode(Bitmap barcodeBitmap)
        {
            BarcodeReader reader = new BarcodeReader();
            reader.Options.CharacterSet = "UTF-8";
            var result = reader.Decode(barcodeBitmap);
            return (result == null) ? null : result.Text;
        }
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static Bitmap Generate2DBarcode(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);
            return map;
        }

Examples

private void Btn_Create2Dbarcode_Click(object sender, EventArgs e)
        {
            BarcodePicture.Image = null;
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start(); //  开始监视代码
             Bitmap bmap=BarcodeHelper.Generate2DBarcode(CreateBarcode.Text, 100, 100);
            stopwatch.Stop(); //  停止监视
            BarcodePicture.Image = bmap;
            TimeSpan timeSpan = stopwatch.Elapsed; //  获取总时间
            double milliseconds = timeSpan.TotalMilliseconds;  //  毫秒数 
            CreateTime.Text = "Identify Time:" + timeSpan.TotalMilliseconds + "ms";
            /* Bitmap bmp = BarcodeHelper.Generate2DBarcode("123456", 100, 100);
             //保存到磁盘文件
             bmp.Save("C:/1.bmp");
             bmp.Dispose();*/
        }

        private void Btn_Identify2Dbarcode_Click(object sender, EventArgs e)
        {
            log.Clear();
            log.AppendText("Identify Start:"+System.DateTime.Now.TimeOfDay.ToString()+"\n");
            uint a = timeGetTime();
            IdentifyBarcode.Text = BarcodeHelper.DecodeQrCode((Bitmap)BarcodePicture.Image);
            //IdentifyBarcode.Text = BarcodeHelper.DecodeQrCode(BarcodeHelper.Generate2DBarcode("123", 100, 100));
            uint b = timeGetTime();
            log.AppendText("Identify End:" + System.DateTime.Now.TimeOfDay.ToString() + "\n");
            IdentifyTime.Text = "Identify Time:" + (b - a).ToString() + "ms";
        }

running result

After testing, the first time in a generation and analytical procedures need to initialize (in my judgment) time will be slightly longer, but then very quickly, within a generation time of 5ms, 2ms resolved within the time, depending on the practical application .
The key point is that image.
Note: At present, only 2D QR barcode generating and parsing the sample code. You can refer to a subsequent project BarcodeHelper.cs

download link

My Gitee Download: https://gitee.com/PErobin/Barcode-ZXing.git
official Github Address: https://github.com/zxing/zxing

Reference blog

Introduction and method parameters of ZXing: https://www.jianshu.com/p/6607e69b1121
ZXing using full resolution, based on ZXing3.1: https://blog.csdn.net/dodod2012/article/details/51315112
this blog entry based on github provide introduction and use: https://www.cnblogs.com/hnsongbiao/p/9145285.html

Guess you like

Origin www.cnblogs.com/aqyl/p/11258827.html