C#中生成二维码(QR码)与读取二维码内容
使用开源类库ZXing.dll可以在C#中生成二维码和解析二维码为指定的字符串(含url)
新建windows窗体应用程序QRCodeDemo,.net 4.5,将默认的Form1重命名为FormQuickResponseCode。
添加对开源类库zxing.dll和zxing.presentation.dll的引用,用于解析和生成条码【一维条码、二维条码均可生成】。
窗体FormQuickResponseCode设计器如下:
新建读写二维码类QrCodeUtil,QrCodeUtil.cs源程序如下:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing;
using ZXing.QrCode;
using ZXing.QrCode.Internal;namespace QRCodeDemo
{/// <summary>/// 文本字符串与二维码图片之间的转换/// 字符串转二维码 String---->Image/// 二维码转字符串 Image---->String/// </summary>public class QrCodeUtil{/// <summary>/// 将文本字符串转化为二维码图片/// </summary>/// <param name="codeContent">打印条码对应的字符串</param>/// <param name="width"></param>/// <param name="height"></param>/// <param name="barcodeFormat">条码格式:CODE_128代表一维条码,QR_CODE代表二维码</param>/// <param name="margin"></param>/// <returns></returns>public static Bitmap GeneateQrCode(string codeContent, int width, int height, BarcodeFormat barcodeFormat, string errorCorrectionLevel, int margin = 1){// 1.设置QR二维码的规格QrCodeEncodingOptions qrEncodeOption = new QrCodeEncodingOptions();qrEncodeOption.ErrorCorrection = GetErrorCorrectionLevel(errorCorrectionLevel);qrEncodeOption.DisableECI = true;qrEncodeOption.CharacterSet = "UTF-8"; // 设置编码格式,否则读取'中文'乱码qrEncodeOption.Height = height;qrEncodeOption.Width = width;qrEncodeOption.Margin = margin; // 设置周围空白边距qrEncodeOption.PureBarcode = true;//System.Windows.Forms.MessageBox.Show("" + qrEncodeOption.ErrorCorrection.Name + "...." + qrEncodeOption.ErrorCorrection.Bits);// 2.生成条形码图片BarcodeWriter wr = new BarcodeWriter();wr.Format = barcodeFormat; // 二维码 BarcodeFormat.QR_CODEwr.Options = qrEncodeOption;Bitmap img = wr.Write(codeContent);return img; }/// <summary>/// 解码二维码,返回一个文本字符串,如果返回为null,则解析(转化)失败/// </summary>/// <param name="barcodeBitmap">待解码的二维码图片</param>/// <returns>扫码结果</returns>public static string DecodeQrCode(Bitmap barcodeBitmap){BarcodeReader reader = new BarcodeReader();reader.Options.CharacterSet = "UTF-8";Result result = reader.Decode(barcodeBitmap);return (result == null) ? null : result.Text;}/// <summary>/// 获取纠错级别/// </summary>/// <param name="name"></param>/// <returns></returns>private static ErrorCorrectionLevel GetErrorCorrectionLevel(string levelName){switch (levelName){case "L"://bits=1return ErrorCorrectionLevel.L;case "M"://bits=0return ErrorCorrectionLevel.M;case "Q"://bits=3return ErrorCorrectionLevel.Q;case "H"://bits=2return ErrorCorrectionLevel.H;default:return ErrorCorrectionLevel.M;}}}
}
窗体FormQuickResponseCode 主要程序如下(忽略设计器自动生成的代码):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace QRCodeDemo
{public partial class FormQuickResponseCode : Form{public FormQuickResponseCode(){InitializeComponent();//矩阵二维码QRCode: Quick Response Code rtxtCodeMessage.Text = "https://blog.csdn.net/ylq1045\n斯内科";this.cboImageFormat.Items.AddRange(new object[] {"GIF","JPG/JPEG","PNG","BMP"});this.cboErrorCorrectionLevel.Items.AddRange(new object[] {"L 7%","M 15%","Q 25%","H 30%"});cboImageFormat.SelectedIndex = 1;cboErrorCorrectionLevel.SelectedIndex = 1;}private void btnGenerateQR_Click(object sender, EventArgs e){if (rtxtCodeMessage.Text.Length == 0){MessageBox.Show("请输入URL或者文本字符串", "提示");return;}string errorCorrectionLevel = "M";if (cboErrorCorrectionLevel.Text.Length > 0){errorCorrectionLevel = cboErrorCorrectionLevel.Text.Substring(0, 1);}Bitmap bitmap = QrCodeUtil.GeneateQrCode(rtxtCodeMessage.Text, 200, 200, ZXing.BarcodeFormat.QR_CODE,errorCorrectionLevel);pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;pictureBox1.Image = bitmap;}private void btnExport_Click(object sender, EventArgs e){SaveFileDialog saveImageDialog = new SaveFileDialog();saveImageDialog.Title = "图片保存";switch (cboImageFormat.Text){case "GIF":saveImageDialog.Filter = "gif|*.gif|All|*.*";break;case "JPG/JPEG":saveImageDialog.Filter = "jpeg|*.jpg|All|*.*";break;case "PNG":saveImageDialog.Filter = "png|*.png|All|*.*";break;case "BMP":saveImageDialog.Filter = "bmp|*.bmp|All|*.*";break;default:saveImageDialog.Filter = "jpeg|*.jpg|bmp|*.bmp|gif|*.gif|png|*.png|All|*.*";break;}if (saveImageDialog.ShowDialog() == DialogResult.OK){System.Drawing.Imaging.ImageFormat imgformat = GetImageFormat(cboImageFormat.Text);string fileName = saveImageDialog.FileName;try{pictureBox1.Image.Save(fileName, imgformat);MessageBox.Show($"导出成功,图片文件路径:\n{fileName}", "导出文件");}catch (Exception ex){MessageBox.Show(ex.Message, "导出文件出错");}}}private void btnOpenFile_Click(object sender, EventArgs e){OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.Filter= "jpeg|*.jpg|bmp|*.bmp|gif|*.gif|png|*.png|All|*.*";if (openFileDialog.ShowDialog() == DialogResult.OK){string fileName = openFileDialog.FileName;try{pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;pictureBox1.Image = Image.FromFile(fileName);}catch (Exception ex){MessageBox.Show(ex.Message, "打开图像文件出错");}}}private void btnDiscern_Click(object sender, EventArgs e){rtxtCodeMessage.Clear();try{string codeMessage = QrCodeUtil.DecodeQrCode(new Bitmap(pictureBox1.Image));if (string.IsNullOrEmpty(codeMessage)){MessageBox.Show($"识别二维码图片出错,可能不是二维码图片格式,或者过于模糊,\n请打开新的二维码图片重新尝试", "识别二维码图片出错");return;}rtxtCodeMessage.Text = codeMessage;}catch (Exception ex){MessageBox.Show(ex.Message, "识别二维码图片出错");}}/// <summary>/// 获取图像文件格式/// </summary>/// <param name="formatName"></param>/// <returns></returns>private System.Drawing.Imaging.ImageFormat GetImageFormat(string formatName){switch (formatName){case "GIF":return System.Drawing.Imaging.ImageFormat.Gif;case "JPG/JPEG":return System.Drawing.Imaging.ImageFormat.Jpeg;case "PNG":return System.Drawing.Imaging.ImageFormat.Png;case "BMP":return System.Drawing.Imaging.ImageFormat.Bmp;default:return System.Drawing.Imaging.ImageFormat.Jpeg;}}}
}
测试运行如图:
C#中生成二维码(QR码)与读取二维码内容相关推荐
- qt中生成含有中文的json文件,读取含有中文的json文件
引言 之前将变量保存并在本地生成json文件,由于其中含有中文,导致生成的json文件出现乱码,或者就是生成的json文件没有乱码,但是读取生成的json文件时出现乱码,不能正常解析json. 示例 ...
- (转)ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果
场景:移动支付需要对二维码的生成与部署有所了解,掌握目前主流的二维码生成技术. 1 ZXing 生成二维码 首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老 ...
- ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果
首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老美开发的,barcode4j对一维条形码处理的很好,而且支持的格式很多,当然也可以对二维码进行处理,效果个人 ...
- jquery-qrcode 生成和读取二维码
首先要导入jar包(生成二维码的jar和读取二维码的jar) 生成二维码: package com.imooc.qrcode;import java.awt.Color; import java.aw ...
- 在VC++中生成伪随机数祥解
摘 要 伪随机数在计算机软件设计中有很广泛的用途.本文介绍了基于数学方法的利用计算机产生伪随机数的一种方法,即线性同余法,任何伪随机数的产生都是运用递推的原理来生成的.以及在Visual C++环境中 ...
- 只需3个步骤,轻松解决程序员在Java中生成、扫描二维码难题
条形码包含有关产品或公司的信息,以机器可读的形式直观地表示.条码广泛用于跟踪货物和库存管理.我们可以在 WPF 应用程序中轻松生成各种类型的条码.二维码广泛用于分享重要信息.对于不同的要求,您可能希望 ...
- PHP合成图片怎么保证清晰度(在phpqrcode中生成带Logo的二维码)
在phpqrcode中生成带Logo的二维码 保持logo的清晰度: PHP合成图片主要用的的函数 : imagecreatefromstring //返回一个图像标识符,其表达了从给定字符串得来 ...
- Visual Studio中使用开源二维码QR库libqr
分享一个轻巧的开源二维码QR库libqr,源码可以从GitHub上下载.下面我们详细说明一下如何在Visual Studio中编译使用. 1. 源码下载 地址:https://github.com/r ...
- Vue中生成二维码的一种方式—vue-qr
Vue中生成二维码的一种方式-vue-qr vue实现二维码生成(vue + vue-qr)
最新文章
- python输出特别的矩阵
- Win系统复制粘贴失效解决办法
- redis集群部署一直卡在Waiting for the cluster to join ......
- 括号匹配问题(0962)
- python核心编程 第八章
- mysql镜像_Mysql phpmyadmin docker镜像安装
- Cookie案例-显示用户的上次访问时间代码实现
- 如何能把 fastdfs-client-java的jar包安装到本地的仓库中
- Java Date实现加一天,年月日类推往后+1,日期+1,月份+1,年份+1
- NFC中国-中国第一NFC论坛,NFC中文论坛+NFC技术社区+NFC_电子发烧友网【申明:来源于网络】...
- 计算机的排除故障的方法,计算机产生故障的原因和排除故障的方法
- fer2013人脸表情数据实践
- 使用wkhtmltopdf将网页转换成pdf文件+前台下载
- 计算机文本自定义,自定义文本编辑器
- phalapi做登录检测_PhalApi框架使用笔记
- c php embed,使用PHP Embed SAPI实现Opcodes查看器
- idea 懒人神器 保存自动格式化 Save Action插件
- 保利威视视频云平台 新版本(Version 1.1.0) 上线通知
- 学习了罗昭锋的文献管理与信息分析的感受
- 运筹学基础:第2章-线性规划建模(习题笔记)