C# - VS2019调用ZXing.NET实现条码、二维码和带有Logo的二维码生成
前言
C# WinFrm程序调用ZXing.NET实现条码、二维码和带有Logo的二维码生成。
ZXing.NET导入
GitHub开源库
ZXing.NET开源库githib下载地址:https://github.com/zxing/zxing
NuGet包管理
选择安装ZXing.NET v0.16.1版本。
前台部署搭建
如下图,创建WinFrm桌面应用程序后,添加如下必要的控件。
封装ZXingLibs类
核心代码如下:
注意条形码暂时只支持数字(Requested contents should only contain digits, but got 'i');
只支持偶数个(The lenght of the input should be even);
码值最大长度为80(Requested contents should be less than 80 digits long, but got 102)。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.QrCode.Internal; namespace GetBarCodeQRCode_ZXing
{
/// <summary>
/// 重新封装条码、二维码生成方法和带有图片的二维码生成方法
/// </summary>
public class ZXingLibs
{
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static Bitmap GetQRCode(string text, int width, int height)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
QrCodeEncodingOptions options = new QrCodeEncodingOptions()
{
DisableECI = true,//设置内容编码
CharacterSet = "UTF-8", //设置二维码的宽度和高度
Width = width,
Height = height,
Margin = //设置二维码的边距,单位不是固定像素
}; writer.Options = options;
Bitmap map = writer.Write(text);
return map;
} /// <summary>
/// 生成一维条形码
/// 只支持数字 Requested contents should only contain digits, but got 'i'
/// 只支持偶数个 The lenght of the input should be even
/// 最大长度80 Requested contents should be less than 80 digits long, but got 102
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static Bitmap GetBarCode(string text, int width, int height)
{
BarcodeWriter writer = new BarcodeWriter();
//使用ITF 格式,不能被现在常用的支付宝、微信扫出来
//如果想生成可识别的可以使用 CODE_128 格式
//writer.Format = BarcodeFormat.ITF;
writer.Format = BarcodeFormat.CODE_39;
EncodingOptions options = new EncodingOptions()
{
Width = width,
Height = height,
Margin =
};
writer.Options = options;
Bitmap map = writer.Write(text);
return map;
} /// <summary>
/// 生成带Logo的二维码
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
public static Bitmap GetQRCodeWithLogo(string text, int width, int height)
{
//Logo 图片
string logoPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\img\logo.bmp";
Bitmap logo = new Bitmap(logoPath);
//构造二维码写码器
MultiFormatWriter writer = new MultiFormatWriter();
Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//hint.Add(EncodeHintType.MARGIN, 2);//旧版本不起作用,需要手动去除白边 //生成二维码
BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width + , height+, hint);
bm = deleteWhite(bm);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap map = barcodeWriter.Write(bm); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
int[] rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置
int middleW = Math.Min((int)(rectangle[] / 3.5), logo.Width);
int middleH = Math.Min((int)(rectangle[] / 3.5), logo.Height);
int middleL = (map.Width - middleW) / ;
int middleT = (map.Height - middleH) / ; Bitmap bmpimg = new Bitmap(map.Width, map.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(map, , , width, height);
//白底将二维码插入图片
g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
g.DrawImage(logo, middleL, middleT, middleW, middleH);
}
return bmpimg;
} /// <summary>
/// 删除默认对应的空白
/// </summary>
/// <param name="matrix"></param>
/// <returns></returns>
private static BitMatrix deleteWhite(BitMatrix matrix)
{
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[] + ;
int resHeight = rec[] + ; BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = ; i < resWidth; i++)
{
for (int j = ; j < resHeight; j++)
{
if (matrix[i + rec[], j + rec[]])
resMatrix[i, j] = true;
}
}
return resMatrix;
}
}
}
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; namespace GetBarCodeQRCode_ZXing
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
// 实例化ZXingLibs类
public ZXingLibs zxTools = new ZXingLibs(); /// <summary>
/// 清空输入框内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClear_Click(object sender, EventArgs e)
{
this.tbValues.Text = "";
} /// <summary>
/// 生成条码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBarCode_Click(object sender, EventArgs e)
{
if (this.tbValues.Text.ToString().Trim() != "")
{
this.pbImage.Image = ZXingLibs.GetBarCode(this.tbValues.Text.ToString(), this.pbImage.Width, this.pbImage.Height);
}
} /// <summary>
/// 生成二维码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnQRCode_Click(object sender, EventArgs e)
{
if (this.tbValues.Text.ToString().Trim() != "")
{
this.pbImage.Image = ZXingLibs.GetQRCode(this.tbValues.Text.ToString(), this.pbImage.Width, this.pbImage.Height);
}
} /// <summary>
/// 生成带有Logo的二维码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetQRCodeWithLogo_Click(object sender, EventArgs e)
{
this.pbImage.Image = ZXingLibs.GetQRCodeWithLogo(this.tbValues.Text.ToString(), this.pbImage.Width, this.pbImage.Height);
}
}
}
实现效果
参考资料 GitHub
作者:Jeremy.Wu
出处:https://www.cnblogs.com/jeremywucnblog/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
C# - VS2019调用ZXing.NET实现条码、二维码和带有Logo的二维码生成的更多相关文章
- C# - VS2019 WinFrm程序调用ZXing.NET实现条码、二维码和带有Logo的二维码的识别
前言 C# WinFrm程序调用ZXing.NET实现条码.二维码和带有Logo的二维码的识别. ZXing.NET导入 GitHub开源库 ZXing.NET开源库githib下载地址:https: ...
- C# 生成二维码,彩色二维码,带有Logo的二维码及普通条形码
每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...
- php--------php库生成二维码和有logo的二维码
php生成二维码和带有logo的二维码,上一篇博客讲的是js实现二维码:php--------使用js生成二维码. 今天写的这个小案例是使用php库生成二维码: 效果图: 使用了 php ...
- (转)ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果
场景:移动支付需要对二维码的生成与部署有所了解,掌握目前主流的二维码生成技术. 1 ZXing 生成二维码 首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老 ...
- 带有logo的二维码
摘要: 前面介绍了使用javascript生成二维码,但是这样并不能满足我们的需求,我们有时也会看到带有logo的二维码,本文就介绍如何生成带有logo的二维码. 简介: 主要使用了svg的文本和图像 ...
- Thinkphp3.2结合phpqrcode生成二维码(含Logo的二维码),附案例
首先,下载phpqrcode,将其解压到项目ThinkPHP\Library\Vendor目录下.Index_index.html(模板可自行配置) <form action="{:U ...
- 建议收藏备用:.net core使用QRCoder生成普通二维码和带Logo的二维码详细使用教程,源码已更新至开源模板
随着互联网越来越生活化,二维码的使用越来越普遍,不论是扫码支付还是扫码关注引流,似乎我们总是离不开二维码,那么很多需要推广的文章或社区想要自己的二维码,那么你是不是需要在网站直接提供给用户呢?很多开发 ...
- php 使用phpqrcode类生成带有logo的二维码 使logo不失真(透明)
在开发中 发现phpqrcode类在加入logo时,如果 logo 是 png 图像带有透明区域时,二维码上都无法正常完美的显示出来 解决方法便是:修改phpqrcode文件中的 QRimage类下的 ...
- js生成带有logo的二维码并保存成图片下载
生成二维码,需要依赖jquery,先引入一个jquery,然后需要一个插件改变过了jquery-qrcode.js 插件代码(需要的自己打开看): /*! jquery-qrcode v0.14.0 ...
随机推荐
- Java生鲜电商平台-订单配送模块的架构与设计
Java生鲜电商平台-订单配送模块的架构与设计 生鲜电商系统最终的目的还是用户下单支付购买, 所以订单管理系统是电商系统中最为复杂的系统,其作为中枢决定着整个商城的运转, 本文将对于生鲜类电商平台的订 ...
- Java生鲜电商平台-生鲜供应链(采购管理)
Java生鲜电商平台-生鲜供应链(采购管理) 在生鲜供应链系统中采购中心这一模块,它是电商公司管理采购的模块,包含供应商管理,采购订单管理,采购商品管理,在该模块中采购订单是采购中心的核心模块.在其他 ...
- django5-书籍与出版社关联外键
1.外键相关 一对多的概念 ,这里是一个出版社对应本书籍 ! 设计表使用model models.ForeignKey('关联一', on_delete=models.CASCADE) #给多设置外键 ...
- rsync 未授权访问漏洞
rsync rsync,remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限.时间.软硬链接等附加信息. rsync是用 &qu ...
- SoapUI、Postman测试WebService
之前装了SoapUI,最近装了Postman,分别用它们测试了WebService,下面为用法. 测试的在线WebService(数据来源于中国气象局)Endpoint: http://www.web ...
- apache jmeter 使用
======================= 插件 ======================= 在JMeter 中很多东西都是基于插件技术的, 所以扩展性非常好, 比如下面这些东西都是插件: 1 ...
- mssql的text字段中文乱码
问题: 1.在页面存入中文后乱码,从前端从后台发现数据未发生异常,发现是存入数据库后乱码: 经查询该字段为text字段,存入中文会乱码 如图 解决办法: 1.将text转为varchar或nvarch ...
- pyhton 基础数据的爬取1
1. 什么是网络爬虫? 在大数据时代,信息的采集是一项重要的工作,而互联网中的数据是海量的,如果单纯靠人力进行信息采集,不仅低效繁琐,搜集的成本也会提高.如何自动高效地获取互联网中我们感兴趣的信息 ...
- 标签中href="javascript:;"表示什么意思?
1.标签的 href 属性用于指定超链接目标的 URL,href 属性的值可以是任何有效文档的相对或绝对 URL,包括片段标识符和 JavaScript 代码段. 2.javascript: 是一个伪 ...
- 爬虫 crawlSpider 分布式 增量式 提高效率
crawlSpider 作用:为了方便提取页面整个链接url,不必使用创参寻找url,通过拉链提取器,将start_urls的全部符合规则的URL地址全部取出 使用:创建文件scrapy startp ...