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

程序下载:点击打开链接

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;
ZXing生成和读取条形码二维码(C#)_第1张图片

2. 条形码读取:

DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List() { 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;
}

ZXing生成和读取条形码二维码(C#)_第2张图片

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;
ZXing生成和读取条形码二维码(C#)_第3张图片

4. 二维码读取

DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List() { 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;
}
ZXing生成和读取条形码二维码(C#)_第4张图片

你可能感兴趣的:(程序)