今天制造了一个C#的软件,具体是用于生成二维码和条形码的,包括常用的QRCode、DataMatrix、Code128、EAN-8等等。

使用的第三方类库是Zxing.net和DataMatrix.net;另外,程序中也用了委托。

(这两个类库可以在VS2015的NuGet管理器中下载)

但说明一下,其中发现PDF_417是有问题,具体问题是生成的条形码,再解析为字符串时,前面多了“A ”,也不知道是什么原因,反正我很少用到PDF_417,就算了,但其他的编码已经测试过是没问题的。

效果图:

使用非常简单,用法是:从ListView中选择编码类型,然后在Content中输入内容,点击“Encode”则在PictureBox中生成编码,下方的save可以保存图片,open则是打开本地的编码图片,点击“Decode”后即可自动解码。

下面直接上代码(第一段代码是Helper,后面的代码如果不感兴趣可以忽略):

(1)CodeHelper

 using DataMatrix.net;
using System.Collections.Generic;
using System.Drawing;
using ZXing;
using ZXing.Aztec;
using ZXing.Common;
using ZXing.OneD;
using ZXing.PDF417;
using ZXing.QrCode; namespace CodeHelper
{
public class CodeHelper
{
#region 编码
public static Bitmap Encode_QR(string content, int width = , int margin = )
{
QrCodeEncodingOptions opt = new QrCodeEncodingOptions();
opt.DisableECI = true;
opt.CharacterSet = "UTF-8";
opt.Width = width;
opt.Height = width;
opt.Margin = margin; BarcodeWriter wr = new BarcodeWriter();
wr.Options = opt;
wr.Format = BarcodeFormat.QR_CODE; Bitmap bm = wr.Write(content);
return bm;
} public static Bitmap Encode_DM(string content, int moduleSize = , int margin = )
{
DmtxImageEncoderOptions opt = new DmtxImageEncoderOptions();
opt.ModuleSize = moduleSize;
opt.MarginSize = margin; DmtxImageEncoder encoder = new DmtxImageEncoder(); Bitmap bm = encoder.EncodeImage(content, opt);
return bm;
} public static Bitmap Encode_PDF_417(string content, int width = , int margin = )
{
PDF417EncodingOptions opt = new PDF417EncodingOptions();
opt.Width = width;
opt.Margin = margin;
opt.CharacterSet = "UTF-8"; BarcodeWriter wr = new BarcodeWriter();
wr.Options = opt;
wr.Format = BarcodeFormat.PDF_417; Bitmap bm = wr.Write(content);
return bm;
} public static Bitmap Encode_AZTEC(string content, int width = , int margin = )
{
AztecEncodingOptions opt = new AztecEncodingOptions();
opt.Width = width;
opt.Height = width;
opt.Margin = margin; BarcodeWriter wr = new BarcodeWriter();
wr.Options = opt;
wr.Format = BarcodeFormat.AZTEC; Bitmap bm = wr.Write(content);
return bm;
} public static Bitmap Encode_Code_128(string content, int heigt = , int margin = )
{
Code128EncodingOptions opt = new Code128EncodingOptions();
opt.Height = heigt;
opt.Margin = margin; BarcodeWriter wr = new BarcodeWriter();
wr.Options = opt;
wr.Format = BarcodeFormat.CODE_128; Bitmap bm = wr.Write(content);
return bm;
} public static Bitmap Encode_Code_39(string content, int height = , int margin = )
{
EncodingOptions encodeOption = new EncodingOptions();
encodeOption.Height = height;
encodeOption.Margin = margin; BarcodeWriter wr = new BarcodeWriter();
wr.Options = encodeOption;
wr.Format = BarcodeFormat.CODE_39; Bitmap bm = wr.Write(content);
return bm;
} public static Bitmap Encode_EAN_8(string content, int height = , int margin = )
{
EncodingOptions encodeOption = new EncodingOptions();
encodeOption.Height = height;
encodeOption.Margin = margin; BarcodeWriter wr = new BarcodeWriter();
wr.Options = encodeOption;
wr.Format = BarcodeFormat.EAN_8; Bitmap bm = wr.Write(content);
return bm;
} public static Bitmap Encode_EAN_13(string content, int height = , int margin = )
{
EncodingOptions encodeOption = new EncodingOptions();
encodeOption.Height = height;
encodeOption.Margin = margin; BarcodeWriter wr = new BarcodeWriter();
wr.Options = encodeOption;
wr.Format = BarcodeFormat.EAN_13; Bitmap bm = wr.Write(content);
return bm;
}
#endregion #region 编码重载
public static Bitmap Encode_QR(string content)
{
return Encode_QR(content, , );
} public static Bitmap Encode_DM(string content)
{
return Encode_DM(content, , );
} public static Bitmap Encode_PDF_417(string content)
{
return Encode_PDF_417(content, , );
} public static Bitmap Encode_AZTEC(string content)
{
return Encode_AZTEC(content, , );
} public static Bitmap Encode_Code_128(string content)
{
return Encode_Code_128(content, , );
} public static Bitmap Encode_Code_39(string content)
{
return Encode_Code_39(content, , );
} public static Bitmap Encode_EAN_8(string content)
{
return Encode_EAN_8(content, , );
} public static Bitmap Encode_EAN_13(string content)
{
return Encode_EAN_13(content, , );
}
#endregion /// <summary>
/// 全部编码类型解码
/// </summary>
/// <param name="bm"></param>
/// <returns></returns>
public static string Decode(Bitmap bm)
{
DecodingOptions opt = new DecodingOptions();
opt.PossibleFormats = new List<BarcodeFormat>()
{
BarcodeFormat.QR_CODE,
BarcodeFormat.DATA_MATRIX,
BarcodeFormat.PDF_417,
BarcodeFormat.AZTEC,
BarcodeFormat.CODE_128,
BarcodeFormat.CODE_39,
BarcodeFormat.EAN_8,
BarcodeFormat.EAN_13
};
opt.CharacterSet = "UTF-8"; BarcodeReader reader = new BarcodeReader();
reader.Options = opt;
Result rs = reader.Decode(bm);
if (rs != null)
{
return rs.Text;
} //DM
DmtxImageDecoder decoder = new DmtxImageDecoder();
List<string> list = decoder.DecodeImage(bm);
if (list.Count > )
{
return list[];
} return "";
}
}
}

(2)Codes类:

 using System.Collections.Generic;
using System.Drawing;
using ZXing; namespace CodeHelper
{
public class Code
{
public string Name { get; set; } public BarcodeFormat Type { get; set; } public delegateGetBm GetBm { get; set; }
} public delegate Bitmap delegateGetBm(string content); public class Codes
{
public Dictionary<int, Code> list { get; set; } public Codes()
{
list = new Dictionary<int, Code>();
list.Add(, new Code { Name = "QR_CODE", Type = BarcodeFormat.QR_CODE, GetBm = CodeHelper.Encode_QR });
list.Add(, new Code { Name = "DATA_MATRIX", Type = BarcodeFormat.DATA_MATRIX, GetBm = CodeHelper.Encode_DM });
list.Add(, new Code { Name = "PDF_417", Type = BarcodeFormat.PDF_417, GetBm = CodeHelper.Encode_PDF_417 });
list.Add(, new Code { Name = "AZTEC", Type = BarcodeFormat.AZTEC, GetBm = CodeHelper.Encode_AZTEC });
list.Add(, new Code { Name = "CODE_128", Type = BarcodeFormat.CODE_128, GetBm = CodeHelper.Encode_Code_128 });
list.Add(, new Code { Name = "CODE_39", Type = BarcodeFormat.CODE_39, GetBm = CodeHelper.Encode_Code_39 });
list.Add(, new Code { Name = "EAN_8", Type = BarcodeFormat.EAN_8, GetBm = CodeHelper.Encode_EAN_8 });
list.Add(, new Code { Name = "EAN_13", Type = BarcodeFormat.EAN_13, GetBm = CodeHelper.Encode_EAN_13 });
}
}
}

(3)主界面的后台代码:

 using System;
using System.Drawing;
using System.Windows.Forms; namespace CodeHelper
{
public partial class FrmMain : Form
{
public Codes Codes = new Codes(); public FrmMain()
{
InitializeComponent();
} private void FrmMain_Load(object sender, EventArgs e)
{
BindListViewItem();
lvType.Items[].Selected = true;
} private void btnEncode_Click(object sender, EventArgs e)
{
string content = txtContent.Text; int index = lvType.SelectedItems[].Index;
try
{
pbCode.Image = Codes.list[index].GetBm(content);
}
catch (Exception ex)
{
txtContent.Text = "";
pbCode.Image = null;
MessageBox.Show(ex.Message);
}
} private void btnSave_Click(object sender, EventArgs e)
{
if (pbCode.Image == null)
{
MessageBox.Show("there is no code.");
return;
} bool isSave = true;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "图片保存";
sfd.Filter = @"jpeg|*.jpg|bmp|*.bmp|png|*.png";
sfd.FileName = txtContent.Text; if (sfd.ShowDialog() == DialogResult.OK)
{
string fileName = sfd.FileName.ToString();
if (fileName != "" && fileName != null)
{
string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + ).ToString();
System.Drawing.Imaging.ImageFormat imgformat = null;
if (fileExtName != "")
{
switch (fileExtName)
{
case "jpg":
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case "bmp":
imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case "png":
imgformat = System.Drawing.Imaging.ImageFormat.Gif;
break;
default:
MessageBox.Show("只能保存为: jpg,bmp,png 格式");
isSave = false;
break;
}
}
if (imgformat == null)
{
imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
}
if (isSave)
{
try
{
pbCode.Image.Save(fileName, imgformat);
}
catch
{
MessageBox.Show("保存失败,还没有图片或已经清空图片!");
}
}
}
}
} private void BindListViewItem()
{
lvType.View = View.LargeIcon;
lvType.LargeImageList = imgList;
lvType.BeginUpdate(); for (int i = ; i < Codes.list.Count; i++)
{
ListViewItem lvi = new ListViewItem();
lvi.ImageIndex = i;
lvi.Text = Codes.list[i].Name;
lvType.Items.Add(lvi);
}
lvType.EndUpdate();
} private void btnDecode_Click(object sender, EventArgs e)
{
txtContent.Text = ""; Bitmap bm = (Bitmap)pbCode.Image;
try
{
txtContent.Text = CodeHelper.Decode(bm);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} private void btnOpen_Click(object sender, EventArgs e)
{
txtContent.Text = "";
pbCode.Image = null; OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开图片";
ofd.Filter = @"所有文件|*.*|jpeg|*.jpg|bmp|*.bmp|png|*.png"; if (ofd.ShowDialog() == DialogResult.OK)
pbCode.Image = Image.FromFile(ofd.FileName);
} private void lvType_SelectedIndexChanged(object sender, EventArgs e)
{
pbCode.Image = null;
}
}
}

界面的控件:

Label lblType;
ListView lvType;
Button btnEncode;
Button btnDecode;
PictureBox pbCode;
TextBox txtContent;
Label lblContent;
Button btnSave;
Button btnOpen;
ImageList imgList;

就这样吧,欢迎交流。

C# QRCode、DataMatrix和其他条形码的生成和解码软件的更多相关文章

  1. C#-利用ZPL语言完毕条形码的生成和打印

     近期由于公司项目的须要,研究了一项对我来说算是新的技术-条形码的生成和打印.由于之前没有接触过这方面的知识,所以刚開始还有点小迷茫和小兴奋,只是一步一步来,问题总会解决的.如今来总结一下做条形码 ...

  2. C#二维码生成与解码(二)

    本文内容在<C#二维码生成与解码>的基础上增加了纠错级别和Logo图标加入,增加了二维码的功能.关于透明度在这里没有单独显现,因为在颜色里面就已经包含,颜色值由8位8进制构成,最前面的两位 ...

  3. Java二维码生成与解码

      基于google zxing 的Java二维码生成与解码   一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> &l ...

  4. C#实现二维码生成与解码

    前几天公司内部分享了一个关于二维码的例子,觉得挺好玩的,但没有提供完整的源码.有时候看到一个好玩的东西,总想自己Demo一个,于是抽空就自己研究了一下. 一.二维码的原理 工欲善其事,必先利其器.要生 ...

  5. HTML-DEV-ToolLink(常用的在线字符串编解码、代码压缩、美化、JSON格式化、正则表达式、时间转换工具、二维码生成与解码等工具,支持在线搜索和Chrome插件。)

    HTML-DEV-ToolLink:https://github.com/easonjim/HTML-DEV-ToolLink 常用的在线字符串编解码.代码压缩.美化.JSON格式化.正则表达式.时间 ...

  6. C#二维码与条形码的生成

    二维码 using Gma.QrCodeNet.Encoding;using Gma.QrCodeNet.Encoding.Windows.Render; string str = "Htt ...

  7. QRCode - 二维码识别与生成

    来源:Yi'mouleng(@丶伊眸冷) 链接:http://t.cn/R40WxcM 前言 有关二维码的介绍,我这里不做过多说明, 可以直接去基维百科查看,附上链接QR code(https://e ...

  8. winfrom 实现条形码批量打印以及将条形码信息生成PDF文件

    最近,老大让给客户做个邮包管理程序.其中,包括一些基本信息的增.删.查和改,这些倒不是很难搞定它分分钟的事.其主要难点就在于如何生成条形码.如何批量打印条形码以及将界面条形码信息批量生成以其各自的 b ...

  9. WEB H5 JS QRCode二维码快速自动生成

    万能的GITHUB: https://github.com/davidshimjs/qrcodejs HTML: <div class="col-xs-10 col-xs-offset ...

随机推荐

  1. java学习之线程

    一.线程总述: 线程是java当中一个重要的内容,如果想说线程的话,那我们应该先来讲一下什么是进程. 进程:那么什么是进程呢,进程从字面上来理解就是,正在进行的程序.就比如说我们在windows当中打 ...

  2. zabbix3.0配置邮件报警

    我们部署一套监控软件,报警这一块自然不可或缺,接下来我们看看zabbix如何实现邮件报警.   1.编写发送邮件的脚本 zabbix通脚本发送邮件,遵循的传参格式为: 脚本   收件人  标题  邮件 ...

  3. HDU-2700 Parity

    http://acm.hdu.edu.cn/showproblem.php?pid=2700 题目意思很重要:  //e:是要使字符串中1的个数变成偶数.o:是要使字符串中1的个数变成奇数 Parit ...

  4. Can't initialize OCI. Error -1

    今天使用Toad连接Oracle时出现"Can't initialize OCI. Error -1" 解决方法 因为是刚做的windows 7系统,所以没有设置更改通知的时间 把 ...

  5. windwos server 2008下iis7各种操作

    1.发布一个asp程序带access数据库的 默认server 08是安装了iis7的,但是它默认没支持asp这项,这时你可以直接去控制面板--程序和功能--打开或关闭windows功能(双击打开)- ...

  6. 线程中Join的使用例子

    1.实现Runnbale接口, package 网易若干java;//这个例子共有2个线程,一个是主线程,一个是线程t public class MyThread1 implements Runnab ...

  7. 删除顺序链表中重复的数 (一) leecode

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  8. zz android 系统 makefile文件(Android.mk)组织结构

    Android.mk脚本结构 下面是main.mk文件包含关系,本文档主要说明的就是这些文件里到底做了什么.(这个文件被根目录下的makefile文件包含) 一.     main.mk 1.检查版本 ...

  9. XSLT教程 比较全的 -摘自网络

    XSLT教程 XML文档树 1) XML可以转化文档树 2) XSLT对XML的转化过程 内建模板规则 根 调用<xsl:apply-templates>处理根节点的儿子.处理时,使用调用 ...

  10. 27 Best Free Eclipse Plug-ins for Java Developer to be Productive

    Eclipse offers an integrated development environment having an extensible plug-in system. This enabl ...