C#利用zxing.net操作二维码和条形码
下载地址:http://zxingnet.codeplex.com/
zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便。
首先下载二进制dll文件,引入工程;
代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using ZXing.QrCode;
- using ZXing;
- using ZXing.Common;
- using ZXing.Rendering;
- namespace zxingTest
- {
- public partial class Form1 : Form
- {
- EncodingOptions options = null;
- BarcodeWriter writer = null;
- public Form1()
- {
- InitializeComponent();
- options = new QrCodeEncodingOptions
- {
- DisableECI = true,
- CharacterSet = "UTF-8",
- Width = pictureBoxQr.Width,
- Height = pictureBoxQr.Height
- };
- writer = new BarcodeWriter();
- writer.Format = BarcodeFormat.QR_CODE;
- writer.Options = options;
- }
- private void buttonQr_Click(object sender, EventArgs e)
- {
- if (textBoxText.Text == string.Empty)
- {
- MessageBox.Show("输入内容不能为空!");
- return;
- }
- Bitmap bitmap = writer.Write(textBoxText.Text);
- pictureBoxQr.Image = bitmap;
- }
- }
- }
效果:
将字符编码时可以指定字符格式;默认为ISO-8859-1英文字符集,但一般移动设备常用UTF-8字符集编码,
可以通过QrCodeEncodingOptions设置编码方式。
如果要生成其他zxing支持的条形码,只要修改BarcodeWriter.Format就可以了。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using ZXing.QrCode;
- using ZXing;
- using ZXing.Common;
- using ZXing.Rendering;
- namespace zxingTest
- {
- public partial class Form1 : Form
- {
- EncodingOptions options = null;
- BarcodeWriter writer = null;
- public Form1()
- {
- InitializeComponent();
- options = new EncodingOptions
- {
- //DisableECI = true,
- //CharacterSet = "UTF-8",
- Width = pictureBoxQr.Width,
- Height = pictureBoxQr.Height
- };
- writer = new BarcodeWriter();
- writer.Format = BarcodeFormat.ITF;
- writer.Options = options;
- }
- private void buttonQr_Click(object sender, EventArgs e)
- {
- if (textBoxText.Text == string.Empty)
- {
- MessageBox.Show("输入内容不能为空!");
- return;
- }
- Bitmap bitmap = writer.Write(textBoxText.Text);
- pictureBoxQr.Image = bitmap;
- }
- }
- }
效果:

输入字符串需要符合编码的格式,不然会报错。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using ZXing.QrCode;
- using ZXing;
- using ZXing.Common;
- using ZXing.Rendering;
- namespace zxingTest
- {
- public partial class Form1 : Form
- {
- BarcodeReader reader = null;
- public Form1()
- {
- InitializeComponent();
- reader = new BarcodeReader();
- }
- private void Form1_DragEnter(object sender, DragEventArgs e)//当拖放进入窗体
- {
- if (e.Data.GetDataPresent(DataFormats.FileDrop))
- e.Effect = DragDropEffects.Copy; //显示拷贝效应
- else
- e.Effect = DragDropEffects.None;
- }
- private void Form1_DragDrop(object sender, DragEventArgs e) //当拖放放在窗体上
- {
- string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, false); //获取文件名
- if (fileNames.Length > 0)
- {
- pictureBoxPic.Load(fileNames[0]); //显示图片
- Result result = reader.Decode((Bitmap)pictureBoxPic.Image); //通过reader解码
- textBoxText.Text = result.Text; //显示解析结果
- }
- }
- }
- }
C#利用zxing.net操作二维码和条形码的更多相关文章
- 基于Asp.Net Core,利用ZXing来生成二维码的一般流程
本文主要介绍如何在.net环境下,基于Asp.Net Core,利用ZXing来生成二维码的一般操作.对二维码工作原理了解,详情见:https://blog.csdn.net/weixin_36191 ...
- 【VB.NET】利用 ZXing.Net 生成二维码(支持自定义LOGO)
有任何疑问请去我的新博客提出 https://blog.clso.fun/posts/2019-03-03/vb-net-zxing-net-qr-maker.html ZXing .NET 的项目主 ...
- C# .Net 使用zxing.dll生成二维码,条形码
public static string GetBarcode(string format, string value, int? width, int? height) { ...
- Android ZXing 二维码、条形码扫描介绍
本帖最后由 Shims 于 2013-11-9 12:39 编辑 最近公司的Android项目需要用到摄像头做条码或二维码的扫描,Google一下,发现一个开源的 ZXing项目.它提供二维码和条形码 ...
- 使用zxing生成解析二维码
1. 前言 随着移动互联网的发展,我们经常在火车票.汽车票.快餐店.电影院.团购网站以及移动支付等各个场景下见到二维码的应用,可见二维码以经渗透到人们生活的各个方面.条码.二维码以及RFID被人们应用 ...
- 二维码、条形码扫描——使用Google ZXing
我在项目中用到了二维码扫描的技术,用的是Google提供的ZXing开源项目,它提供二维码和条形码的扫描.扫描条形码就是直接读取条形码的内容,扫描二维码是按照自己指定的二维码格式进行编码和解码. 可以 ...
- 二维码之zxing仿新浪微博二维码
在前言中最后部分,提到了二维码开发工具资源ZXing.网上有它最新1.7版的源码,感兴趣的可以下载下来看看,要打包生成core比较麻烦,网上有相关教程.嫌麻烦的朋友,可以去我的资源里下载Java版的c ...
- C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码(二)
1.使用ZXint.Net生成带logo的二维码 /// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name ...
- Android二维码开源项目zxing用例简化和生成二维码、条形码
上一篇讲到:Android二维码开源项目zxing编译,编译出来后有一个自带的測试程序:CaptureActivity比較复杂,我仅仅要是把一些不用的东西去掉,用看起来更方便,二维码和条形码的流行性自 ...
随机推荐
- Mysql 存储过程-转载
存储过程简介 SQL语句需要先编译然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给定参数(如果该存储 ...
- NodeJS之express的路由浅析
路由路径和请求方法一起定义了请求的端点,它可以是字符串.字符串模式或者正则表达式.后端在获取路由后,可通过一系列类似中间件的函数去执行事务. 可使用字符串的路由路径: // 匹配根路径的请求 app. ...
- IoC基础例子
一个简单的例子: 一般新建一个com.dao包,存放一些dao接口. com.dao.impl里面存放具体的dao com.service存放service接口 com.service.impl具体的 ...
- 20165202 Mypwd
实现mypwd(选做,加分) 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd 提交过程博客的链接 实现过程 使用m ...
- firefox下reset()不好使的问题
最近在测试项目时发现,在firefox下,form.reset()方法对于隐藏的<input>等不起效果,导致程序中出现了错误,以下面为例: js代码: document.agentFor ...
- cocos2d-x移植:xcode到eclipse
xcode程序移植到eclipse 必要组件: 1.macos gcc编译器,若没有,在xcode->preference->downloads中下载command line tools( ...
- Mathematica 迭代函数
学习Mathematica迭代函数的几个画图例子: 1.三角形沿着某一点旋转 verticse = {{0, 0}, {1, 0}, {1/2, Sqrt[3]/2}}; tri = Line[ver ...
- Python GIL 系列之再谈Python的GIL
1. 之前写过一篇<通过实例认识Python的GIL>的文章,感觉有些意犹未尽 2. 这次对例子作了些扩展,进一步的分析GIL对Python程序的影响 2.1 先来看例子: [python ...
- HandBrake 开源视频转码器、编码转换器、格式转换器
HandBrake 开源视频转码器.编码转换器.格式转换器 点击下图进入官网下载页面:https://handbrake.fr/downloads.php macOS 下可能会阻止安装! 其实也不是安 ...
- magento如何在首页显示产品
1.首先现在magento后台创建一个新的分类,记下这个分类的 ID 号码.使用这个新建的分类来管理你的首页产品,这个分类设置为前台不可见.这样就不会显示在你的分类菜单中了,但使用代码调用的时候却会显 ...