C#利用zxing.net操作二维码和条形码
来源:互联网 发布:手机开淘宝店实名认证 编辑:程序博客网 时间:2024/06/06 03:53
下载地址:http://zxingnet.codeplex.com/
zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便。
首先下载二进制dll文件,引入工程;
代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using ZXing.QrCode;
- using ZXing;
- using ZXing.Common;
- using ZXing.Rendering;
- namespace zxingTest
- {
- public partial class Form1 : Form
- {
- EncodingOptions options = null;
- BarcodeWriter writer = null;
- public Form1()
- {
- InitializeComponent();
- options = new QrCodeEncodingOptions
- {
- DisableECI = true,
- CharacterSet = "UTF-8",
- Width = pictureBoxQr.Width,
- Height = pictureBoxQr.Height
- };
- writer = new BarcodeWriter();
- writer.Format = BarcodeFormat.QR_CODE;
- writer.Options = options;
- }
- private void buttonQr_Click(object sender, EventArgs e)
- {
- if (textBoxText.Text == string.Empty)
- {
- MessageBox.Show("输入内容不能为空!");
- return;
- }
- Bitmap bitmap = writer.Write(textBoxText.Text);
- pictureBoxQr.Image = bitmap;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using ZXing.QrCode;
- using ZXing;
- using ZXing.Common;
- using ZXing.Rendering;
- namespace zxingTest
- {
- public partial class Form1 : Form
- {
- EncodingOptions options = null;
- BarcodeWriter writer = null;
- public Form1()
- {
- InitializeComponent();
- options = new QrCodeEncodingOptions
- {
- DisableECI = true,
- CharacterSet = "UTF-8",
- Width = pictureBoxQr.Width,
- Height = pictureBoxQr.Height
- };
- writer = new BarcodeWriter();
- writer.Format = BarcodeFormat.QR_CODE;
- writer.Options = options;
- }
- private void buttonQr_Click(object sender, EventArgs e)
- {
- if (textBoxText.Text == string.Empty)
- {
- MessageBox.Show("输入内容不能为空!");
- return;
- }
- Bitmap bitmap = writer.Write(textBoxText.Text);
- pictureBoxQr.Image = bitmap;
- }
- }
- }
效果:
将字符编码时可以指定字符格式;默认为ISO-8859-1英文字符集,但一般移动设备常用UTF-8字符集编码,
可以通过QrCodeEncodingOptions设置编码方式。
如果要生成其他zxing支持的条形码,只要修改BarcodeWriter.Format就可以了。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using ZXing.QrCode;
- using ZXing;
- using ZXing.Common;
- using ZXing.Rendering;
- namespace zxingTest
- {
- public partial class Form1 : Form
- {
- EncodingOptions options = null;
- BarcodeWriter writer = null;
- public Form1()
- {
- InitializeComponent();
- options = new EncodingOptions
- {
- //DisableECI = true,
- //CharacterSet = "UTF-8",
- Width = pictureBoxQr.Width,
- Height = pictureBoxQr.Height
- };
- writer = new BarcodeWriter();
- writer.Format = BarcodeFormat.ITF;
- writer.Options = options;
- }
- private void buttonQr_Click(object sender, EventArgs e)
- {
- if (textBoxText.Text == string.Empty)
- {
- MessageBox.Show("输入内容不能为空!");
- return;
- }
- Bitmap bitmap = writer.Write(textBoxText.Text);
- pictureBoxQr.Image = bitmap;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using ZXing.QrCode;
- using ZXing;
- using ZXing.Common;
- using ZXing.Rendering;
- namespace zxingTest
- {
- public partial class Form1 : Form
- {
- EncodingOptions options = null;
- BarcodeWriter writer = null;
- public Form1()
- {
- InitializeComponent();
- options = new EncodingOptions
- {
- //DisableECI = true,
- //CharacterSet = "UTF-8",
- Width = pictureBoxQr.Width,
- Height = pictureBoxQr.Height
- };
- writer = new BarcodeWriter();
- writer.Format = BarcodeFormat.ITF;
- writer.Options = options;
- }
- private void buttonQr_Click(object sender, EventArgs e)
- {
- if (textBoxText.Text == string.Empty)
- {
- MessageBox.Show("输入内容不能为空!");
- return;
- }
- Bitmap bitmap = writer.Write(textBoxText.Text);
- pictureBoxQr.Image = bitmap;
- }
- }
- }
输入字符串需要符合编码的格式,不然会报错。
解码方式:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using ZXing.QrCode;
- using ZXing;
- using ZXing.Common;
- using ZXing.Rendering;
- namespace zxingTest
- {
- public partial class Form1 : Form
- {
- BarcodeReader reader = null;
- public Form1()
- {
- InitializeComponent();
- reader = new BarcodeReader();
- }
- private void Form1_DragEnter(object sender, DragEventArgs e)//当拖放进入窗体
- {
- if (e.Data.GetDataPresent(DataFormats.FileDrop))
- e.Effect = DragDropEffects.Copy; //显示拷贝效应
- else
- e.Effect = DragDropEffects.None;
- }
- private void Form1_DragDrop(object sender, DragEventArgs e) //当拖放放在窗体上
- {
- string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, false); //获取文件名
- if (fileNames.Length > 0)
- {
- pictureBoxPic.Load(fileNames[0]); //显示图片
- Result result = reader.Decode((Bitmap)pictureBoxPic.Image); //通过reader解码
- textBoxText.Text = result.Text; //显示解析结果
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using ZXing.QrCode;
- using ZXing;
- using ZXing.Common;
- using ZXing.Rendering;
- namespace zxingTest
- {
- public partial class Form1 : Form
- {
- BarcodeReader reader = null;
- public Form1()
- {
- InitializeComponent();
- reader = new BarcodeReader();
- }
- private void Form1_DragEnter(object sender, DragEventArgs e)//当拖放进入窗体
- {
- if (e.Data.GetDataPresent(DataFormats.FileDrop))
- e.Effect = DragDropEffects.Copy; //显示拷贝效应
- else
- e.Effect = DragDropEffects.None;
- }
- private void Form1_DragDrop(object sender, DragEventArgs e) //当拖放放在窗体上
- {
- string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, false); //获取文件名
- if (fileNames.Length > 0)
- {
- pictureBoxPic.Load(fileNames[0]); //显示图片
- Result result = reader.Decode((Bitmap)pictureBoxPic.Image); //通过reader解码
- textBoxText.Text = result.Text; //显示解析结果
- }
- }
- }
- }
0 0
- C#利用zxing.net操作二维码和条形码
- C#利用zxing.net操作二维码和条形码
- C#利用zxing.net操作二维码和条形码
- .NET C#利用ZXing生成、识别二维码/条形码
- C#利用zxing.net生成二维码和条形码并实现打印的功能
- C#利用Zxing.net生成条形码和二维码并实现打印的功能
- C#利用Zxing.net生成二维码和条形码并实现打印的功能
- ZXing 生成二维码和条形码
- 关于ZBar和ZXing二维码和条形码
- 用ZXing 生成条形码和二维码图片
- 使用ZXing扫描条形码和二维码
- ZXing和ZBar扫描二维码、扫描条形码
- ZXing二维码、条形码生成和扫描
- 使用zxing制作条形码和二维码
- C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)
- C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)
- C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)
- ZXing.Net生成二维码和解码二维码图像 C#
- 如何查看交换机的单板序号、插槽号、端口号??
- Cannot find interface declarartion for 'nsobject' & Unknow type name 'NSString'
- 即时通讯中XML文件解析专利研究
- 黑马程序员-IOS学习笔记 99乘法表 打印
- C# List去重的三种方法
- C#利用zxing.net操作二维码和条形码
- 密码发生器
- TCP 、UDP、IP包的最大长度是多少?
- 设置 Linux 文件和目录权限的方法
- 系统硬件、引导、开机流程 鸟哥笔记
- cocos2dx 命令解释
- poj2886--Who Gets the Most Candies?(线段树+反素数)
- 开博记念及附上第一个android工程:Txt文本阅读器
- 黑马程序员-IOS学习笔记 折半查找 在数组中插入一个数返回index