一、前言 ZXing.Net的一個可移植軟體包,是一個開源的、多格式的1D/2D條形碼圖像處理庫,最初是用Java實現的。已經過大量優化和改進,它已經被手動移植。它與.Net 2.0、.Net 3.5、.Net 4.x、.Net 5.x、.Net 6.x、.Net 7.x、Windows RT類庫和 ...
一、前言
ZXing.Net的一個可移植軟體包,是一個開源的、多格式的1D/2D條形碼圖像處理庫,最初是用Java實現的。已經過大量優化和改進,它已經被手動移植。它與.Net 2.0、.Net 3.5、.Net 4.x、.Net 5.x、.Net 6.x、.Net 7.x、Windows RT類庫和組件、UWP、.Net Standard 1.x和2.0x、.Net Core App 3.x、Silverlight 4、Silverlight 5、Windows Phone 7.x和Windows Phone 8.x以及Xamarin.Android相容。
二、項目環境和搭建
項目框架:.NET Framework 4.6.1
項目依賴:ZXing.Net
項目結構和界面設計:
三、實現核心代碼
ZXing幫助類
/// <summary>
/// ZXing.NET 二維碼幫助類
/// </summary>
public class ZXingHelper
{
/// <summary>
/// 站點二維碼的目錄
/// </summary>
private static string QRCodeDirectory = "QRCode";
/// <summary>
/// 使用zxing動態庫生成二維碼
/// </summary>
/// <param name="conetnt">二維碼內容</param>
/// <param name="errorMessage">異常信息</param>
/// <param name="logoPath">logo圖片路徑,預設為空。為空時生成的二維碼不帶logo</param>
/// <param name="height">二維碼圖片高度,預設240 單位 pixels</param>
/// <param name="width">二維碼圖片寬度,預設240 單位 pixels</param>
/// <param name="margin">二維碼圖片邊距,預設為0</param>
/// <returns></returns>
public static Bitmap GenerateQRCode(string conetnt, out string errorMessage, string logoPath = "", int height = 240, int width = 240, int margin = 0)
{
errorMessage = string.Empty;
try
{
BarcodeWriter barCodeWriter = new BarcodeWriter();
barCodeWriter.Format = BarcodeFormat.QR_CODE;
barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
barCodeWriter.Options.Height = height;
barCodeWriter.Options.Width = width;
barCodeWriter.Options.Margin = margin;
BitMatrix bm = barCodeWriter.Encode(conetnt);
Bitmap qrCodeImage = barCodeWriter.Write(bm);
if (!string.IsNullOrEmpty(logoPath))
{
// 添加Logo
Bitmap logo = new Bitmap(logoPath);
int logoSize = (int)(qrCodeImage.Width * 0.2); // Logo大小為二維碼大小的1/5
int logoX = (qrCodeImage.Width - logoSize) / 2;
int logoY = (qrCodeImage.Height - logoSize) / 2;
Graphics qrGraphics = Graphics.FromImage(qrCodeImage);
qrGraphics.DrawImage(logo, new Rectangle(logoX, logoY, logoSize, logoSize));
}
return qrCodeImage;
}
catch (Exception ex)
{
errorMessage = $"Exception raised when generating QRCode,, Exception;{ex}";
return null;
}
}
/// <summary>
/// 使用zxing動態庫解析:二維碼,條形碼......
/// </summary>
/// <param name="image">二維碼圖像</param>
/// <returns></returns>
public static string ParseBarCode(Bitmap image)
{
BarcodeReader reader = new BarcodeReader();
Result result = reader.Decode(image);
return result.Text;
}
/// <summary>
/// 使用zxing動態庫生成二維碼
/// </summary>
/// <param name="conetnt">二維碼內容</param>
/// <param name="logoPath">logo圖片路徑,預設為空。為空時生成的二維碼不帶logo</param>
/// <param name="height">二維碼圖片高度,預設240 單位 pixels</param>
/// <param name="width">二維碼圖片寬度,預設240 單位 pixels</param>
/// <param name="margin">二維碼圖片邊距,預設為0</param>
/// <returns></returns>
public static void GenerateQRCode(string conetnt, string logoPath = "", int height = 240, int width = 240, int margin = 0)
{
try
{
BarcodeWriter barCodeWriter = new BarcodeWriter();
barCodeWriter.Format = BarcodeFormat.QR_CODE;
barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
barCodeWriter.Options.Height = height;
barCodeWriter.Options.Width = width;
barCodeWriter.Options.Margin = margin;
BitMatrix bm = barCodeWriter.Encode(conetnt);
Bitmap qrCodeImage = barCodeWriter.Write(bm);
if (!string.IsNullOrEmpty(logoPath))
{
// 添加Logo
Bitmap logo = new Bitmap(logoPath);
int logoSize = (int)(qrCodeImage.Width * 0.2); // Logo大小為二維碼大小的1/5
int logoX = (qrCodeImage.Width - logoSize) / 2;
int logoY = (qrCodeImage.Height - logoSize) / 2;
Graphics qrGraphics = Graphics.FromImage(qrCodeImage);
qrGraphics.DrawImage(logo, new Rectangle(logoX, logoY, logoSize, logoSize));
}
string qrCodeFile = AppDomain.CurrentDomain.BaseDirectory + QRCodeDirectory + "/" + "qrcode.jpg";
if (File.Exists(qrCodeFile))
{
File.Delete(qrCodeFile);
}
qrCodeImage.Save(qrCodeFile, System.Drawing.Imaging.ImageFormat.Jpeg);//保存二維碼圖片
}
catch (Exception ex)
{
}
}
}
選擇logo
/// <summary>
/// 選擇logo
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_selectlogo_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//初始路徑為桌面
openFileDialog.Filter = "Logo圖片|*.png;*.jpg;*.jpeg;*.ico";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
picLogo.Image = Image.FromFile(openFileDialog.FileName);
logPath = openFileDialog.FileName;//logo文件路徑
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
生成二維碼
/// <summary>
/// 生成二維碼
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bt_generate_Click(object sender, EventArgs e)
{
try
{
string errorMsg = "";
string content = rtbQRCodeContent.Text;
if (string.IsNullOrWhiteSpace(content))
{
MessageBox.Show("二維碼內容不能為空!");
return;
}
picQRCode.Image = ZXingHelper.GenerateQRCode(content, out errorMsg, logPath);
if (!string.IsNullOrWhiteSpace(errorMsg))
{
MessageBox.Show(errorMsg);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
識別二維碼
/// <summary>
/// 識別二維碼
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_identityQrCode_Click(object sender, EventArgs e)
{
try
{
if (picQRCode.Image == null)
{
MessageBox.Show("請先生成二維碼!");
return;
}
Bitmap Imagemap = new Bitmap(picQRCode.Image);
string QRCodeResult = ZXingHelper.ParseBarCode(Imagemap);
rtbQRCodeResult.Text = QRCodeResult;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
保存二維碼
/// <summary>
/// 保存二維碼
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_save_Click(object sender, EventArgs e)
{
try
{
if (picQRCode.Image == null)
{
MessageBox.Show("請先生成二維碼!");
return;
}
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "保存圖片|*.png;*.jpg;*.jpeg";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
picQRCode.Image.Save(saveFileDialog.FileName, ImageFormat.Png);
MessageBox.Show("保存成功!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
四、實現演示
五、源碼工具獲取
關註公眾號,後臺回覆關鍵字:二維碼工具
本文來自博客園,作者:碼農阿亮,轉載請註明原文鏈接:https://www.cnblogs.com/wml-it/p/17761875.html
技術的發展日新月異,隨著時間推移,無法保證本博客所有內容的正確性。如有誤導,請大家見諒,歡迎評論區指正!
開源庫地址,歡迎點亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群聲明: 本著技術在於分享,方便大家交流學習的初心,特此建立【編程內功修煉交流群】,為大家答疑解惑。熱烈歡迎各位愛交流學習的程式員進群,也希望進群的大佬能不吝分享自己遇到的技術問題和學習心得!進群方式:掃碼關註公眾號,後臺回覆【進群】。