C#利用ZXing.Net生成條形碼和二維碼
本文是利用ZXing.Net在WinForm中生成條形碼,二維碼的小例子,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正。
什么是ZXing.Net?
ZXing是一個(gè)開(kāi)放源碼的,用Java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫(kù),它包含了聯(lián)系到其他語(yǔ)言的端口。而ZXing.Net是ZXing的端口之一。
在工程中引用ZXing.Net
在項(xiàng)目中,點(diǎn)擊項(xiàng)目名稱右鍵-->管理NuGet程序包,打開(kāi)NuGet包管理器窗口,進(jìn)行搜索下載即可,如下圖所示:

ZXing.Net關(guān)鍵類結(jié)構(gòu)圖
包括Reader【識(shí)別圖片中的條形碼和二維碼】) 和Writer【生成條形碼和二維碼到圖片中】?jī)刹糠?,如下圖所示:

涉及知識(shí)點(diǎn):
BarcodeWriter 用于生成圖片格式的條碼類,通過(guò)Write函數(shù)進(jìn)行輸出。繼承關(guān)系如上圖所示。
BarcodeFormat 枚舉類型,條碼格式
QrCodeEncodingOptions 二維碼設(shè)置選項(xiàng),繼承于EncodingOptions,主要設(shè)置寬,高,編碼方式等信息。
MultiFormatWriter 復(fù)合格式條碼寫(xiě)碼器,通過(guò)encode方法得到BitMatrix。
BitMatrix 表示按位表示的二維矩陣數(shù)組,元素的值用true和false表示二進(jìn)制中的1和0。
示例效果圖

關(guān)鍵代碼
如下所示,包含一維條碼,二維條碼,和帶logo的條碼
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.QrCode.Internal;
namespace DemoQrCode
{
/// <summary>
/// 描述:條形碼和二維碼幫助類
/// 時(shí)間:2018-02-18
/// </summary>
public class BarcodeHelper
{
/// <summary>
/// 生成二維碼
/// </summary>
/// <param name="text">內(nèi)容</param>
/// <param name="width">寬度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static Bitmap Generate1(string text,int width,int height)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
QrCodeEncodingOptions options = new QrCodeEncodingOptions()
{
DisableECI = true,//設(shè)置內(nèi)容編碼
CharacterSet = "UTF-8", //設(shè)置二維碼的寬度和高度
Width = width,
Height = height,
Margin = 1//設(shè)置二維碼的邊距,單位不是固定像素
};
writer.Options = options;
Bitmap map = writer.Write(text);
return map;
}
/// <summary>
/// 生成一維條形碼
/// </summary>
/// <param name="text">內(nèi)容</param>
/// <param name="width">寬度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static Bitmap Generate2(string text,int width,int height)
{
BarcodeWriter writer = new BarcodeWriter();
//使用ITF 格式,不能被現(xiàn)在常用的支付寶、微信掃出來(lái)
//如果想生成可識(shí)別的可以使用 CODE_128 格式
//writer.Format = BarcodeFormat.ITF;
writer.Format = BarcodeFormat.CODE_39;
EncodingOptions options = new EncodingOptions()
{
Width = width,
Height = height,
Margin = 2
};
writer.Options = options;
Bitmap map = writer.Write(text);
return map;
}
/// <summary>
/// 生成帶Logo的二維碼
/// </summary>
/// <param name="text">內(nèi)容</param>
/// <param name="width">寬度</param>
/// <param name="height">高度</param>
public static Bitmap Generate3(string text, int width, int height)
{
//Logo 圖片
string logoPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\img\logo.png";
Bitmap logo = new Bitmap(logoPath);
//構(gòu)造二維碼寫(xiě)碼器
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);
//hint.Add(EncodeHintType.MARGIN, 2);//舊版本不起作用,需要手動(dòng)去除白邊
//生成二維碼
BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width+30, height+30, hint);
bm = deleteWhite(bm);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap map = barcodeWriter.Write(bm);
//獲取二維碼實(shí)際尺寸(去掉二維碼兩邊空白后的實(shí)際尺寸)
int[] rectangle = bm.getEnclosingRectangle();
//計(jì)算插入圖片的大小和位置
int middleW = Math.Min((int)(rectangle[2] / 3), logo.Width);
int middleH = Math.Min((int)(rectangle[3] / 3), 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);
//白底將二維碼插入圖片
g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
g.DrawImage(logo, middleL, middleT, middleW, middleH);
}
return bmpimg;
}
/// <summary>
/// 刪除默認(rèn)對(duì)應(yīng)的空白
/// </summary>
/// <param name="matrix"></param>
/// <returns></returns>
private static BitMatrix deleteWhite(BitMatrix matrix)
{
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++)
{
for (int j = 0; j < resHeight; j++)
{
if (matrix[i + rec[0], j + rec[1]])
resMatrix[i, j]=true;
}
}
return resMatrix;
}
}
}
關(guān)于生成條形碼和二維碼的方式有很多,條碼的種類也有很多種,每一種都有其對(duì)應(yīng)的應(yīng)用領(lǐng)域。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- c# 生成二維碼的示例
- C#如何用ThoughtWorks生成二維碼
- C# 根據(jù)字符串生成二維碼的實(shí)例代碼
- C#實(shí)現(xiàn)掃描槍掃描二維碼并打印(實(shí)例代碼)
- C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能示例
- C#生成帶二維碼的專屬微信公眾號(hào)推廣海報(bào)實(shí)例代碼
- C#二維碼圖片識(shí)別代碼
- C#生成帶logo的二維碼
- C#生成二維碼的方法
- .NET C#利用ZXing生成、識(shí)別二維碼/條形碼
- C#利用QrCode.Net生成二維碼(Qr碼)的方法
- C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片方法介紹
相關(guān)文章
js驗(yàn)證電話號(hào)碼手機(jī)號(hào)碼的正則表達(dá)式
本篇文章主要是對(duì)js驗(yàn)證電話號(hào)碼手機(jī)號(hào)碼的正則表達(dá)式進(jìn)行了介紹。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
C#中一些你可能沒(méi)用過(guò)的調(diào)試窗口的方法
其他窗口比較常用,就不介紹了,是不是有一些你沒(méi)用到的窗口呢?2013-05-05
Unity圖形學(xué)之ShaderLab入門基礎(chǔ)
Unity中所有Shader文件都通過(guò)一種陳述性語(yǔ)言進(jìn)行描述,稱為“ShaderLab”, 這篇文章主要介紹了Unity圖形學(xué)之ShaderLab入門基礎(chǔ),需要的朋友可以參考下2022-01-01
解析C#彩色圖像灰度化算法的實(shí)現(xiàn)代碼詳解
本篇文章是對(duì)C#中彩色圖像灰度化算法的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Unity?UGUI的RawImage原始圖片組件使用示例詳解
這篇文章主要為大家介紹了Unity?UGUI的RawImage原始圖片組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

