using QRCoder; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; using ZXing; using ZXing.Common; using ZXing.Rendering; namespace NCC.Code.QrCode { /// /// 二维码类 /// public static class QrCodeCommon { /// /// 生成二维码 /// /// /// 像素大小 /// public static Bitmap GetQRCode(string plainText, int pixel) { return null; } /// /// 生成二维码 /// /// 二维码内容 /// public static Bitmap CreateQRCodeImage(string content) { // var writer = new BarcodeWriter // { // Format = global::ZXing.BarcodeFormat.QR_CODE, // Options = new EncodingOptions // { // Height = 200, // Width = 200, // Margin = 1 // }, // Renderer = new BitmapRenderer // { // Foreground = Color.Black, // Background = Color.White // } // }; // return writer.Write(content); return null; } /// /// 读取二维码(ThoughtWorks) /// /// /// public static string ReadQRCodeImage(Bitmap map) { try { QRCodeDecoder qrDecoder = new QRCodeDecoder(); string content = qrDecoder.decode(new QRCodeBitmapImage(map), Encoding.UTF8); return content; } catch (Exception e) { return ""; } } /// /// 添加二维码并保存到文件夹 /// /// 二维码内容 /// 子文件夹 /// public static string AddQRCodeSaveToFile(string content, string dir) { string fileName = Guid.NewGuid().ToString() + ".jpg"; string filePath = AppDomain.CurrentDomain.BaseDirectory.ToString() + @"resource\image\qr\" + dir; try { Bitmap qrCodeImage = CreateQRCodeImage(content); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } qrCodeImage.Save(filePath + @"\" + fileName, System.Drawing.Imaging.ImageFormat.Jpeg); qrCodeImage.Dispose(); return "/resource/image/qr/" + dir + "/" + fileName; } catch (Exception e) { return e.ToString(); } } /// /// 生成二维码 /// /// 内容 /// 像素大小 默认5 可自行调整 /// public static Bitmap ToQrCode(this string plainText, int pixel = 5) { return GetQRCode(plainText, pixel); } /// /// 生成二维码 Base64 /// /// 内容 /// 像素大小 默认5 可自行调整 /// 包含data:image... 前缀 默认false /// public static string ToQrCodeBase64(this string plainText, int pixel = 5, bool hasprev = false) { string base64 = ""; if (hasprev) { base64 = "data:image/png;base64,"; } base64 += FileHelper.BitmapToBase64(GetQRCode(plainText, pixel)); return base64; } } }