C# QRCode、DataMatrix和其他条形码的生成和解码软件
今天制造了一个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和其他条形码的生成和解码软件的更多相关文章
- C#-利用ZPL语言完毕条形码的生成和打印
近期由于公司项目的须要,研究了一项对我来说算是新的技术-条形码的生成和打印.由于之前没有接触过这方面的知识,所以刚開始还有点小迷茫和小兴奋,只是一步一步来,问题总会解决的.如今来总结一下做条形码 ...
- C#二维码生成与解码(二)
本文内容在<C#二维码生成与解码>的基础上增加了纠错级别和Logo图标加入,增加了二维码的功能.关于透明度在这里没有单独显现,因为在颜色里面就已经包含,颜色值由8位8进制构成,最前面的两位 ...
- Java二维码生成与解码
基于google zxing 的Java二维码生成与解码 一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> &l ...
- C#实现二维码生成与解码
前几天公司内部分享了一个关于二维码的例子,觉得挺好玩的,但没有提供完整的源码.有时候看到一个好玩的东西,总想自己Demo一个,于是抽空就自己研究了一下. 一.二维码的原理 工欲善其事,必先利其器.要生 ...
- HTML-DEV-ToolLink(常用的在线字符串编解码、代码压缩、美化、JSON格式化、正则表达式、时间转换工具、二维码生成与解码等工具,支持在线搜索和Chrome插件。)
HTML-DEV-ToolLink:https://github.com/easonjim/HTML-DEV-ToolLink 常用的在线字符串编解码.代码压缩.美化.JSON格式化.正则表达式.时间 ...
- C#二维码与条形码的生成
二维码 using Gma.QrCodeNet.Encoding;using Gma.QrCodeNet.Encoding.Windows.Render; string str = "Htt ...
- QRCode - 二维码识别与生成
来源:Yi'mouleng(@丶伊眸冷) 链接:http://t.cn/R40WxcM 前言 有关二维码的介绍,我这里不做过多说明, 可以直接去基维百科查看,附上链接QR code(https://e ...
- winfrom 实现条形码批量打印以及将条形码信息生成PDF文件
最近,老大让给客户做个邮包管理程序.其中,包括一些基本信息的增.删.查和改,这些倒不是很难搞定它分分钟的事.其主要难点就在于如何生成条形码.如何批量打印条形码以及将界面条形码信息批量生成以其各自的 b ...
- WEB H5 JS QRCode二维码快速自动生成
万能的GITHUB: https://github.com/davidshimjs/qrcodejs HTML: <div class="col-xs-10 col-xs-offset ...
随机推荐
- 李洪强iOS开发Swift篇---12_NSThread线程相关简单说明
李洪强iOS开发Swift篇---12_NSThread线程相关简单说明 一 说明 1)关于多线程部分的理论知识和OC实现,在之前的博文中已经写明,所以这里不再说明. 2)该文仅仅简单讲解NSThre ...
- POJ3984 迷宫问题
典型BFS. #include <iostream> #include <memory.h> #include <queue> #include <map&g ...
- 最简单的CRC32源码-查表法
这个算法是在逐BYTE法的基础上进行修改的,在上一篇文章里我们说过,如果不查表的话,逐BYTE法和逐BIT法没什么区别,现在我们就把这个算法写出来,注意在调用CRC校验函数前需要先调用表生成函数: u ...
- 值得推荐的C/C++框架和库 very good
[本文系外部转贴,原文地址:http://coolshell.info/c/c++/2014/12/13/c-open-project.htm]留作存档 下次造轮子前先看看现有的轮子吧 值得学习的C语 ...
- HTML Jquery;marquee标签
在<网页制作Dreamweaver(悬浮动态分层导航)>中,运用到了jQuery的技术,轻松实现了菜单的下拉.显示.隐藏的效果,不必再用样式表一点点地修改,省去了很多麻烦,那么jQuery ...
- bzoj1458
题做多的话不难想到可能是以行列作为二分图两个点集,然后网络流相关 具体怎么弄呢 我们可以用求补集的思想,假设有解 我们先把棋盘能放的地方放满士兵,然后我们尽量的把士兵拿走 并且要满足行和列的要求, 说 ...
- Android 在webView中创建web应用(译文)
如果你想在客户端添加一个web应用程序或者仅仅一个web页面,你可以通过使用WebView,WebView是基于android中View的扩展,能够在Activity的layout中实现显示网页,它不 ...
- 《C语言程序设计现代方法》第4章 编程题
1 编写一个程序,要求用户输入一个两位数,然后按数位的逆序打印出这个数. 方法一:没技术含量的 #include <stdio.h> int main() { int high, low; ...
- [SAM4N学习笔记]UART的使用
一.准备工作: 将上一节搭建的工程复制一份,命名为"3.uart".这一节主要讲如何使用SAM4N的UART功能,实现串口的收发. 二.程序编写: 细心看数据手册的朋友也 ...
- 【转】谁说Vim不是IDE?(一)
谁说Vim不是IDE?(一) 话说操作系统.编程语言和编辑器似乎是程序员永恒的吐槽话题,技术发展了几十年,大家讨论起这几个“之争”还是充满愤怒.津津乐道.有人描述过Emacs和VI程序员,大家沿着 ...