赞
踩
在linux下生成和识别二维码,试了大半天,找到个库测试通过可以使用
ThoughtWorks.QRCodCore
其它的库有些仅在window下可用,有些使用困难
这里如果你在linux下跑的话,一定要安装
skiaSharp.NativeAssets.Linux 库
不然在window下正常,在linux下会报错
测试的时候提示报错如下
unable to load shared library libgdiplus or one of its dependencies
缺少图型库 libgdiplus
运行指令安装 解决
apt-get update && apt-get install -y apt-utils libgdiplus libc6-dev
上代码:
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Text;
- using ThoughtWorks.QRCode.Codec;
- using ThoughtWorks.QRCode.Codec.Data;
-
- namespace Service.QRcode
- {
- public class QRcodeThoughtWorks
- {
- public void test()
- {
- string filename = AppDomain.CurrentDomain.BaseDirectory + "/" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
-
- //生成
- CreateQRImg(@"test", filename);
- // Thread.Sleep(2000);
-
- //读
- string result = GetImageFile(filename);
- Console.WriteLine("结果:" + result);
-
- }
- /// <summary>
- /// 生成并保存二维码图片的方法
- /// </summary>
- /// <param name="str">输入的内容</param>
- private void CreateQRImg(string str, string filename)
- {
- Bitmap bt;
- string enCodeString = str;
- //生成设置编码实例
- QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
-
- qrCodeEncoder.QRCodeBackgroundColor = Color.White;//设置二维码背景颜色
- qrCodeEncoder.QRCodeForegroundColor = Color.Black;//设置二维码前景色
- qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//设置二维码编码类型
- qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//设置二维码拍错率
- qrCodeEncoder.QRCodeScale = 4;//设置二维码的大小
- qrCodeEncoder.QRCodeVersion = 7;//设置二维码的版本
- //Bitmap bmp = qRCodeEncoder.Encode(str);
-
-
- 设置二维码的规模 默认 4
- //qrCodeEncoder.QRCodeScale = 4;
- 设置二维码的版本 默认 7
- //qrCodeEncoder.QRCodeVersion = 7;
- 设置错误检验级别 默认为中等
- //qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
- //生成二维码图片
- bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);
- //二维码图片名称
- bt = RedrawQrImg(bt);
- //保存二维码图片在photos路径下
- bt.Save(@"" + filename);
- //图片控件要显示的二维码图片路径
- }
- /// <summary>
- /// 将二维码转换成网页可以显示的数据
- /// </summary>
- /// <param name="qrcode"></param>
- /// <returns></returns>
- public static string GetQrcodeImageData(string qrcode)
- {
- QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
- qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
- qrCodeEncoder.QRCodeScale = 12;
- qrCodeEncoder.QRCodeVersion = 2;
- qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
- var bmp = qrCodeEncoder.Encode(qrcode);
- byte[] b = null;
- using (MemoryStream stream = new MemoryStream())
- {
- bmp.Save(stream, ImageFormat.Jpeg);//将图像以指定的格式存入缓存内存流
- b = new byte[stream.Length];
- stream.Position = 0;
- stream.Read(b, 0, Convert.ToInt32(b.Length));
- }
- return "data:image/jpeg;base64," + Convert.ToBase64String(b);
- }
- /// <summary>
- /// 对二维码图片进行处理
- /// </summary>
- /// <param name="QrImg">二维码</param>
- /// <returns>处理过后的二维码</returns>
- public Bitmap RedrawQrImg(Bitmap QrImg)
- {
- Bitmap bitmap = new Bitmap(QrImg.Width + 3, QrImg.Height + 3);
- Graphics g = Graphics.FromImage(bitmap);
- g.Clear(Color.White);
- g.DrawRectangle(new Pen(Color.White), new Rectangle(0, 0, bitmap.Width, bitmap.Height));
- g.DrawImage(QrImg, new PointF(2, 2));
-
- return bitmap;
- }
- /// <summary>
- /// 二维码解码
- /// </summary>
- /// <param name="filePath">图片路径</param>
- /// <returns></returns>
- public static string GetImageFile(string filePath)
- {
- try
- {
- if (!File.Exists(filePath))
- return "not filepath";
- Bitmap myBitmap = new Bitmap(Image.FromFile(filePath));
- QRCodeDecoder decoder = new QRCodeDecoder();
- string decodedString = decoder.decode(new QRCodeBitmapImage(myBitmap), Encoding.UTF8);
- return decodedString;
- }
- catch (Exception ex)
- {
- return ex.ToString();
- }
- }
-
-
- }
- }
复制即可使用
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。