asp.net C#生成和解析二维码的实例代码
程序员文章站
2024-02-28 09:16:58
类库文件我们在文件最后面下载
【thoughtworks.qrcode.dll 就是类库】
使用时需要增加:复制代码 代码如下:using thoughtworks.q...
类库文件我们在文件最后面下载
【thoughtworks.qrcode.dll 就是类库】
使用时需要增加:
复制代码 代码如下:
using thoughtworks.qrcode.codec;
using thoughtworks.qrcode.codec.data;
using thoughtworks.qrcode.codec.util;
主要源代码:
1.生成二维码
复制代码 代码如下:
qrcodeencoder qrcodeencoder = new qrcodeencoder();
string encoding = cboencoding.text;
if (encoding == "byte")
{
qrcodeencoder.qrcodeencodemode = qrcodeencoder.encode_mode.byte;
}
else if (encoding == "alphanumeric")
{
qrcodeencoder.qrcodeencodemode = qrcodeencoder.encode_mode.alpha_numeric;
}
else if (encoding == "numeric")
{
qrcodeencoder.qrcodeencodemode = qrcodeencoder.encode_mode.numeric;
}
try
{
int scale = convert.toint16(txtsize.text);
qrcodeencoder.qrcodescale = scale;
}
catch (exception ex)
{
messagebox.show("invalid size!");
return;
}
try
{
int version = convert.toint16(cboversion.text);
qrcodeencoder.qrcodeversion = version;
}
catch (exception ex)
{
messagebox.show("invalid version !");
}
string errorcorrect = cbocorrectionlevel.text;
if (errorcorrect == "l")
qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction.l;
else if (errorcorrect == "m")
qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction.m;
else if (errorcorrect == "q")
qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction.q;
else if (errorcorrect == "h")
qrcodeencoder.qrcodeerrorcorrect = qrcodeencoder.error_correction.h;
image image;
string data = txtencodedata.text;
image = qrcodeencoder.encode(data);
pb_view.image = image;
2.解析二维码
复制代码 代码如下:
qrcodedecoder decoder = new qrcodedecoder();
string decodedstring = decoder.decode(new qrcodebitmapimage(new bitmap(pb_view.image)));
txtencodedata.text = decodedstring;