C# 使用Zxing元件(透過nuget下載)產生Qrcode圖片[CS_QrCode] (GOOGLE: qrcode with logo)
C# 使用Zxing元件(透過nuget下載)產生Qrcode圖片[CS_QrCode] (GOOGLE: qrcode with logo)
GITHUB:https://github.com/jash-git/CS_QrCode.git
結果圖:
CODE:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; //--- //QrCode using ZXing; using ZXing.QrCode; using ZXing.Rendering; //---QrCode //--- //參考資料 //https://www.cnblogs.com/chenwolong/p/erweima.html //https://dangerlover9403.pixnet.net/blog/post/199434147-%5B%E6%95%99%E5%AD%B8%5D-decode-encode-qrcode-(c%23-version) //https://dotblogs.com.tw/neil_coding/2019/10/25/qrcode //---參考資料 namespace CS_QrCode { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private Bitmap GetQRCodeByZXingNet(String strMessage, Int32 width, Int32 height) { String imagePath = System.Windows.Forms.Application.StartupPath + "\\ICON.png"; Bitmap result = null; try { BarcodeWriter barCodeWriter = new BarcodeWriter(); barCodeWriter.Format = BarcodeFormat.QR_CODE; //barcode格式 barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); //編碼字元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 = 0; //外邊距 ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage); //將訊息寫入 result = barCodeWriter.Write(bm); Bitmap overlay = new Bitmap(imagePath); //載入圖片 int deltaHeigth = result.Height - overlay.Height; //圖片y int deltaWidth = result.Width - overlay.Width; //圖片x Graphics g = Graphics.FromImage(result); //圖型 g.DrawImage(overlay, new Point(deltaWidth / 2, deltaHeigth / 2)); //畫出圖片 } catch (Exception ex) { MessageBox.Show(ex.ToString()); } return result; } private void Form1_Load(object sender, EventArgs e) { //--- //單純QR Bitmap bitmap = null; string strQrCodeContent = "http://jashliao.eu/wordpress/"; ZXing.BarcodeWriter writer = new ZXing.BarcodeWriter { Format = ZXing.BarcodeFormat.QR_CODE, Options = new ZXing.QrCode.QrCodeEncodingOptions { //Create Photo Height = 200, Width = 200, CharacterSet = "UTF-8", //錯誤修正容量 //L水平 7%的字碼可被修正 //M水平 15%的字碼可被修正 //Q水平 25%的字碼可被修正 //H水平 30%的字碼可被修正 ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H } }; bitmap = writer.Write(strQrCodeContent);//Create Qr-code , use input string //--- //Storage bitmpa string strDir; strDir = System.Windows.Forms.Application.StartupPath; strDir += "\\temp.png"; bitmap.Save(strDir, System.Drawing.Imaging.ImageFormat.Png); //---Storage bitmpa //---單純QR //--- //插入LOGO QrCode strDir = System.Windows.Forms.Application.StartupPath; strDir += "\\temp01.png"; Bitmap bitmap01 = null; bitmap01 = GetQRCodeByZXingNet("http://jashliao.eu/wordpress/", 200, 200); bitmap01.Save(strDir, System.Drawing.Imaging.ImageFormat.Png); pictureBox1.Image = bitmap01; //---插入LOGO QrCode } } }
5 thoughts on “C# 使用Zxing元件(透過nuget下載)產生Qrcode圖片[CS_QrCode] (GOOGLE: qrcode with logo)”
H水平 30%的字碼可被修正
QR CODE 300×300
目前測試 LOGO圖片 最大 60×60 還OK
如果要使用更大的圖片就要設定 『 EncodeHintType.QR_VERSION [1~40] 』,API文件 建議不要自己設定,如果真的要玩,可以先在『https://qr.ioi.tw/zh/』 測試之後在時現在程式碼
目前測試
資料: 0000000000000001
LOGO圖片: 80×80
EncodeHintType.QR_VERSION 設定 4
[C#][QR Code 一維碼、二維碼] ~ 產生二維碼
https://dotblogs.com.tw/kevin_blog/2018/11/28/151842
private Bitmap CreateQRCode(string content)
{
ZXing.BarcodeWriter writer = new ZXing.BarcodeWriter
{
//要產生的Code類型
Format = ZXing.BarcodeFormat.QR_CODE,
//產生圖形的屬性
Options = new ZXing.QrCode.QrCodeEncodingOptions
{
Height = 250, //圖形的高
Width = 250, //圖形的寬
CharacterSet = "UTF-8", //編碼格式 UTF-8 中文才不會出現亂
//錯誤修正內容
ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.M
};
var bm = writer.Write(content);
return bm;
}
[C#][QR Code 一維碼、二維碼] ~ 產生二維碼
private Bitmap CreateBarCode(string content)
{
BarcodeWriter barcodeWriter = new BarcodeWriter
{
Format = BarcodeFormat.CODE_128,
Options = new EncodingOptions
{
Height = 100,
Width = 200,
PureBarcode = false,
Margin = 0
}
};
var barCode = barcodeWriter.Write(content);
return barCode;
}
[C#][QR Code 一維碼、二維碼] ~ 讀取一、二維碼
private string ReadCode(string filePath)
{
//filePath 可以是一維或二維碼
ZXing.IBarcodeReader reader = new ZXing.BarcodeReader();
FileStream fs = new FileStream(filePath, FileMode.Open);
Byte[] data = new Byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close();
MemoryStream ms = new MemoryStream(data);
var bitmap = (Bitmap)Image.FromStream(ms);
ZXing.Result result = reader.Decode(bitmap);
return result.ToString();
}