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 ...
随机推荐
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
- delphi非IE内核浏览器控件TEmbeddedChrome下载|TEmbeddedChrome代码
下载地址: 点击下载 代码示例: 在TForm的oncreate方法中写入一些代码 procedure TForm1.FormCreate(Sender: TObject); begin Chromi ...
- vs2005 ,2008,2010中引入app.manifest(即c#程序在win7下以管理员权限运行方法)
打开VS2005.VS2008.VS2010工程,查看工程文件夹中的Properties文件夹下是否有app.manifest这个文件:如没有,按如下方式创建:鼠标右击工程在菜单中选择“属性”,点击工 ...
- 【HDOJ】1198 Farm Irrigation
其实就是并查集,写麻烦了,同样的代码第一次提交wa了,第二次就过了. #include <stdio.h> #include <string.h> #define MAXNUM ...
- C#/PHP Compatible Encryption (AES256) ZZ
Finding a way to encrypt messages in C# and decrypting them in PHP or vice versa seems to be a " ...
- Ubuntu下安装Apache2, php5 mysql
不错的博文:http://blog.csdn.net/guaikai/article/details/6905781 1:首先安装apache:打开终端(ctrl+Alt+t), 输入命令:sudo ...
- ARM学习笔记4——加载存储指令
一.字数据传送指令 作用:用于把单一的数据传入或者传出一个寄存器. 1.LDR指令 1.1.作用 根据<addr_mode>所确定的地址模式从内存中将一个32位的字段读取到目标寄存器< ...
- HDOJ/HDU 1015 Safecracker(深搜)
Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle ...
- Android学习笔记(十四)方便实用的首选项-PreferenceActivity
突然发现已经好多天没更新博客了,最近公司项目正在进行一个大跨度的重构,又碰上有新需求,一连好多天都是很晚才到家.其实这篇博文在草稿箱里面也存了很久了,本来想着不发了,不过感觉PreferenceAct ...
- 高性能MySql进化论(十一):常见查询语句的优化
总结一下常见查询语句的优化方式 1 COUNT 1. COUNT的作用 · COUNT(table.filed)统计的该字段非空值的记录行数 · ...