-Begin-
今天給大家分享一下,如何基于C#來生成并識別條形碼或者二維碼。
實現二維碼生成的庫有很多,我們這里采用的是ZXing.Net。
ZXing是一個開放源碼的,用Java實現的多種格式的一維二維條碼圖像處理庫,而ZXing.Net是ZXing在.Net平臺下的實現。
我們通過Nuget搜索zxing.net即可搜索安裝。
安裝好ZXing.Net后,我們創建一個BarCodeHelper,對于進行封裝,封裝了以下幾個方法:
方法一:生成條形碼
public static Bitmap GenerateBarCode(string text, int width, int height)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.CODE_39;
EncodingOptions options = new EncodingOptions()
{
Width = width,
Height = height,
Margin = 2,
PureBarcode = true
};
writer.Options = options;
Bitmap map = writer.Write(text);
return map;
}
方法二:生成二維碼
public static Bitmap GenerateQRCode(string text, int width, int height)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
QrCodeEncodingOptions options = new QrCodeEncodingOptions()
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = width,
Height = height,
Margin = 1
};
writer.Options = options;
Bitmap map = writer.Write(text);
return map;
}
方法三:生成帶Logo二維碼:
public static Bitmap GenerateQRCodeWithLogo(string text, int width, int height, Bitmap logo)
{
MultiFormatWriter writer = new MultiFormatWriter();
Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hint);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap map = barcodeWriter.Write(bm);
int[] rectangle = bm.getEnclosingRectangle();
int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height);
int middleL = (map.Width - middleW) / 2;
int middleT = (map.Height - middleH) / 2;
Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmpimg))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(map, 0, 0, width, height);
}
Graphics myGraphic = Graphics.FromImage(bmpimg);
myGraphic.FillRectangle(Brushes.Transparent, middleL, middleT, middleW, middleH);
myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH);
return bmpimg;
}
方法四:識別條碼二維碼:
public static string ReadCode(Bitmap bitmap)
{
BarcodeReader barcodeReader = new BarcodeReader();
barcodeReader.Options.CharacterSet = "UTF-8";
Result result = barcodeReader.Decode(bitmap);
return result.Text;
}
有了以上方法后,編寫一個案例來進行功能測試。
按鈕事件代碼很簡單,就是調用BarCodeHelper中的對應方法即可。
private void btn_BarCode_Click(object sender, EventArgs e)
{
this.pic_BarCode.Image = BarCodeHelper.GenerateBarCode(this.txt_BarCode.Text, this.pic_BarCode.Width, this.pic_BarCode.Height);
}
private void btn_QRCode_Click(object sender, EventArgs e)
{
if (chk_Logo.Checked)
{
this.pic_QRCode.Image = BarCodeHelper.GenerateQRCodeWithLogo(this.txt_QRCode.Text,
this.pic_QRCode.Width, this.pic_QRCode.Height, new Bitmap(this.pic_Logo.Image));
}
else
{
this.pic_QRCode.Image = BarCodeHelper.GenerateQRCode(this.txt_QRCode.Text, this.pic_QRCode.Width, this.pic_QRCode.Height);
}
}
private void btn_ReadQRCode_Click(object sender, EventArgs e)
{
MessageBox.Show(BarCodeHelper.ReadCode(new Bitmap(this.pic_QRCode.Image)));
}
private void btn_ReadBarCode_Click(object sender, EventArgs e)
{
MessageBox.Show(BarCodeHelper.ReadCode(new Bitmap(this.pic_BarCode.Image)));
}
最終實現效果如下所示:
-END-?
該文章在 2024/12/25 16:13:25 編輯過