C# WinForm生成二維碼,一維碼,條形碼 操作


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 

 

如不夠詳細請評論下面


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM