前言

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的二维码生成的更多相关文章

  1. C# - VS2019 WinFrm程序调用ZXing.NET实现条码、二维码和带有Logo的二维码的识别

    前言 C# WinFrm程序调用ZXing.NET实现条码.二维码和带有Logo的二维码的识别. ZXing.NET导入 GitHub开源库 ZXing.NET开源库githib下载地址:https: ...

  2. C# 生成二维码,彩色二维码,带有Logo的二维码及普通条形码

    每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...

  3. php--------php库生成二维码和有logo的二维码

    php生成二维码和带有logo的二维码,上一篇博客讲的是js实现二维码:php--------使用js生成二维码. 今天写的这个小案例是使用php库生成二维码: 效果图:        使用了 php ...

  4. (转)ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果

    场景:移动支付需要对二维码的生成与部署有所了解,掌握目前主流的二维码生成技术. 1 ZXing 生成二维码 首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老 ...

  5. 带有logo的二维码

    摘要: 前面介绍了使用javascript生成二维码,但是这样并不能满足我们的需求,我们有时也会看到带有logo的二维码,本文就介绍如何生成带有logo的二维码. 简介: 主要使用了svg的文本和图像 ...

  6. Thinkphp3.2结合phpqrcode生成二维码(含Logo的二维码),附案例

    首先,下载phpqrcode,将其解压到项目ThinkPHP\Library\Vendor目录下.Index_index.html(模板可自行配置) <form action="{:U ...

  7. 建议收藏备用:.net core使用QRCoder生成普通二维码和带Logo的二维码详细使用教程,源码已更新至开源模板

    随着互联网越来越生活化,二维码的使用越来越普遍,不论是扫码支付还是扫码关注引流,似乎我们总是离不开二维码,那么很多需要推广的文章或社区想要自己的二维码,那么你是不是需要在网站直接提供给用户呢?很多开发 ...

  8. php 使用phpqrcode类生成带有logo的二维码 使logo不失真(透明)

    在开发中 发现phpqrcode类在加入logo时,如果 logo 是 png 图像带有透明区域时,二维码上都无法正常完美的显示出来 解决方法便是:修改phpqrcode文件中的 QRimage类下的 ...

  9. js生成带有logo的二维码并保存成图片下载

    生成二维码,需要依赖jquery,先引入一个jquery,然后需要一个插件改变过了jquery-qrcode.js 插件代码(需要的自己打开看): /*! jquery-qrcode v0.14.0 ...

随机推荐

  1. Java自定义注解(1)

    Java注解简介 1. Java注解(Annotation) Java注解是附加在代码中的一些元信息,用于一些工具在编译. 运行时进行解析和使用,起到说明.配置的功能. 注解相关类都包含在java.l ...

  2. PlayJava Day001

    今日所学: /* 2019.08.19开始学习,此为补档. */ 三目(元)运算符 格式:(表达式)? 表达式为true返回值A : 表达式为false返回值B 例: String s=2>3 ...

  3. FCC---Make Motion More Natural Using a Bezier Curve--- juggling movement

    This challenge animates an element to replicate the movement of a ball being juggled. Prior challeng ...

  4. Metasploit漏洞扫描

    Metasploit漏洞扫描 漏洞扫描是自动在目标中寻找和发现安全弱点. 漏洞扫描器会在网络上和对方产生大量的流量,会暴露自己的行为过程,如此就不建议你使用漏扫了. 基本的漏洞扫描 我们首先使用net ...

  5. mmap - 内存映射文件 - 减少一次内核空间内数据向用户空间数据拷贝的操作

    关于mmap 网上有很多有用的文章,我这里主要记录,日常使用到mmap时的理解: https://www.cnblogs.com/huxiao-tee/p/4660352.html 测试代码: htt ...

  6. Ubuntu安装DaVinci Resolve

    安装DaVinci Resolve所需依赖 sudo apt install libssl1.0.0 ocl-icd-opencl-dev fakeroot xorriso 下载MakeResolve ...

  7. fiddler---Fiddler实现手机抓包

    测试app的时候发现一些问题,我们也可以通过Fiddler进行对手机app进行抓包. 手机抓包 环境准备 1.手机一台 2.电脑上必须安装Fiddler 3.Fiddler和手机保持在同一个局域网内 ...

  8. OpenGL实例:纹理映射

    OpenGL实例:纹理映射 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 更多请查看:计算机图形学 1. 介绍 用于指定一维.二维和三维纹理的函数分别 ...

  9. python 生成sql语句

    1. 别名 s = '' name = ['张三', '李四', '王五'] for i in range(len(name)): s += "'" + name[i] + &qu ...

  10. Python else

    Python else else 可以用来搭配其他语句完成条件判断 最常用的就是 if...else... 当然还有一些其他语句也可以配合 else 使用 if if...else... 是最简单的条 ...