当前位置: 移动技术网 > IT编程>开发语言>.net > ZXing.net 生成和解析二维码

ZXing.net 生成和解析二维码

2018年09月12日  | 移动技术网IT编程  | 我要评论

玩mm,广东海洋大学寸金学院教务处,朱秋颖

nuget引用zxing.net包

public partial class form1 : form
{
public form1()
{
initializecomponent();
}

private void button1_click(object sender, eventargs e)
{
this.picturebox1.image = getqrcodebyzxingnet("https://mp.weixin.qq.com/",238,238);
}
/// <summary>
/// 生成二维码图片
/// </summary>
/// <param name="strmessage">要生成二维码的字符串</param>
/// <param name="width">二维码图片宽度</param>
/// <param name="height">二维码图片高度</param>
/// <returns></returns>
private bitmap getqrcodebyzxingnet(string strmessage, int32 width, int32 height)
{
bitmap result = null;
try
{
barcodewriter barcodewriter = new barcodewriter();
barcodewriter.format = barcodeformat.qr_code;
barcodewriter.options.hints.add(encodehinttype.character_set, "utf-8");
barcodewriter.options.hints.add(encodehinttype.error_correction, zxing.qrcode.internal.errorcorrectionlevel.h);
barcodewriter.options.height = height;
barcodewriter.options.width = width;
barcodewriter.options.margin = 0;
zxing.common.bitmatrix bm = barcodewriter.encode(strmessage);
result = barcodewriter.write(bm);
//string filename = @"c:\users\test.png";
//result .save(filename, imageformat.png);
//result .dispose();
}
catch (exception ex)
{
//异常输出
}
return result;
}
/// <summary>
/// 解码二维码
/// </summary>
/// <param name="barcodebitmap">待解码的二维码图片</param>
/// <returns>扫码结果</returns>
private string decodeqrcode(bitmap barcodebitmap)
{
barcodereader reader = new barcodereader();
reader.options.characterset = "utf-8";
var result = reader.decode(barcodebitmap);
return (result == null) ? null : result.text;
}

private void button2_click(object sender, eventargs e)
{
this.label1.text = decodeqrcode((bitmap)this.picturebox1.image);
}
}

 


如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网