1、ZXing.Net介绍:
ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。而ZXing.Net是ZXing的端口之一。这里是Zxing文件下载地址,提取密码是56jn;
2、在项目的引用中添加这个dll文件的引用,如下图所示:
3、添加关键代码展示:
(1):这个方法是生成打印的二维码图片,需要修改的地方就是你自己电脑保存图片文件的位置,在Save()方法处修改。
///
/// 生成二维码图片
///
/// 要生成二维码的字符串
/// 二维码图片宽度
/// 二维码图片高度
///
private Bitmap GetQRCodeByZXingNet(String strMessage, Int32 width, Int32 height)
{
Bitmap result = null;
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 = 0;
ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage);
result = barCodeWriter.Write(bm);
string imgName = DateTime.Now.Millisecond.ToString();
result.Save(@"E:\测试\" + imgName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
this.textBox2.Text = Convert.ToString(ex);
}
return result;
}
(2):这个方法是生成打印的二维码图片,需要修改的地方就是你自己电脑保存图片文件的位置,在Save()方法处修改。
///
/// 生成带Logo的二维码
///
/// 内容
/// 宽度
/// 高度
private Bitmap GetQRCodeByZXingNetTwo(String strMessage, Int32 width, Int32 height)
{
Bitmap result = null;
Bitmap bmpimg = null;
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 = 0;
ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage);
result = barCodeWriter.Write(bm);
string imgName = DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString();
result.Save(@"E:\测试\" + imgName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
string logoPath = @"E:\VS2013Projects\BaPrint\ZXingTest\img\timg.jpg";
Bitmap logo = new Bitmap(logoPath);
//获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
int[] rectangle = bm.getEnclosingRectangle();
//计算插入图片的大小和位置
int middleW = Math.Min((int)(rectangle[2] / 3), logo.Width);
int middleH = Math.Min((int)(rectangle[3] / 3), logo.Height);
int middleL = (result.Width - middleW) / 2;
int middleT = (result.Height - middleH) / 2;
//Bitmap bmpimg = new Bitmap(result.Width, result.Height, PixelFormat.Format32bppArgb);
bmpimg = new Bitmap(result.Width, result.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(result, 0, 0, width, height);
//白底将二维码插入图片
g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
g.DrawImage(logo, middleL, middleT, middleW, middleH);
}
bmpimg.Save(@"E:\测试\" + imgName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
this.textBox1.Text = Convert.ToString(ex);
}
return bmpimg;
}
///
/// 删除默认对应的空白
///
///
///
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;
}
如果想要打印条形码在下图所示处选择想要打印条码的格式
好了,
生成条形码和二维码的方式多种,条码的种类也有很多种,每一种都有其对应的应用领域,希望此文能够帮到你