一维码、二维码生成与识别,获得二维码位置

配置环境:VS2012+Zxing0.14.0.0

Zxing链接地址:https://download.youkuaiyun.com/download/kissgoodbye2012/10275837

一、一维码生成(barcode)

            // 1.设置条形码规格
            EncodingOptions encodeOption = new EncodingOptions();
            encodeOption.Height = 130; // 必须制定高度、宽度
            encodeOption.Width = 240;

            // 2.生成条形码图片并保存
            ZXing.BarcodeWriter wr = new BarcodeWriter();
            wr.Options = encodeOption;
            wr.Format = BarcodeFormat.EAN_13; //  条形码规格:EAN13规格:12(无校验位)或13位数字
            Bitmap img = wr.Write(this.ContentTxt.Text); // 生成图片
            string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\EAN_13-" + this.ContentTxt.Text + ".jpg";
            img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

            // 3.读取保存的图片
            this.ImgPathTxt.Text = filePath;
            this.barCodeImg.Image = img;
            MessageBox.Show("保存成功:" + filePath);

EncodingOptions包含在using ZXing.Common;

ContentTxt为一个输入文本框。

这个一维码代码将文本框的输入信息转换为一维码。

条形码中有很多码制,这里是EAN13。对我的应用来说,掌握一种就够了,反正是自己定义,自己识别。

只能生成数字的一维码图片。不能是汉字和字母。

二、条形码的识别

            // 1.设置读取条形码规格
            DecodingOptions decodeOption = new DecodingOptions();
            decodeOption.PossibleFormats = new List<BarcodeFormat>() { 
               BarcodeFormat.EAN_13,
            };

            // 2.进行读取操作
            ZXing.BarcodeReader br = new BarcodeReader();
            br.Options = decodeOption;
            ZXing.Result rs = br.Decode(this.barCodeImg.Image as Bitmap);
            if (rs == null)
            {
                this.ContentTxt.Text = "读取失败";
                MessageBox.Show("读取失败");
            }
            else
            {
                this.ContentTxt.Text = rs.Text;
                MessageBox.Show("读取成功,内容:" + rs.Text);
            }

DecodingOptions包含在using ZXing.Common;

对于怎么得到一维码在图片中的位置,后续再探索。(err)

三、二维码的生成(Qrcode)

            // 1.设置QR二维码的规格
            ZXing.QrCode.QrCodeEncodingOptions qrEncodeOption = new ZXing.QrCode.QrCodeEncodingOptions();
            qrEncodeOption.CharacterSet = "UTF-8"; // 设置编码格式,否则读取'中文'乱码
            qrEncodeOption.Height = 200;
            qrEncodeOption.Width = 200;
            qrEncodeOption.Margin = 1; // 设置周围空白边距

            // 2.生成条形码图片并保存
            ZXing.BarcodeWriter wr = new BarcodeWriter();
            wr.Format = BarcodeFormat.QR_CODE; // 二维码
            wr.Options = qrEncodeOption;
            Bitmap img = wr.Write(this.ContentTxt.Text);
            string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\QR-" + this.ContentTxt.Text + ".jpg";
            img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

            // 3.读取保存的图片
            this.ImgPathTxt.Text = filePath;
            this.barCodeImg.Image = img;
            MessageBox.Show("保存成功:" + filePath);

QrCodeEncodingOptions包含于ZXing.QrCode

四、二维码的识别

            // 1.设置读取二维码规格
            DecodingOptions decodeOption = new DecodingOptions();
            decodeOption.PossibleFormats = new List<BarcodeFormat>() { 
               BarcodeFormat.QR_CODE,
           };

            // 2.进行读取操作
            ZXing.BarcodeReader br = new BarcodeReader();
            br.Options = decodeOption;
            ZXing.Result rs = br.Decode(this.barCodeImg.Image as Bitmap);
            if (rs == null)
            {
                this.ContentTxt.Text = "读取失败";
                MessageBox.Show("读取失败");
            }
            else
            {
                this.ContentTxt.Text = rs.Text;
                MessageBox.Show("读取成功,内容:" + rs.Text);
            }

五、获取二维码位置

            ZXing.Result rs = br.Decode(this.barCodeImg.Image as Bitmap);
            
            textBox1.Text = rs.ResultPoints.GetValue(0).ToString();
            textBox2.Text = rs.ResultPoints.GetValue(1).ToString();
            textBox3.Text = rs.ResultPoints.GetValue(2).ToString();

textBox1等为输入框的显示;

三个得到的是二维码上三个点。

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kissgoodbye2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值