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 ...
随机推荐
- python网络爬虫进阶之HTTP原理,爬虫的基本原理,Cookies和代理介绍
目录 一.HTTP基本原理 (一)URI和URL (二)超文本 (三)HTTP和HTTPS (四)HTTP请求过程 (五)请求 1.请求方法 2.请求的网址 3.请求头 4.请求体 (六)响应 1.响 ...
- 通过JS屏蔽鼠标右键
我也是第一次接触这个功能,只需一行代码即可搞定,直译过来就是“屏蔽上下文菜单”,特此记录一下吧. document.oncontextmenu = () => false;
- Redis Python(一)
Infi-chu: http://www.cnblogs.com/Infi-chu/ NoSQL(NoSQL=Not Only SQL),中文意思是非关系型数据库. 随着互联网Web2.0网站的兴起, ...
- [20190522]DISABLE TABLE LOCK.txt
[20190522]DISABLE TABLE LOCK.txt --//如果禁止table lock时,一些ddl操作会被禁止.但是我有点吃惊的是增加字段不受限制.--//通过测试说明问题. 1.环 ...
- MongoDB 副本集丢失数据的测试
在MongoDB副本集的测试中发现了一个丢数据的案例. 1. 概要描述 测试场景为:一主一从一验证 测试案例 step1 :关闭从副本: step 2 :向主副本中插入那条数据: step 3 :关闭 ...
- openresty配置
目录 环境 wsl安装 openresty安装 openssl版本问题 Lua模块安装 openresty使用 nginx常用命令 nginx.conf 配置 lua 的一些坑 OpenResty缓存 ...
- 爬虫---爬取b站小视频
前面通过python爬虫爬取过图片,文字,今天我们一起爬取下b站的小视频,其实呢,测试过程中需要用到视频文件,找了几个网站下载,都需要会员什么的,直接写一篇爬虫爬取视频~~~ 分析b站小视频 1.进入 ...
- nginx默认配置文件解释
nginx默认配置文件 nginx.conf 介绍: 全局配置 user nginx; 设置nginx服务的系统使用用户 worker_processes 1; 工作进程数(建议和CPU核心数保持 ...
- 初学JavaScript正则表达式(三)
正则表达式由两种基本的字符类型组成 原义文本字符 //a abc 1 元字符 元字符是有特使含义的非字母字符 * 匹配前面的子表达式零次或多次 + 匹配前面的子表达式一次或多次 ? 匹配前面的子表达式 ...
- 2017年蓝桥杯B组C/C++决赛题解
2017年蓝桥杯B组C/C++决赛题目(不含答案) 1.36进制 ok 求36进制,类比二进制转10进制,36^3 + 36^2 + 36^1 + 36^0 2.磁砖样式 ok dfs搜索 我自己写的 ...