当前位置:网站首页>ThoughtWorks.QRCode和ZXing.Net 二维码,网址可以直接跳转

ThoughtWorks.QRCode和ZXing.Net 二维码,网址可以直接跳转

2022-06-22 12:50:00 51CTO

 ThoughtWorks.QRCode和ZXing.Net 二维码,网址可以直接跳转

1、项目引用 ThoughtWorks.QRCode和ZXing.Net 二维码的相关生成类库,管理NuGet程序包搜索添加ThoughtWorks.QRCode和ZXing.Net包

2、添加按钮生成二维码按钮(ThoughtWorks.QRCode和ZXing.Net  两个按钮),添加picturebox显示二维码

3、ThoughtWorks.QRCode和ZXing.Net  两个按钮事件的主要代码如下

ThoughtWorks.QRCode和ZXing.Net 

      
      
#region 二维码
int i = 2;
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnProduct_Click(object sender, EventArgs e)
{
#region ThoughtWorks.QRCode 二维码,网址可以直接跳转
ThoughtWorks.QRCode.Codec.QRCodeEncoder endocder = new ThoughtWorks.QRCode.Codec.QRCodeEncoder();
//二维码背景颜色
endocder.QRCodeBackgroundColor = System.Drawing.Color.White;
//二维码编码方式
endocder.QRCodeEncodeMode = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE;
//每个小方格的宽度
endocder.QRCodeScale = 4;
//二维码版本号
endocder.QRCodeVersion = 5;//控制版本,不同版本,显示出来的样式不一样
//纠错等级
endocder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M;

//将json做成二维码
//序列化对象
//var person = new { Id = i, Name = "66666", Gender = 1, Age = 24 + i++, oid = i, url = "https://www.baidu.com/" };
//using (Bitmap bitmap = endocder.Encode(new JavaScriptSerializer().Serialize(person), System.Text.Encoding.UTF8))
//using (Bitmap bitmap = endocder.Encode(new JavaScriptSerializer().Serialize(person)))

// 链接地址
var person = "http://www.baidu.com/";
using (Bitmap bitmap = endocder.Encode(person))
{
//Bitmap bitmap = endocder.Encode(new JavaScriptSerializer().Serialize(person), System.Text.Encoding.UTF8);
var strSavePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"QRCode{i}.jpg");
//strSavePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"QRCode.jpg");
if (!Directory.Exists(Path.GetDirectoryName(strSavePath)))
{
Directory.CreateDirectory(strSavePath);
}
//System.Runtime.InteropServices.ExternalException:
//使用错误的图像格式保存图像。 - 或 - 图像已保存到同一文件从创建它。出现一般都是保存路经问题,或者aspnet中是读写文件权限问题
bitmap.Save(strSavePath, System.Drawing.Imaging.ImageFormat.Jpeg);
pbxPicture.SizeMode = PictureBoxSizeMode.Zoom;
//pbxQRCode.Image = Bitmap.FromHbitmap(bitmap.GetHbitmap());
//第一种绘图
IntPtr hBitmap = bitmap.GetHbitmap();
pbxQRCode.Image = Bitmap.FromHbitmap(hBitmap);
//pbxQRCode.Image.Save(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"QRCode.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
DeleteObject(hBitmap);
bitmap.Dispose();

//第二种绘图
//Graphics graphics = Graphics.FromImage(bitmap);
//graphics.Clear(Color.White);
////再bitmap上绘图
//graphics.DrawImage(new Bitmap(""), new PointF(0, 0));
//graphics.Dispose();
}


//解密二维码
//ThoughtWorks.QRCode.Codec.QRCodeDecoder qRCodeDecoder = new ThoughtWorks.QRCode.Codec.QRCodeDecoder();
//var decoderResult = qRCodeDecoder.decode(new ThoughtWorks.QRCode.Codec.Data.QRCodeBitmapImage(new Bitmap(strSavePath)));
#endregion

#region ZXing.Net 二维码,网址可以直接跳转
//var textBoxText = "http://106.12.79.39/#/login?redirect=%2Fdashboard";
//textBoxText = "https://www.baidu.com/";
//BarcodeWriter barCodeWriter = new BarcodeWriter();
//barCodeWriter.Format = BarcodeFormat.QR_CODE;
//barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
//barCodeWriter.Options.Height = 230;
//barCodeWriter.Options.Width = 230;
//BitMatrix bm = barCodeWriter.Encode(textBoxText);
//Bitmap img = barCodeWriter.Write(bm);
//pbxQRCode.Image = img;
//pbxQRCode.SizeMode = PictureBoxSizeMode.Zoom;
#endregion

}
private void btnZxing_Click(object sender, EventArgs e)
{
#region ZXing.Net 二维码,网址可以直接跳转 ThoughtWorks.QRCode 二维码
//var textBoxText = "http://106.12.79.39/#/login?redirect=2Fdashboard&ee=6";
//textBoxText = "https://www.baidu.com?redirect=2Fdashboard&ee=6";
//var textBoxText = new { Id = i, Name = "wolfy", Gender = 1, Age = 24 + i++, oid = i, url = "https://www.baidu.com/" };
BarcodeWriter barCodeWriter = new BarcodeWriter();
barCodeWriter.Format = BarcodeFormat.QR_CODE;
barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
barCodeWriter.Options.Height = 230;
barCodeWriter.Options.Width = 230;

//链接地址
//textBoxText = "https://www.baidu.com?redirect=2Fdashboard&ee=6";
//BitMatrix bm = barCodeWriter.Encode(textBoxText);

//序列化对象
//var textBoxText = new { Id = i, Name = "wolfy", Gender = 1, Age = 24 + i++, oid = i, url = "https://www.baidu.com/" };
//BitMatrix bm = barCodeWriter.Encode(new JavaScriptSerializer().Serialize(textBoxText));

//字符串
var textBoxText = "https://www.baidu.com?redirect=2Fdashboard&ee=6";
BitMatrix bm = barCodeWriter.Encode(textBoxText);
Bitmap img = barCodeWriter.Write(bm);
pbxQRCode.Image = img;
pbxQRCode.SizeMode = PictureBoxSizeMode.Zoom;
#endregion
}

/// <summary>
/// 解密二维码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConsume_Click(object sender, EventArgs e)
{
var strSavePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"QRCode{i}.jpg");
//Bitmap bitmap = new Bitmap(new MemoryStream(File.ReadAllBytes(strSavePath)));
using (Bitmap bitmap = new Bitmap(new MemoryStream(File.ReadAllBytes(strSavePath))))
{
strSavePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"QRCode.jpg");
ThoughtWorks.QRCode.Codec.QRCodeDecoder qRCodeDecoder = new ThoughtWorks.QRCode.Codec.QRCodeDecoder();
//var decoderResult = qRCodeDecoder.decode(new ThoughtWorks.QRCode.Codec.Data.QRCodeBitmapImage(new Bitmap(strSavePath)));
var decoderResult = qRCodeDecoder.decode(new ThoughtWorks.QRCode.Codec.Data.QRCodeBitmapImage(bitmap));
lblTakephotoSavePath.Text = $"二维码:{decoderResult}";
}
}
#endregion
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://blog.51cto.com/u_15693505/5407598