尝试以下代码。它已经过测试,但您可能需要根据您的使用情况进行调整,这应该不会太困难。
下载/安装 NuGet 包:ZXing.Net
(版本:0.16.8)
添加以下 using 指令:
using System.IO;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
Code:
private Bitmap CreateQrCode(string data)
{
//specify desired options
QrCodeEncodingOptions options = new QrCodeEncodingOptions()
{
CharacterSet = "UTF-8",
DisableECI = true,
Width = 250,
Height = 250
};
//create new instance and set properties
BarcodeWriter writer = new BarcodeWriter()
{
Format = BarcodeFormat.QR_CODE,
Options = options
};
//create QR code and return Bitmap
return writer.Write(data);
}
private byte[] GetQrCodeBytes(string data, System.Drawing.Imaging.ImageFormat imgFormat)
{
using (MemoryStream ms = new MemoryStream())
{
//create QR code and save to file
using (Bitmap bmp = CreateQrCode(data))
{
//save to MemoryStream
bmp.Save(ms, imgFormat);
}
return ms.ToArray();
}
}
private string GetTextFromQrCode(byte[] qrcodeBytes)
{
//specify desired options
DecodingOptions options = new DecodingOptions()
{
CharacterSet = "UTF-8"
};
//create new instance and set properties
BarcodeReader reader = new BarcodeReader() { Options = options };
using (MemoryStream ms = new MemoryStream(qrcodeBytes))
{
using (Bitmap bmp = (Bitmap)Bitmap.FromStream(ms))
{
//decode QR code
Result r = reader.Decode(bmp);
//return QR code text
return r.Text;
}
}
}
private string GetTextFromQrCode(string filename)
{
//specify desired options
DecodingOptions options = new DecodingOptions()
{
CharacterSet = "UTF-8"
};
//create new instance and set properties
BarcodeReader reader = new BarcodeReader() { Options = options };
//read image and convert to Bitmap
using (Bitmap bmp = (Bitmap)Bitmap.FromFile(filename))
{
//decode QR code
Result r = reader.Decode(bmp);
//return QR code text
return r.Text;
}
}
private void SaveQrCode(string data, string filename, System.Drawing.Imaging.ImageFormat imgFormat)
{
//create QR code and save to file
using (Bitmap bmp = CreateQrCode(data))
{
bmp.Save(filename, imgFormat);
}
}
解码二维码(用法1):
string filename = @"C:\Temp\TestQrCode.png"
string qrcodeText = GetTextFromQrCode(filename);
解码二维码(用法2):
Note:在下面的代码中,从文件中读取二维码并将其放入byte[]
。从文件中读取用于演示/测试目的。
string filename = @"C:\Temp\TestQrCode.png"
byte[] qrcodeBytes = File.ReadAllBytes(filename);
string qrcodeText = GetTextFromQrCode(qrcodeBytes);
保存二维码(用法1):
string qrcodeText = "This is a test";
string filename = @"C:\Temp\TestQrCode.png"
//get filename extension
string ext = Path.GetExtension(filename);
if (ext == ".bmp" || ext == ".dib" || ext == ".rle")
SaveQrCode(qrcodeText, filename, System.Drawing.Imaging.ImageFormat.Bmp);
else if (ext == ".jpg" || ext == ".jpeg" || ext == ".jfif" || ext == ".jpe")
SaveQrCode(qrcodeText, fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
else if (ext == ".png")
SaveQrCode(qrcodeText, fileName, System.Drawing.Imaging.ImageFormat.Png);
保存二维码(用法2):
Note:在下面的代码中,二维码被保存到byte[]
,然后写入文件。写入文件用于演示/测试目的。
string qrcodeText = "This is a test";
string filename = @"C:\Temp\TestQrCode.png"
//get filename extension
string ext = Path.GetExtension(filename);
byte[] qrcodeBytes = null;
if (ext == ".bmp" || ext == ".dib" || ext == ".rle")
qrcodeBytes = GetQrCodeBytes(qrcodeText, System.Drawing.Imaging.ImageFormat.Bmp);
else if (ext == ".jpg" || ext == ".jpeg" || ext == ".jfif" || ext == ".jpe")
qrcodeBytes = GetQrCodeBytes(qrcodeText, System.Drawing.Imaging.ImageFormat.Jpeg);
else if (ext == ".png")
qrcodeBytes = GetQrCodeBytes(qrcodeText, System.Drawing.Imaging.ImageFormat.Png);
//save to file
File.WriteAllBytes(fileName, qrcodeBytes);
资源
- 如何使用 C# 和 ZXing.NET 读取和创建条形码图像 https://jeremylindsayni.wordpress.com/2016/04/02/how-to-read-and-create-barcode-images-using-c-and-zxing-net/
- 使用 Zxing 库的 QR 码基础知识 https://www.codeproject.com/Articles/1005081/Basic-with-QR-Code-using-Zxing-Library
- 将过滤器设置为 OpenFileDialog 以允许典型的图像格式? https://stackoverflow.com/questions/2069048/setting-the-filter-to-an-openfiledialog-to-allow-the-typical-image-formats