C#使用ZXing.Net實(shí)現(xiàn)生成二維碼和條碼
寫在前面
條碼生成是一個(gè)經(jīng)常需要處理的功能,本文介紹一個(gè)條碼處理類庫,ZXing用Java實(shí)現(xiàn)的多種格式的一維二維條碼圖像處理庫,而ZXing.Net是其.Net版本的實(shí)現(xiàn)。
在WinForm下使用該類庫需要從NuGet安裝兩個(gè)組件:
ZXing.Net

ZXing.Net.Bindings.Windows.Compatibility

代碼實(shí)現(xiàn)
using ZXing;
using ZXing.Common;
using ZXing.OneD;
using ZXing.QrCode;
using ZXing.Windows.Compatibility;
namespace QrCodeGen
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var writer = new BarcodeWriter<Bitmap>();
writer.Format = BarcodeFormat.QR_CODE;
QrCodeEncodingOptions options = new QrCodeEncodingOptions()
{
DisableECI = true, //設(shè)置內(nèi)容編碼
CharacterSet = "UTF-8", //設(shè)置二維碼的寬度和高度
Width = 200,
Height = 200,
Margin = 1 //設(shè)置二維碼的邊距,單位不是固定像素
};
var render = new AlternateBitmapRenderer();
writer.Renderer = render;
writer.Options = options;
Bitmap map = writer.Write("Hello world");
pictureBox1.Image = map;
}
private void button2_Click(object sender, EventArgs e)
{
var w = new EAN13Writer();
var render = new AlternateBitmapRenderer();
render.TextFont = new Font("Segoe UI", 25);
var content = "1234567890123";
// EAN-13 商品條碼的標(biāo)準(zhǔn)尺寸是 37.29mm x 26.26 mm ,按照 300 DPI 換算像素大小是 440 x 310
var m = w.encode(content, BarcodeFormat.EAN_13, 440, 310);
//渲染得到的圖片
var bmp = render.Render(m, BarcodeFormat.EAN_13, content, new EncodingOptions { Width = 440, Height = 310 });
pictureBox2.Image = bmp;
}
}
}EAN-13商品條碼是表示EAN/UCC-13商品標(biāo)識(shí)代碼的條碼符號(hào),由左側(cè)空白區(qū)、起始符、左側(cè)數(shù)據(jù)符、中間分隔符、右側(cè)數(shù)據(jù)符、校驗(yàn)符、終止符、右側(cè)空白區(qū)及供人識(shí)別字符組成。
調(diào)用示例

到此這篇關(guān)于C#使用ZXing.Net實(shí)現(xiàn)生成二維碼和條碼的文章就介紹到這了,更多相關(guān)C#生成二維碼條碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c# 從內(nèi)存中釋放Selenium chromedriver.exe
這篇文章主要介紹了c# 從內(nèi)存中釋放Selenium chromedriver.exe的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01
Winform下實(shí)現(xiàn)圖片切換特效的方法
這篇文章主要介紹了Winform下實(shí)現(xiàn)圖片切換特效的方法,包括百葉窗、淡入、旋轉(zhuǎn)等多種效果,需要的朋友可以參考下2014-08-08
10個(gè)C#程序員經(jīng)常用到的實(shí)用代碼片段
如果你是一個(gè)C#程序員,那么本文介紹的10個(gè)C#常用代碼片段一定會(huì)給你帶來幫助,從底層的資源操作,到上層的UI應(yīng)用,這些代碼也許能給你的開發(fā)節(jié)省不少時(shí)間。以下是原文:2015-09-09
C#中序列化實(shí)現(xiàn)深拷貝,實(shí)現(xiàn)DataGridView初始化刷新的方法
下面小編就為大家?guī)硪黄狢#中序列化實(shí)現(xiàn)深拷貝,實(shí)現(xiàn)DataGridView初始化刷新的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
C#實(shí)現(xiàn)在應(yīng)用程序間發(fā)送消息的方法示例
這篇文章主要介紹了C#實(shí)現(xiàn)在應(yīng)用程序間發(fā)送消息的方法,結(jié)合具體實(shí)例形式分析了C#實(shí)現(xiàn)項(xiàng)目之間信息發(fā)送、接收等交互操作相關(guān)技巧,需要的朋友可以參考下2017-06-06

