Nuget下载依赖
注意
在做答题卡识别的时候不用用二值化的图片进行二维码的识别,识别率会大大下降。
public static Bitmap createQRImage(string str)
{
QrCodeEncodingOptions options = new QrCodeEncodingOptions();
options.CharacterSet = "UTF-8";
options.DisableECI = true; // Extended Channel Interpretation (ECI) 主要用于特殊的字符集。并不是所有的扫描器都支持这种编码。
options.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H; // 纠错级别
options.Width = 300;
options.Height = 300;
options.Margin = 1;
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
Bitmap bmp = writer.Write(str);
return bmp;
}
纠错级别:
L - 约 7% 纠错能力。 M - 约 15% 纠错能力。 Q - 约 25% 纠错能力。 H - 约 30% 纠错能力。
public static Bitmap create128Image(string str)
{
QrCodeEncodingOptions options = new QrCodeEncodingOptions();
options.CharacterSet = "UTF-8";
options.Width = 300;
options.Height = 50;
options.Margin = 1;
options.PureBarcode = false; // 是否是纯码,如果为 false,则会在图片下方显示数字
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.CODE_128;
writer.Options = options;
Bitmap bmp = writer.Write(str);
return bmp;
}
using System.Drawing;
using ZXing;
namespace z.Common
{
public class ZQrcodeUtil
{
public static string readCode(string imagePath)
{
string str = "";
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
using (Bitmap bmp = new Bitmap(imagePath))
{
Result result = reader.Decode(bmp);
str = result.Text;
}
return str;
}
}
}
using System;
using System.Drawing;
using ZXing;
using ZXing.QrCode;
namespace Z.Common
{
public class ZQrcodeUtil
{
/// <summary>
/// 创建二维码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static Bitmap createQRImage(string str)
{
QrCodeEncodingOptions options = new QrCodeEncodingOptions();
options.CharacterSet = "UTF-8";
options.DisableECI = true; // Extended Channel Interpretation (ECI) 主要用于特殊的字符集。并不是所有的扫描器都支持这种编码。
options.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H; // 纠错级别
options.Width = 300;
options.Height = 300;
options.Margin = 1;
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
Bitmap bmp = writer.Write(str);
return bmp;
}
/// <summary>
/// 识别二维码
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public static string readCode(Bitmap bmp)
{
string str = "";
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
try
{
Result result = reader.Decode(bmp);
if (result != null)
{
str = result.Text;
}
}
catch (Exception)
{
return str;
}
return str;
}
/// <summary>
/// 识别二维码
/// </summary>
/// <param name="imagePath"></param>
/// <returns></returns>
public static string readCode(string imagePath)
{
string str = "";
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
using (Bitmap bmp = new Bitmap(imagePath))
{
try
{
Result result = reader.Decode(bmp);
if (result != null)
{
str = result.Text;
}
}
catch (Exception)
{
return str;
}
}
return str;
}
}
}
https://www.npmjs.com/package/qrcode#usage
引用
<script src="https://cdn.jsdelivr.net/npm/qrcode/build/qrcode.min.js"></script>
或者
npm install --save qrcode
import QRCode from 'qrcode'
调用
let that = this;
var opts = {
errorCorrectionLevel: 'H',
type: 'image/jpeg',
quality: 0.9,
margin: 0,
color: {
dark: "#000000",
light: "#ffffff"
}
};
QRCode.toDataURL(
"https://www.psvmc.cn",
opts,
function (err, url) {
that.qrcode_url = url;
});
注意
要转换的内容必须是字符串,不能是数字,否则会报错。
https://www.npmjs.com/package/qrcodejs2
let qrcodes = this.$refs["qrcode"];
for (const qrcode of qrcodes) {
new QRCode(qrcode, {
text: "https://www.psvmc.cn",
width: 128,
height: 128,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.M
});
}
更新二维码
qrcode.clear();
qrcode.makeCode("https://www.psvmc.cn");