C#利用zxing.net操作二维码和条形码

下载地址:http://zxingnet.codeplex.com/

zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便。

首先下载二进制dll文件,引入工程;

代码:

C#代码  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using ZXing.QrCode;
  9. using ZXing;
  10. using ZXing.Common;
  11. using ZXing.Rendering;
  12. namespace zxingTest
  13. {
  14. public partial class Form1 : Form
  15. {
  16. EncodingOptions options = null;
  17. BarcodeWriter writer = null;
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. options = new QrCodeEncodingOptions
  22. {
  23. DisableECI = true,
  24. CharacterSet = "UTF-8",
  25. Width = pictureBoxQr.Width,
  26. Height = pictureBoxQr.Height
  27. };
  28. writer = new BarcodeWriter();
  29. writer.Format = BarcodeFormat.QR_CODE;
  30. writer.Options = options;
  31. }
  32. private void buttonQr_Click(object sender, EventArgs e)
  33. {
  34. if (textBoxText.Text == string.Empty)
  35. {
  36. MessageBox.Show("输入内容不能为空!");
  37. return;
  38. }
  39. Bitmap bitmap = writer.Write(textBoxText.Text);
  40. pictureBoxQr.Image = bitmap;
  41. }
  42. }
  43. }

效果:


 将字符编码时可以指定字符格式;默认为ISO-8859-1英文字符集,但一般移动设备常用UTF-8字符集编码,

可以通过QrCodeEncodingOptions设置编码方式。

如果要生成其他zxing支持的条形码,只要修改BarcodeWriter.Format就可以了。

C#代码  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using ZXing.QrCode;
  9. using ZXing;
  10. using ZXing.Common;
  11. using ZXing.Rendering;
  12. namespace zxingTest
  13. {
  14. public partial class Form1 : Form
  15. {
  16. EncodingOptions options = null;
  17. BarcodeWriter writer = null;
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. options = new EncodingOptions
  22. {
  23. //DisableECI = true,
  24. //CharacterSet = "UTF-8",
  25. Width = pictureBoxQr.Width,
  26. Height = pictureBoxQr.Height
  27. };
  28. writer = new BarcodeWriter();
  29. writer.Format = BarcodeFormat.ITF;
  30. writer.Options = options;
  31. }
  32. private void buttonQr_Click(object sender, EventArgs e)
  33. {
  34. if (textBoxText.Text == string.Empty)
  35. {
  36. MessageBox.Show("输入内容不能为空!");
  37. return;
  38. }
  39. Bitmap bitmap = writer.Write(textBoxText.Text);
  40. pictureBoxQr.Image = bitmap;
  41. }
  42. }
  43. }

效果:


 输入字符串需要符合编码的格式,不然会报错。

解码方式:

C#代码  

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using ZXing.QrCode;
  9. using ZXing;
  10. using ZXing.Common;
  11. using ZXing.Rendering;
  12. namespace zxingTest
  13. {
  14. public partial class Form1 : Form
  15. {
  16. BarcodeReader reader = null;
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. reader = new BarcodeReader();
  21. }
  22. private void Form1_DragEnter(object sender, DragEventArgs e)//当拖放进入窗体
  23. {
  24. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  25. e.Effect = DragDropEffects.Copy;    //显示拷贝效应
  26. else
  27. e.Effect = DragDropEffects.None;
  28. }
  29. private void Form1_DragDrop(object sender, DragEventArgs e) //当拖放放在窗体上
  30. {
  31. string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, false); //获取文件名
  32. if (fileNames.Length > 0)
  33. {
  34. pictureBoxPic.Load(fileNames[0]);   //显示图片
  35. Result result = reader.Decode((Bitmap)pictureBoxPic.Image); //通过reader解码
  36. textBoxText.Text = result.Text; //显示解析结果
  37. }
  38. }
  39. }
  40. }

时间: 2024-07-30 23:40:04

C#利用zxing.net操作二维码和条形码的相关文章

转--C#利用ZXing.NET操作二维码和条形码

zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便. 首先下载二进制dll文件,引入工程: 代码: C#代码   using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using ZXing.QrCod

[Python]在Windows系统中使用ZXing模块实现二维码、条形码读码

??需要实现Python读取图片中二维码.条形码信息.前段时间研究使用zbarlight模块,费了很大功夫安装调试好,但是发现有些图片读取不正确,而且如果图片中二维码倾斜,就读取不了,不能满足要求.昨天琢磨着试一试ZXing,下载ZXing模块安装后,却一直报错.打开模块源码仔细分析,原来该模块是通过调用java程序,使用ZXing的java库来实现的,通过分析命令行输出得到解码结果.忙活了一天多,各种测试.查资料,终于解决了问题.调试过程非常艰辛,现将做法整理如下(Windows 10系统):

【VB.NET】利用 ZXing.Net 生成二维码(支持自定义LOGO)

ZXing .NET 的项目主页https://zxingnet.codeplex.com/ 代码基本上抄袭自下面两篇文章 XDhttp://www.cnblogs.com/tianma3798/p/5426869.htmlhttp://www.cnblogs.com/tianma3798/p/5426880.html 仅作参数优化,更加实用和简便一点 Shared Function MakeQR(ByVal qrtext As String, Optional ByVal width As I

利用ZXing插件生成二维码

using System.Drawing; using ZXing; using ZXing.QrCode; /// <summary> /// 生成二维码 /// </summary> /// <param name="dirPath">路径</param> /// <returns></returns> private string GenerateQRCode(string dirPath) { Barcod

Android ZXing 二维码、条形码扫描介绍

本帖最后由 Shims 于 2013-11-9 12:39 编辑 最近公司的Android项目需要用到摄像头做条码或二维码的扫描,Google一下,发现一个开源的 ZXing项目.它提供二维码和条形码的扫描.扫描条形码就是直接读取条形码的内容,扫描二维码是按照自己指定的二维码格式进行编码和解码. 1.什么是二维码和条形码?                          二维条形码最早发明于日本,它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代

C#利用QrCode.Net生成二维码(Qr码)

现在网上很多应用都是用二维码来分享网址或者其它的信息.尤其在移动领域,二维码更是有很大的应用场景.因为项目的需要,需要在网站中增加一个生成二维码分析网址的功能,在谷歌大幅度抽筋的情况下无奈使用百度.百度N多,找到一些项目,但是可用性不强.(有一个项目是用VS2005开发的,在2010中调试不开.)终于在codeplex上找到一个“神器”,这个“神器”可以很方便的生成二维码,速度那是相当的快,并且可支持中文,遵从MIT协议. QrCode.Net是一个使用C#编写的用于生成二维码图片的类库,使用它

二维码、条形码扫描——使用Google ZXing

我在项目中用到了二维码扫描的技术,用的是Google提供的ZXing开源项目,它提供二维码和条形码的扫描.扫描条形码就是直接读取条形码的内容,扫描二维码是按照自己指定的二维码格式进行编码和解码. 可以到http://code.google.com/p/zxing/下载ZXing项目的源码,然后按照官方文档进行开发,我这里使用的ZXing是经过简化版的,去除了一些一般使用不必要的文件,项目工程截图如下: 其中encoding包是我在它的基础上自己加上去的,功能是根据传入的字符串来生成二维码图片,返

二维码之zxing仿新浪微博二维码

在前言中最后部分,提到了二维码开发工具资源ZXing.网上有它最新1.7版的源码,感兴趣的可以下载下来看看,要打包生成core比较麻烦,网上有相关教程.嫌麻烦的朋友,可以去我的资源里下载Java版的core.jar,地址前言最后已经给出. 今天开始介绍利用android生成普通二维码,以及仿新浪微博二维码.话说新浪微博也是采用了ZXing的技术,而腾讯微信,我推测它好像是通过服务器生成后下载下来的.因为每次生成二维码,如果没有网络的情况下就无法得到.补一句:因为都是java开发语句,所以开发j2

iOS7+ 扫描二维码和条形码实现 耗时操作

iOS7以前都是用Zxing Zbar 来实现 扫描二维码以及条形码,从iOS7 出来后,系统自带API很强大,渐渐也就放弃了Zxing Zbar的使用, 直接上主要代码: - (void)setupCamera { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [HUDUtils showHUDProgress:@""]; AVAuthorizationStatus a