ZXing使用详解与范例(C#)
介绍
ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。(引自百度百科)
用途
生成一维码、二维码,支持各种格式(比如:Datamatrix、QR、Code39等)
解析一维码、二维码,支持各种格式(比如:Datamatrix、QR、Code39等)
源码
库
/// <summary>
/// 解码二维码
/// </summary>
/// <param name="barcodeBitmap">待解码的二维码图片</param>
/// <returns>扫码结果</returns>
public static string DecodeQrCode(Bitmap barcodeBitmap)
{
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
var result = reader.Decode(barcodeBitmap);
return (result == null) ? null : result.Text;
}
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static Bitmap Generate2DBarcode(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 = 1//设置二维码的边距,单位不是固定像素
};
writer.Options = options;
Bitmap map = writer.Write(text);
return map;
}
示例
private void Btn_Create2Dbarcode_Click(object sender, EventArgs e)
{
BarcodePicture.Image = null;
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start(); // 开始监视代码
Bitmap bmap=BarcodeHelper.Generate2DBarcode(CreateBarcode.Text, 100, 100);
stopwatch.Stop(); // 停止监视
BarcodePicture.Image = bmap;
TimeSpan timeSpan = stopwatch.Elapsed; // 获取总时间
double milliseconds = timeSpan.TotalMilliseconds; // 毫秒数
CreateTime.Text = "Identify Time:" + timeSpan.TotalMilliseconds + "ms";
/* Bitmap bmp = BarcodeHelper.Generate2DBarcode("123456", 100, 100);
//保存到磁盘文件
bmp.Save("C:/1.bmp");
bmp.Dispose();*/
}
private void Btn_Identify2Dbarcode_Click(object sender, EventArgs e)
{
log.Clear();
log.AppendText("Identify Start:"+System.DateTime.Now.TimeOfDay.ToString()+"\n");
uint a = timeGetTime();
IdentifyBarcode.Text = BarcodeHelper.DecodeQrCode((Bitmap)BarcodePicture.Image);
//IdentifyBarcode.Text = BarcodeHelper.DecodeQrCode(BarcodeHelper.Generate2DBarcode("123", 100, 100));
uint b = timeGetTime();
log.AppendText("Identify End:" + System.DateTime.Now.TimeOfDay.ToString() + "\n");
IdentifyTime.Text = "Identify Time:" + (b - a).ToString() + "ms";
}
运行效果
/// <summary>
/// 解码二维码
/// </summary>
/// <param name="barcodeBitmap">待解码的二维码图片</param>
/// <returns>扫码结果</returns>
public static string DecodeQrCode(Bitmap barcodeBitmap)
{
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
var result = reader.Decode(barcodeBitmap);
return (result == null) ? null : result.Text;
}
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static Bitmap Generate2DBarcode(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 = 1//设置二维码的边距,单位不是固定像素
};
writer.Options = options;
Bitmap map = writer.Write(text);
return map;
}
private void Btn_Create2Dbarcode_Click(object sender, EventArgs e)
{
BarcodePicture.Image = null;
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start(); // 开始监视代码
Bitmap bmap=BarcodeHelper.Generate2DBarcode(CreateBarcode.Text, 100, 100);
stopwatch.Stop(); // 停止监视
BarcodePicture.Image = bmap;
TimeSpan timeSpan = stopwatch.Elapsed; // 获取总时间
double milliseconds = timeSpan.TotalMilliseconds; // 毫秒数
CreateTime.Text = "Identify Time:" + timeSpan.TotalMilliseconds + "ms";
/* Bitmap bmp = BarcodeHelper.Generate2DBarcode("123456", 100, 100);
//保存到磁盘文件
bmp.Save("C:/1.bmp");
bmp.Dispose();*/
}
private void Btn_Identify2Dbarcode_Click(object sender, EventArgs e)
{
log.Clear();
log.AppendText("Identify Start:"+System.DateTime.Now.TimeOfDay.ToString()+"\n");
uint a = timeGetTime();
IdentifyBarcode.Text = BarcodeHelper.DecodeQrCode((Bitmap)BarcodePicture.Image);
//IdentifyBarcode.Text = BarcodeHelper.DecodeQrCode(BarcodeHelper.Generate2DBarcode("123", 100, 100));
uint b = timeGetTime();
log.AppendText("Identify End:" + System.DateTime.Now.TimeOfDay.ToString() + "\n");
IdentifyTime.Text = "Identify Time:" + (b - a).ToString() + "ms";
}
经过测试,在程序第一次生成和解析时,需要初始化(据我判断)时间会略长,但是之后速度很快,生成时间在5ms之内,解析时间在2ms之内,具体取决于实际应用。
最关键点在于图像。
备注:目前示例代码中只有2D QR条码的生成和解析。后续方法可以参考项目中的BarcodeHelper.cs
下载地址
我的Gitee下载地址:https://gitee.com/PErobin/Barcode-ZXing.git
官方Github地址:https://github.com/zxing/zxing
参考博客
ZXing的介绍和方法参数:https://www.jianshu.com/p/6607e69b1121
ZXing使用全解析,基于ZXing3.1:https://blog.csdn.net/dodod2012/article/details/51315112
该篇博客基于github提供介绍和使用:https://www.cnblogs.com/hnsongbiao/p/9145285.html
ZXing使用详解与范例(C#)的更多相关文章
- PHP中header用法详解带范例(转)
header的用法 header()函数的作用是:发送一个原始 HTTP 标头[Http Header]到客户端.标头 (header) 是服务器以 HTTP 协义传 HTML 资料到浏览器前所送出的 ...
- ZXing开发详解
博客转载自:https://blog.csdn.net/skillcollege/article/details/38852183 什么是Z*? 在Android平台做过二维码相关模块的肯定都熟知ZX ...
- TestNG并发执行用例详解和范例
前言 TestNG有多种并发方式支持,方法的并发,class级的并发,test级的并发等:根据实际应用可以灵活的配置和使用,下面分别对几种并发方法进行说明: 一.方法级并发 方法级并发即method级 ...
- mongodb 3.2 yaml 配置详解及范例
mongodb3.x版本后就是要yaml语法格式的配置文件,下面是yaml配置文件格式如下:官方yaml配置文件选项参考:https://docs.mongodb.org/manual/ ... #c ...
- 3.awk数组详解及企业实战案例
awk数组详解及企业实战案例 3.打印数组: [root@nfs-server test]# awk 'BEGIN{array[1]="zhurui";array[2]=" ...
- Linux下ps命令详解 Linux下ps命令的详细使用方法
http://www.jb51.net/LINUXjishu/56578.html Linux下的ps命令比较常用 Linux下ps命令详解Linux上进程有5种状态:1. 运行(正在运行或在运行队列 ...
- ElasticSearch第四步-查询详解
ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSea ...
- Web.xml详解(转)
这篇文章主要是综合网上关于web.xml的一些介绍,希望对大家有所帮助,也欢迎大家一起讨论. ---题记 一. Web.xml详解: (一) web.xml加载过程(步骤) 首 ...
- scp命令详解
\ svn 删除所有的 .svn文件 find . -name .svn -type d -exec rm -fr {} \; linux之cp/scp命令+scp命令详解 名称:cp 使用权限: ...
随机推荐
- cs244a-Introduction to Computer Networking-Unit1
Unit 1 学习目标: how an application use the Internet The structure of the Internet:The 4 layer model The ...
- vue、iview动态菜单(可折叠)
vue项目与iview3实现可折叠动态菜单. 菜单实现一下效果: 动态获取项目路由生成动态三级菜单导航 可折叠展开 根据路由name默认打开子目录,选中当前项 自动过滤需要隐藏的路由(例:登陆) 在手 ...
- iptable防火墙原理
iptable防火墙原理 简介 Linux 2.0 ipfs/firewalld Linux 2.2 ipchain/firewall Linux 2.4 iptables/netfilter (ip ...
- centos在无外网情况下,进行yum挂载
- python grobal 的使用方法
写一个功能,运行报错,name 'number' is used prior to global declaration ,查资料梳理一下 因为这个函数需要调用多次,第一次调用的时候,走if语句,后面 ...
- Vim搜索关键字
有以下两种方法 Method 1:/content 默认从上往下查找 只读模式下输入 /content 后回车 按 n 向下查找 按N 向上查找 Method 2:?content 默认从下往上查找 ...
- 生成不带版本的jar包 不影响deploy
1 How to build maven project without version? 工程pom中增加 <project> ... <build> ... <fin ...
- MySQL中添加、修改、删除约束
https://blog.csdn.net/dz77dz/article/details/82119000 主要包含的约束: 非空.唯一.check.not null.默认值.主键.外键
- JAVA笔记18-容器之二增强的for循环(不重要)
JDK1.5增强的for循环(foreach??)
- 操作系统——HugePage
TLB:页表一般都很大,并且存放在内存中,所以处理器引入MMU后,读取指令.数据需要访问两次内存:首先通过查询页表得到物理地址,然后访问该物理地址读取指令.数据.为了减少因为MMU导致的处理器性能下降 ...