注意 :需要使用以下库文件
using ThoughtWorks.QRCode.Codec;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
具体实现如下所示:
帮助类一:
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using ThoughtWorks.QRCode.Codec;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
namespace KK.BARCODE.HELPER
{
/// <summary>
/// 二维码:在水平和垂直方向的二维空间存储信息的条形码。
///常用码制:PDF417、QR Code、Code 49、Code 16K、Code One等
/// </summary>
public class QrCodeHelper
{
private static QrCodeHelper instance;
public static QrCodeHelper Instance
{
get
{
if (instance == null) instance = new QrCodeHelper();
return QrCodeHelper.instance;
}
}
/// <summary>
/// 使用ThoughtWorks生成二维码图片
/// </summary>
/// <param name="imgPath">图片路径</param>
/// <param name="codeContent">内容信息</param>
/// <returns></returns>
public string NewQRCodeByThoughtWorks(string imgPath, string codeContent, ImageFormat imgType)
{
QRCodeEncoder encoder = new QRCodeEncoder();
encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字)
encoder.QRCodeScale = 4;//大小(值越大生成的二维码图片像素越高)
encoder.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误)
encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//错误效验、错误更正(有4个等级)
encoder.QRCodeBackgroundColor = Color.Yellow;
encoder.QRCodeForegroundColor = Color.Green;
Bitmap bcodeBitmap = encoder.Encode(codeContent);
bcodeBitmap.Save(imgPath, imgType);//@"E:\test.png" ImageFormat.Png
bcodeBitmap.Dispose();
return imgPath;
}
/// <summary>
/// 使用ZxingNet生成二维码图片
/// </summary>
/// <param name="imgPath">图片路径</param>
/// <param name="codeContent">内容信息</param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="imgType">ImageFormat imgType</param>
/// <param name="BarcodeFormat barcodeFormat">barcodeFormat</param>
/// <returns></returns>
public string NewQRCodeByZxingNet(string imgPath, string codeContent, int width, int height,
ImageFormat imgType, BarcodeFormat barcodeFormat)
{
// 1.设置QR二维码的规格
QrCodeEncodingOptions code = new QrCodeEncodingOptions();
code.CharacterSet = "UTF-8"; // 设置编码格式,否则读取'中文'乱码
code.Height = height;
code.Width = width;
code.Margin = 1; // 设置周围空白边距
// 2.生成条形码图片并保存
BarcodeWriter wr = new BarcodeWriter();
wr.Format = barcodeFormat; // 二维码 BarcodeFormat.QR_CODE
wr.Options = code;
Bitmap img = wr.Write(codeContent);
img.Save(imgPath, imgType);
return imgPath;
}
/// <summary>
/// ZXing根据图片读取二维码内容
/// </summary>
/// <param name="img">Image img</param>
/// <param name="BarcodeFormat barcodeFormat">barcodeFormat</param>
/// <returns></returns>
public string ReadQrCode(Image img, BarcodeFormat barcodeFormat)
{
// 1.设置读取条形码规格
DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List<BarcodeFormat>() { barcodeFormat };
// 2.进行读取操作
ZXing.BarcodeReader br = new BarcodeReader();
br.Options = decodeOption;
ZXing.Result rs = br.Decode(img as Bitmap);
if (rs == null) return string.Empty;
else return rs.Text;
}
/// <summary>
/// 使用ZxingNet生成二维码图片 二维码带有校验功能,故可以在中间区域展示一定尺寸的图片
/// </summary>
/// <param name="logoImg">logo图片路径</param>
/// <param name="imgPath">二维码图片路径</param>
/// <param name="codeContent"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="imgType">ImageFormat imgType</param>
/// <param name="BarcodeFormat barcodeFormat">barcodeFormat</param>
/// <returns></returns>
public string NewQrCodeAndImgByZxingNet(string logoImg, string imgPath, string codeContent,
int width, int height, ImageFormat imgType, BarcodeFormat barcodeFormat)
{
// 1.设置QR二维码的规格
QrCodeEncodingOptions qrEncodeOption = new QrCodeEncodingOptions();
qrEncodeOption.CharacterSet = "UTF-8"; // 设置编码格式,否则读取'中文'乱码
qrEncodeOption.Height = height;
qrEncodeOption.Width = width;
qrEncodeOption.Margin = 1; // 设置周围空白边距
// 2.生成条形码图片
BarcodeWriter wr = new BarcodeWriter();
wr.Format = barcodeFormat; // 二维码 BarcodeFormat.QR_CODE
wr.Options = qrEncodeOption;
Bitmap img = wr.Write(codeContent);
// 3.在二维码的Bitmap对象上绘制logo图片
Bitmap logo = Bitmap.FromFile(logoImg) as Bitmap;
Graphics g = Graphics.FromImage(img);
Rectangle logoRec = new Rectangle(); // 设置logo图片的大小和绘制位置
logoRec.Width = img.Width / 6;
logoRec.Height = img.Height / 6;
logoRec.X = img.Width / 2 - logoRec.Width / 2; // 中心点
logoRec.Y = img.Height / 2 - logoRec.Height / 2;
g.DrawImage(logo, logoRec);
// 4.保存绘制后的图片
img.Save(imgPath, imgType);
return imgPath;
}
}
}
帮助类二:
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using ZXing;
using ZXing.Common;
namespace KK.BARCODE.HELPER
{
/// <summary>
/// 一维条形码:只是在一个方向(一般是水平方向)表达信息,而在垂直方向则不表达任何信息。
/// 常用码制:EAN码、39码、交叉25码、UPC码、128码、93码,ISBN码及Codabar(库德巴码)等。
/// 国内推行使用的是EAN商品条形码,可分为EAN-13(标准版)和EAN-8(缩短版)两种。
/// 例图:
/// </summary>
public class OneCodeHelper
{
private static OneCodeHelper instance;
public static OneCodeHelper Instance
{
get
{
if (instance == null) instance = new OneCodeHelper();
return OneCodeHelper.instance;
}
}
/// <summary>
/// 创建一维码
/// </summary>
/// <param name="imgPath">图片路径</param>
/// <param name="codeText">图片内容</param>
/// <param name="width">图片宽度</param>
/// <param name="heigth">图片高度</param>
/// <param name="imgType">图片类型</param>
/// <param name="barcodeFormat">条形码规格</param>
/// <returns></returns>
public string NewOneCode(string imgPath, string codeText, int width, int heigth,
ImageFormat imgType, BarcodeFormat barcodeFormat)
{
// 1.设置条形码规格
EncodingOptions encodeOption = new EncodingOptions();
encodeOption.Height = heigth; // 必须制定高度、宽度
encodeOption.Width = width;
// 2.生成条形码图片并保存
BarcodeWriter wr = new BarcodeWriter();
wr.Options = encodeOption;
wr.Format = barcodeFormat; // 条形码规格:EAN13规格:12(无校验位)或13位数字
Bitmap img = wr.Write(codeText); // 生成图片
img.Save(imgPath, imgType);
return imgPath;
}
/// <summary>
/// ZXing根据图片读取一维码内容
/// </summary>
/// <param name="img">Image img</param>
/// <param name="BarcodeFormat barcodeFormat">barcodeFormat</param>
/// <returns></returns>
public string ReadQrCode(Image img, BarcodeFormat barcodeFormat)
{
// 1.设置读取条形码规格
DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List<BarcodeFormat>() { barcodeFormat };
// 2.进行读取操作
ZXing.BarcodeReader br = new BarcodeReader();
br.Options = decodeOption;
ZXing.Result rs = br.Decode(img as Bitmap);
if (rs == null) return string.Empty;
else return rs.Text;
}
}
}
测试界面:
using KK.BARCODE.HELPER;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using ZXing;
namespace KK.BARCODE.TEST
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnNewImg_Click(object sender, EventArgs e)
{
string path = @"E:\test.Jpeg";
string text = "kk.qrcode.helper001";
QrCodeHelper.Instance.NewQRCodeByThoughtWorks(path, text, ImageFormat.Jpeg);
if (File.Exists(path))
{
this.pbQrImg.Image = Image.FromFile(path);
}
}
private void btnOne_Click(object sender, EventArgs e)
{
string path = @"E:\test.Jpeg";
this.txtOne.Text=QrCodeHelper.Instance.ReadQrCode(Image.FromFile(path), BarcodeFormat.QR_CODE);
}
private void button1_Click(object sender, EventArgs e)
{
string path = @"E:\test1.Jpeg";
string text = "kk.qrcode.helper002";
QrCodeHelper.Instance.NewQRCodeByZxingNet(path, text,200,200, ImageFormat.Jpeg, BarcodeFormat.QR_CODE);
if (File.Exists(path))
{
this.pictureBox1.Image = Image.FromFile(path);
}
}
private void btnTwo_Click(object sender, EventArgs e)
{
string path = @"E:\test1.Jpeg";
this.txtTwo.Text = QrCodeHelper.Instance.ReadQrCode(Image.FromFile(path), BarcodeFormat.QR_CODE);
}
private void button2_Click(object sender, EventArgs e)
{
string path = @"E:\test2.Jpeg";
string logo = Application.StartupPath + "\\logo.png";
string text = "kk.qrcode.helper003";
QrCodeHelper.Instance.NewQrCodeAndImgByZxingNet(logo,path, text, 200, 200, ImageFormat.Jpeg, BarcodeFormat.QR_CODE);
if (File.Exists(path))
{
this.pictureBox2.Image = Image.FromFile(path);
}
}
private void btnThree_Click(object sender, EventArgs e)
{
string path = @"E:\test2.Jpeg";
this.txtThree.Text = QrCodeHelper.Instance.ReadQrCode(Image.FromFile(path), BarcodeFormat.QR_CODE);
}
private void button3_Click(object sender, EventArgs e)
{
string path = @"E:\test3.Jpeg";
string text = "kk.qrcode.helper004";
OneCodeHelper.Instance.NewOneCode(path, text, 100, 300, ImageFormat.Jpeg, BarcodeFormat.CODE_128);
if (File.Exists(path))
{
this.pictureBox3.Image = Image.FromFile(path);
}
}
private void btnFour_Click(object sender, EventArgs e)
{
string path = @"E:\test3.Jpeg";
this.txtFour.Text = OneCodeHelper.Instance.ReadQrCode(Image.FromFile(path), BarcodeFormat.CODE_128);
}
}
}
运行效果如下图所示: