ZXing使用详解与范例(C#)

介绍

ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。(引自百度百科)

用途

生成一维码、二维码,支持各种格式(比如:Datamatrix、QR、Code39等)
解析一维码、二维码,支持各种格式(比如:Datamatrix、QR、Code39等)

源码

 /// <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;
        }

示例

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";
        }

运行效果

经过测试,在程序第一次生成和解析时,需要初始化(据我判断)时间会略长,但是之后速度很快,生成时间在5ms之内,解析时间在2ms之内,具体取决于实际应用。
最关键点在于图像。
备注:目前示例代码中只有2D QR条码的生成和解析。后续方法可以参考项目中的BarcodeHelper.cs

下载地址

我的Gitee下载地址:https://gitee.com/PErobin/Barcode-ZXing.git
官方Github地址:https://github.com/zxing/zxing

参考博客

ZXing的介绍和方法参数:https://www.jianshu.com/p/6607e69b1121
ZXing使用全解析,基于ZXing3.1:https://blog.csdn.net/dodod2012/article/details/51315112
该篇博客基于github提供介绍和使用:https://www.cnblogs.com/hnsongbiao/p/9145285.html

猜你喜欢

转载自www.cnblogs.com/aqyl/p/11258827.html