c#实现二维码编码过程

不要用编译好的dll文件,要有算法,真心求大神只需实现基本功能即可,不需要复杂的功能... 不要用编译好的dll文件,要有算法,真心求大神
只需实现基本功能即可,不需要复杂的功能
展开
 我来答
xiangjuan314
2016-01-19 · TA获得超过3.3万个赞
知道大有可为答主
回答量:2.9万
采纳率:0%
帮助的人:2744万
展开全部
C# 生成二维码
在C#中直接引用ThoughtWorks.QRCode.dll 类,

ThoughtWorks.QRCode.Codec.QRCodeEncoder encoder = new QRCodeEncoder();
encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字)
encoder.QRCodeScale = 4;//大小(值越大生成的二维码图片像素越高)
encoder.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误)
encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//错误效验、错误更正(有4个等级)
String qrdata = "二维码信息";
System.Drawing.Bitmap bp = encoder.Encode(qrdata.ToString(), Encoding.GetEncoding("GB2312"));
Image image = bp;
pictureBox1.Image = bp;
保存二维码图片:
SaveFileDialog sf = new SaveFileDialog();
sf.Title = "选择保存文件位置";
sf.Filter = "保存图片(*.jpg) |*.jpg|所有文件(*.*) |*.*";
//设置默认文件类型显示顺序
sf.FilterIndex = 1;
//保存对话框是否记忆上次打开的目录
sf.RestoreDirectory = true;
if (sf.ShowDialog() == DialogResult.OK)
{
Image im = this.pictureBox1.Image;
//获得文件路径
string localFilePath = sf.FileName.ToString();
if (sf.FileName != "")
{
string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//获取文件名,不带路径
// newFileName = fileNameExt+DateTime.Now.ToString("yyyyMMdd") ;//给文件名后加上时间
string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf(".")); //获取文件路径,带文件名,不带后缀
string fn = sf.FileName;
pictureBox1.Image.Save(FilePath +"-"+ DateTime.Now.ToString("yyyyMMdd") + ".jpg");

}
}
//解析二维码信息
// QRCodeDecoder decoder = new QRCodeDecoder();
// String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(pictureBox1.Image)));
//this.label3.Text = decodedString;

2、另一种方法,引用ZXing类库。
ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码。目标是能够对QR编码、Data Matrix、UPC的1D条形码进行解码。于此同时,它同样提供 cpp,ActionScript,android,iPhone,rim,j2me,j2se,jruby,C#等方式的类库。zxing类库的作用主 要是解码,是目前开源类库中解码能力比较强的(商业的另说,不过对于动辄成千上万的类库授权费用,的确很值)。

到谷歌code下载相应的代码
1.下载zxing最新的包
到zxing的主页: http://code.google.com/p/zxing/
找到其中的CSharp文件夹,在vs中打开并编译,将obj下debug中的zxing.dll复制并粘帖到你的项目中的bin文件目录下,

右击添加项目引用。将zxing.dll引用到项目中,就可以在需要的地方使用了。
源代码中有两处UTF-8的问题,会导致中文出现乱码(编译.dll之前修改)
其一:com.google.zxing.qrcode.encoder.encoder类中的
internal const System.String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";
此处,将ISO-8859-1改为UTF-8
其二:com.google.zxing.qrcode.decoder.DecodedBitStreamParser类的成员
private const System.String UTF8 = "UTF8";
应将UTF8改为UTF-8

生成代码:
//引用
using com.google.zxing.qrcode;
using com.google.zxing;
using com.google.zxing.common;
using ByteMatrix = com.google.zxing.common.ByteMatrix;
using EAN13Writer = com.google.zxing.oned.EAN13Writer;
using EAN8Writer = com.google.zxing.oned.EAN8Writer;
using MultiFormatWriter = com.google.zxing.MultiFormatWriter;
方法:
string content = "二维码信息";
ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
Bitmap bitmap = toBitmap(byteMatrix);
pictureBox1.Image = bitmap;
SaveFileDialog sFD = new SaveFileDialog();
sFD.Filter = "保存图片(*.png) |*.png|所有文件(*.*) |*.*";
sFD.DefaultExt = "*.png|*.png";
sFD.AddExtension = true;
if (sFD.ShowDialog() == DialogResult.OK)
{
if (sFD.FileName != "")
{
writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);
}

}
解析:
if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
Image img = Image.FromFile(this.openFileDialog1.FileName);
Bitmap bmap;
try
{
bmap = new Bitmap(img);
}
catch (System.IO.IOException ioe)
{
MessageBox.Show(ioe.ToString());
return;
}
if (bmap == null)
{
MessageBox.Show("Could not decode image");
return;
}
LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
com.google.zxing.BinaryBitmap bitmap1 = new com.google.zxing.BinaryBitmap(new HybridBinarizer(source));
Result result;
try
{
result = new MultiFormatReader().decode(bitmap1);
}
catch (ReaderException re)
{
MessageBox.Show(re.ToString());
return;
}

MessageBox.Show(result.Text);

public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)
{
Bitmap bmap = toBitmap(matrix);
bmap.Save(file, format);
}

public static Bitmap toBitmap(ByteMatrix matrix)
{
int width = matrix.Width;
int height = matrix.Height;
Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
}
}
return bmap;
}
AiPPT
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图... 点击进入详情页
本回答由AiPPT提供
Jarhf
2014-04-21 · TA获得超过1862个赞
知道小有建树答主
回答量:1736
采纳率:25%
帮助的人:1011万
展开全部

已传

更多追问追答
追问
visual studio 2013说有什么不兼容
追答
是有两个移动项目,这个你不用管移除就行了,只看QRCodeLib和QRCodeSampleApp
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式