您的位置:首页 > 编程语言 > C#

ZXing生成和读取条形码二维码(C#)

2017-01-10 15:20 1311 查看
程序下载:点击打开链接
http://download.csdn.net/detail/softimite_zifeng/9733790
1. 条形码生成:

//设置条形码规格
EncodingOptions encodeOption = new EncodingOptions();
//设置宽和高
encodeOption.Height = 130;
encodeOption.Width = 240;
BarcodeWriter wr = new BarcodeWriter();
wr.Options = encodeOption;
//条形码:根据自己的需要选择条形码格式
wr.Format = BarcodeFormat.EAN_13;
//生成条形码
Bitmap image = wr.Write(textBox1.Text);
//显示
pictureBox1.Image = image;


2. 条形码读取:

DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.EAN_13 };
//读取条形码
BarcodeReader br = new BarcodeReader();
br.Options = decodeOption;
Result result = br.Decode(pictureBox2.Image as Bitmap);
if (result == null)
{
MessageBox.Show("读取失败");
}
else
{
//读取成功
textBox3.Text = result.Text;
}



3. 二维码生成:

//设置QR二维码的规格
QrCodeEncodingOptions qrEncodeOption = new QrCodeEncodingOptions();
//设置编码格式,否则中文乱码
qrEncodeOption.CharacterSet = "UTF-8";
//设置宽和高
qrEncodeOption.Height = 200;
qrEncodeOption.Width = 200;
//设置周围空白边距
qrEncodeOption.Margin = 1;
BarcodeWriter wr = new BarcodeWriter();
//二维码
wr.Format = BarcodeFormat.QR_CODE;
wr.Options = qrEncodeOption;
//生成二维码
Bitmap image = wr.Write(textBox4.Text);
//显示
pictureBox3.Image = image;


4. 二维码读取:

DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE };
//读取二维码
BarcodeReader br = new BarcodeReader();
br.Options = decodeOption;
Result result = br.Decode(pictureBox4.Image as Bitmap);
if (result == null)
{
MessageBox.Show("读取失败");
}
else
{
//读取成功
textBox7.Text = result.Text;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息