赞
踩
先看看效果
新建项目,在项目中引入zxing.dll
生成二维码的代码很简单,如下
- Image image = new BarcodeWriter()
- {
- Format = BarcodeFormat.QR_CODE,
- Options = new QrCodeEncodingOptions
- {
- DisableECI = true,
- CharacterSet = "UTF-8",
- Width = 200,
- Height = 200,
- Margin = 0
- }
- }.Write("123");
就这么简单
Format 表示生成条码的类型,可以生成二维码,一维码(条形码)
BarcodeFormat.QR_CODE 表示二维码
BarcodeFormat.CODE_128 表示条形码
Options 可以设置条码的编码,大小,边距
首先,条码的格式有很多种,一起来探索一下
在窗体加载时,我们将BarcodeFormat的所有数据绑定在ComboBox上
- private void Form1_Load(object sender, EventArgs e)
- {
- Array array = Enum.GetValues(typeof(BarcodeFormat));
-
- Dictionary<BarcodeFormat, string> bFs = new Dictionary<BarcodeFormat, string>();
-
- foreach (var item in array)
- {
- bFs.Add((BarcodeFormat)item, item.ToString());
- }
-
- cmbBarcodeFormat.DataSource = new BindingSource() { DataSource = bFs };
-
- cmbBarcodeFormat.ValueMember = "Key";
- cmbBarcodeFormat.DisplayMember = "Value";
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
点击生成按钮时,生成条码显示在PictureBox上
-
- private void btnGenerate_Click(object sender, EventArgs e)
- {
- if (!int.TryParse(tbWidth.Text, out int width))
- {
- MessageBox.Show("请输入正确整数", "提示");
- return;
- }
-
- if (!int.TryParse(tbHeight.Text, out int height))
- {
- MessageBox.Show("请输入正确整数", "提示");
- return;
- }
-
- if (tbContent.Text == string.Empty)
- {
- MessageBox.Show("请输入内容", "提示");
- return;
- }
-
- picBarcode.Width = width;
- picBarcode.Height = height;
-
- BarcodeWriter barcodeWriter = new BarcodeWriter()
- {
- Format = cbSenior.Checked ? (BarcodeFormat)cmbBarcodeFormat.SelectedValue :
- (rbQRCode.Checked ? BarcodeFormat.QR_CODE : BarcodeFormat.CODE_128),
-
- Options = new QrCodeEncodingOptions
- {
- DisableECI = true,
- CharacterSet = "UTF-8",
- Width = width,
- Height = height,
- Margin = 0
- }
- };
-
- try
- {
- picBarcode.Image = barcodeWriter.Write(tbContent.Text);
- }
- catch (Exception ex)
- {
- MessageBox.Show("生成失败\r\n" + ex.Message, "提示");
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
勾选高级时,可选所有条码类型,但是测试过程中,部分类型会生成失败,自己探索吧
- private void cbSenior_CheckedChanged(object sender, EventArgs e)
- {
- bool b = cbSenior.Checked;
-
- rbQRCode.Enabled = !b;
- rbCode.Enabled = !b;
-
- lbSeniorInfo.Visible = b;
- lbBarcodeFormat.Visible = b;
- cmbBarcodeFormat.Visible = b;
- }
这个只能输入7个或8个数字
这个只能输入12个或13个数字
这个只能输入偶位数
这个只能输入11个或12个数字
点击保存时,用DrawToBitmap将图片保存到所选目录
- private void btnSave_Click(object sender, EventArgs e)
- {
- try
- {
- Control control = picBarcode;
- Bitmap bitmap = new Bitmap(control.Width, control.Height);
- control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height));
-
- SaveFileDialog saveFileDialog = new SaveFileDialog()
- {
- Filter = "图片(*.png)|*.png|图片(*.jpg)|*.jpg|所有文件|*.*",
- FileName = "barcode",
- RestoreDirectory = true
- };
-
- if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
- {
- string fileName = saveFileDialog.FileName;
- bitmap.Save(fileName);
-
- string path = fileName.Substring(0, fileName.LastIndexOf("\\"));
- System.Diagnostics.Process.Start(path);//打开文件夹
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("保存失败\r\n" + ex.Message, "提示");
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
保存二维码
我们可以将图片拖进软件进行识别
首先设置Form的AllowDrop为True,再添加DragEnter和DragDrop两个事件
- BarcodeReader barcodeReader = new BarcodeReader();
-
- private void FrmBarcode_DragEnter(object sender, DragEventArgs e)
- {
- e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Link : DragDropEffects.None;
- }
-
- private void FrmBarcode_DragDrop(object sender, DragEventArgs e)
- {
- try
- {
- string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
- picBarcode.Load(path);
-
- Result result = barcodeReader.Decode((Bitmap)picBarcode.Image);
- tbContent.Text = result?.Text;
- }
- catch(Exception ex)
- {
- MessageBox.Show("解析失败\r\n" + ex.Message, "提示");
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
将图片拖到软件中,即可识别
如果不是条码,则识别为空
现在在拖入刚才保存下来的条码
识别成功
资源地址:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。