码迷,mamicode.com
首页 > Windows程序 > 详细

C#条形码 WinForm生成读取

时间:2019-03-20 20:52:25      阅读:431      评论:0      收藏:0      [点我收藏+]

标签:4.0   1.3   code   main   版本支持   ase   代码   bit   window   

1.1 条形码

1.2 条形码分类

一维条形码:只是在一个方向(一般是水平方向)表达信息,而在垂直方向则不表达任何信息。

二维条形码:在水平和垂直方向的二维空间存储信息的条形码。

 

1.3 第三方类库:ZXing.Net

1.3.1 说明

ZXing 是一个可生成和读取 1D/2D(1维/2维) 条形码的开源类库。原先是Java版本,后由第三方衍生了支持QT、C++、.Net等版本。

.Net版本支持的平台:.Net 2.0, 3.5 and 4.0、Silverlight 4 and 5、Windows Phone 7.0, 7.1 and 8.0、Windows CE、Unity3D、Xamarin.Android 等等。

 在项目引用中的引用ZXing

技术图片

进行联网下载

技术图片

 

需要的引用

 

 技术图片

 

2. 一维码操作

如有看不懂下面下载源代码

2.1 介绍

一维条形码:只是在一个方向(一般是水平方向)表达信息,而在垂直方向则不表达任何信息。

例图:

技术图片

 

 

 

2.2 生成一维码

以生成EAN-13码制为例:

//生成一维码
private void button1_Click(object sender, EventArgs e)
{//设置条形码规格
EncodingOptions encoding = new EncodingOptions();
encoding.Height = 120;//设置宽高
encoding.Width = 200;
//生成条形码的图片并保存
BarcodeWriter wr = new BarcodeWriter();
wr.Options = encoding;//进行指定规格
wr.Format = BarcodeFormat.EAN_13;//条形码的规格 EAN13规格
Bitmap img = wr.Write(textBox1.Text);//生成图片
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\ENA_13" + this.textBox1.Text + ".jpg";
img.Save(filepath,System.Drawing.Imaging.ImageFormat.Jpeg);

//读取保存的图片
textBox2.Text = filepath;//设置路径
pictureBox1.Image = img;//存入图片
//弹出一维码图片的路径
MessageBox.Show("保存成功:" + filepath);
}

 技术图片

 

 

2.3 读取一维码

 

以读取EAN-13码制的图片为例:

private void button3_Click(object sender, EventArgs e)
{
//1设置读取条形码的规格
DecodingOptions decoding = new DecodingOptions();
decoding.PossibleFormats = new List<BarcodeFormat>()
{
BarcodeFormat.EAN_13
};//指定读取的格式

//2.进行读取操作
BarcodeReader br = new BarcodeReader();
br.Options = decoding;//指定规格
Result result = br.Decode(pictureBox1.Image as Bitmap);//进行读取条形码数字
if (result==null)
{
textBox1.Text = "读取失败";
MessageBox.Show("读取失败");
}
else
{
this.textBox1.Text = result.Text;
MessageBox.Show("读取成功,内容:" + result.Text);
}
}

技术图片

 

3. 二维码操作

如有看不懂下面下载源代码

3.1 介绍

二维码:在水平和垂直方向的二维空间存储信息的条形码。

例图:

技术图片

 

 

3.2 生成二维码

以生成QR码制为例:

//[生成二维码]
private void button2_Click(object sender, EventArgs e)
{
//1先设置二维码的规格
QrCodeEncodingOptions qr = new QrCodeEncodingOptions();
qr.CharacterSet = "UTF-8";//设置编码格式,否则会乱码
qr.Height = 200;
qr.Width = 200;
qr.Margin = 1;//设置二维码图片周围空白边距

//2生成条形码图片保存
BarcodeWriter wr = new BarcodeWriter();
wr.Format = BarcodeFormat.QR_CODE;//二维码
wr.Options = qr;//指定格式
Bitmap bitmap = wr.Write(textBox1.Text);//存放二维码
//设置图片的路径
string file = AppDomain.CurrentDomain.BaseDirectory + "\\QR-" + textBox1.Text + ".jpg";
//进行保存
bitmap.Save(file,ImageFormat.Jpeg);


//3读取保存的图片
textBox2.Text = file;
pictureBox1.Image = bitmap;
MessageBox.Show("保存成功,"+file);

}

技术图片

 

 

3.3 读取二维码

以读取QR码制的图片为例:

//[识别二维码]
private void button4_Click(object sender, EventArgs e)
{
//1设置读取条形码规格
DecodingOptions dr = new DecodingOptions();
dr.PossibleFormats = new List<BarcodeFormat>()
{
BarcodeFormat.QR_CODE
};

//2进行读取操作
BarcodeReader br = new BarcodeReader();
br.Options = dr;//指定规格
Result rs = br.Decode(pictureBox1.Image as Bitmap);
if (rs == null)
{
textBox1.Text = "读取失败";
MessageBox.Show("读取失败");
}
else
{

textBox1.Text = "读取成功";
MessageBox.Show("读取成功.内容为:"+rs.Text);
}
}

技术图片

 

 

3.4 生成带Logo的二维码

二维码带有校验功能,故可以在中间区域展示一定尺寸的图片。

例图:

 

 

技术图片

 

 

 

代码:

//[带图片的二维码]
private void button6_Click(object sender, EventArgs e)
{
//1设置QR二维码的规格
QrCodeEncodingOptions qr = new QrCodeEncodingOptions();
qr.CharacterSet = "UTF-8"; // 设置编码格式,否则读中文会乱码
qr.Height = 200;
qr.Width = 200;
qr.Margin = 1; // 设置二维码周围空白边距(可选)

//2生成条形码图片
ZXing.BarcodeWriter wr = new BarcodeWriter();
wr.Format = BarcodeFormat.QR_CODE; // 二维码
wr.Options = qr;
Bitmap img = wr.Write(this.textBox1.Text);


// 3.在二维码的Bitmap对象上绘制logo图片
Bitmap logoImg = Bitmap.FromFile(System.AppDomain.CurrentDomain.BaseDirectory + "\\logo.jpg") as Bitmap;
Graphics g = Graphics.FromImage(img);
Rectangle logoRec = new Rectangle(); // 设置logo图片的大小和绘制位置
logoRec.Width = img.Width / 6;
logoRec.Height = img.Height / 6;
logoRec.X = img.Width / 2 - logoRec.Width / 2; // 中心点
logoRec.Y = img.Height / 2 - logoRec.Height / 2;
g.DrawImage(logoImg, logoRec);

// 4.保存绘制后的图片
string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\QR-" + this.textBox1.Text + ".jpg";
img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

// 5.读取保存的图片
this.textBox2.Text = filePath;
this.pictureBox1.Image = img;
MessageBox.Show("保存成功:" + filePath);
}

技术图片

 

 

4. 打开图片

如有看不懂下面下载源代码

4.1 运行图

技术图片

技术图片

 

源代码下载地址

百度网盘:

链接:https://pan.baidu.com/s/1d21ZrtXx4QEE5cz4hk6xpw 
提取码:m601 

 

如不够详细请评论下面

C#条形码 WinForm生成读取

标签:4.0   1.3   code   main   版本支持   ase   代码   bit   window   

原文地址:https://www.cnblogs.com/guoxing1998/p/10567301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!