当前位置:网站首页>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.
边栏推荐
- Do you know the scope and process of software project acceptance testing?
- Starting Oracle under Linux
- 防火墙基础之策略部署
- HMS core news industry solution: let technology add humanistic temperature
- What is the difference between Z-score and deltf/f?
- 定金预售的规则思路详解
- Screenshot of the uniapp app and save it locally
- MySQL如何让一个表中可以有多个自增列
- “不敢去懷疑代碼,又不得不懷疑代碼”記一次網絡請求超時分析
- Some common SQL (version 05 and above) database maintenance scripts
猜你喜欢

Tables converting to latex format

防火墙基础之策略部署

Stephencovey's tips for efficient work for young people

Problème de sous - séquence / substrat leetcode

My suggestions on SAP ABAP transformation

Offline physical stores combined with VR panorama make virtual shopping more realistic

Configuring cplex12.4 tutorial in VS2010

如何保护WordPress网站免受网络攻击?采取安全措施至关重要

Kubernetes monitoring: grafana adds datasource and dashboard through automation

Leetcode interval DP
随机推荐
七牛云上传图片
HW is around the corner. Can't you read the danger message?
微服务测试效率治理
Interpretation of the thesis -- factorization meets the neighborhood: a multifaceted collaborative filtering model
HMS Core新闻行业解决方案:让技术加上人文的温度
Interaction between awk language and Oracle database for nearly half a year
Locks in MySQL
Oracle stored procedure 2
JSP based library management system, including source code, database script, video tutorial for project operation, and video tutorial for thesis writing
leetcode-背包问题
毕业论文写作中致谢词的常见写法及优秀范文
Seven cattle cloud upload picture
Tasks and responsibilities of the test team and basic concepts of testing
History of hash index design
Leetcode math problems
Leetcode union search set
Z-Score和deltf/f有什么区别?
Eureka的InstanceInfoReplicator类(服务注册辅助类)
Oracle cursor
Redis+Caffeine两级缓存的实现