C#二维码生成与解码
【窗体效果图】

【程序源代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using com.google.zxing;
using COMMON = com.google.zxing.common; namespace Qcode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
{
MessageBox.Show("请输入需要转换的信息!");
}
else
{
string content = this.textBox1.Text;//待编码数据
try
{
int QSize = Int32.Parse(textBox2.Text);//二维码大小
string s = hScrollBar1.Value.ToString("X");//二维码透明度
string q = hScrollBar2.Value.ToString("X");//背景透明度
string Scolor = "0x" + s + textBox3.Text;//二维码颜色
string Qcolor = "0x" + q + textBox4.Text;//背景颜色
COMMON.ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QSize, QSize);
Bitmap bt = toBitmap(byteMatrix, Scolor, Qcolor);
pictureBox1.Image = bt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
} public static Bitmap toBitmap(COMMON.ByteMatrix matrix,string scolor,string qcolor)
{
int width = matrix.Width;
int height = matrix.Height;
Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for (int x = ; x < width; x++)
{
for (int y = ; y < height; y++)
{
bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != - ? ColorTranslator.FromHtml(scolor) : ColorTranslator.FromHtml(qcolor));
}
} return bmap;
} private void button3_Click(object sender, EventArgs e)
{
Image img = pictureBox1.Image;
if (img != null)
{
SaveFileDialog sFD = new SaveFileDialog();
sFD.Filter = "*.png|*.png";
if (sFD.ShowDialog() == DialogResult.OK)
{
Bitmap bmap = new Bitmap(img, img.Width, img.Height);
bmap.Save(sFD.FileName);
MessageBox.Show("保存成功!");
}
}
else
{
MessageBox.Show("您还没有生成二维码!");
}
} private void button2_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
Bitmap bmap;
try
{
Image img = Image.FromFile(this.openFileDialog1.FileName);
bmap = new Bitmap(img);
if (bmap == null)
{
MessageBox.Show("解码错误,请确保二维码图片已打开!");
return;
}
}
catch
{
MessageBox.Show("解码错误,请确保图片格式正确!");
return;
} LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new COMMON.HybridBinarizer(source));
Result result;
try
{
result = new MultiFormatReader().decode(bitmap);
}
catch
{
string str = "解码失败,失败原因可能是:"+"\n";
str += "1.您打开的图片非二维码图片!" + "\n";
str += "2.您打开的二维码图片背景色太深!" + "\n";
str += "3.您打开的二维码图片二维码和背景色太接近!" + "\n";
MessageBox.Show(str);
return;
}
textBox1.Text = result.Text;
} }
}
【引用dll文件】
http://pan.baidu.com/s/1ntNr79v
【关注我的博客】
C#二维码生成与解码的更多相关文章
- C#二维码生成与解码(二)
本文内容在<C#二维码生成与解码>的基础上增加了纠错级别和Logo图标加入,增加了二维码的功能.关于透明度在这里没有单独显现,因为在颜色里面就已经包含,颜色值由8位8进制构成,最前面的两位 ...
- Java二维码生成与解码
基于google zxing 的Java二维码生成与解码 一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> &l ...
- HTML-DEV-ToolLink(常用的在线字符串编解码、代码压缩、美化、JSON格式化、正则表达式、时间转换工具、二维码生成与解码等工具,支持在线搜索和Chrome插件。)
HTML-DEV-ToolLink:https://github.com/easonjim/HTML-DEV-ToolLink 常用的在线字符串编解码.代码压缩.美化.JSON格式化.正则表达式.时间 ...
- C#实现二维码生成与解码
前几天公司内部分享了一个关于二维码的例子,觉得挺好玩的,但没有提供完整的源码.有时候看到一个好玩的东西,总想自己Demo一个,于是抽空就自己研究了一下. 一.二维码的原理 工欲善其事,必先利其器.要生 ...
- Java二维码生成与解码工具Zxing使用
Zxing是Google研发的一款非常好用的开放源代码的二维码生成工具,目前源码托管在github上,源码地址: https://github.com/zxing/zxing 可以看到Zxing库有很 ...
- .NET 二维码生成(ThoughtWorks.QRCode)
引用ThoughtWorks.QRCode.dll (源代码里有) 1.简单二维码生成及解码代码: //生成二维码方法一 private void CreateCode_Simple(string n ...
- .NET 二维码生成(ThoughtWorks.QRCode)【转发jiangys】
.NET 二维码生成(ThoughtWorks.QRCode) 2015-06-21 22:19 by jiangys, 3790 阅读, 8 评论, 收藏, 编辑 引用ThoughtWorks.QR ...
- Asp.Net 之 二维码生成
首先,引用 ThoughtWorks.QRCode.dll . 简单二维码生成及解码代码: //生成二维码方法一 private void CreateCode_Simple(string nr) { ...
- simple go web application & 二维码生成 & 打包部署
go语言简易web应用 & 二维码生成及解码 & 打包部署 转载请注明出处: https://www.cnblogs.com/funnyzpc/p/10801476.html 前言(闲 ...
随机推荐
- 表的顺序结构---重写Arraylist类
重写ArrayList类,为防止冲突,重写为MyArrayList,未继承Iterable类. public class MyArrayList<AnyType>{ int N=10; A ...
- HDU 1698 Just a Hook (段树更新间隔)
Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing for m ...
- .NET Framework 4.0 以上版本 下载地址
https://msdn.microsoft.com/zh-cn/library/5a4x27ek(v=vs.110).aspx
- ZenCoding for EmEditor Snippets 的安装
ZenCoding for EmEditor的安装 你可以从这里下载所需文件Library under the Snippets category.安装前请确认你的EmEditor内置有代码片段(Sn ...
- Unity 二战中加飞机
一个简短的引论: 谢意: 本申请中使用<Unity3D\2D移动游戏开发>提供资源.著作权属于作者.感谢作者.基于原始时本申请的二次开发. 要素: 1.增加2s cd的机身旋转,旋转时保持 ...
- iphone6 plus有什么办法
在苹果9月9号最新的秋季公布会上苹果官方公布了最新的iPhone6 plus,可能非常多朋友不知道plus是什么意思,这样命名有什么特殊的意义呢?本次Ly经典家居小编就为大家带来这方面的具体解答,一起 ...
- newlisp 接受jenkins带空格的参数
jenkins有一个参数text种类,它能够以文本的段落拷贝作为参数 newlispThe program receives parameters are separated by spaces, 更 ...
- 7 JavaScript Basics Many Developers Aren't Using (Properly)【转】
JavaScript, at its base, is a simple language that we continue to evolve with intelligent, flexible ...
- linux shell 不同进制数据转换(二进制,八进制,十六进制,base64) (转)
shell可以在不调用第3方命令,表示不同进制数据.这里总结以下表示方法.shell 脚本默认数值是由10 进制数处理,除非这个数字某种特殊的标记法或前缀开头. 才可以表示其它进制类型数值.如:以 0 ...
- [LeetCode] Combinations [38]
称号 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...