From 6721d150e09c69c8f4c4d55427f36125ce5ff0c5 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 28 Aug 2024 10:20:37 +0800 Subject: [PATCH] Refactor QRCodeHelper --- v2rayN/ServiceLib/Common/QRCodeHelper.cs | 15 +++++++++++ v2rayN/ServiceLib/ServiceLib.csproj | 1 + v2rayN/v2rayN/Common/QRCodeHelper.cs | 32 +++++++++++++++++------- v2rayN/v2rayN/v2rayN.csproj | 1 - 4 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 v2rayN/ServiceLib/Common/QRCodeHelper.cs diff --git a/v2rayN/ServiceLib/Common/QRCodeHelper.cs b/v2rayN/ServiceLib/Common/QRCodeHelper.cs new file mode 100644 index 00000000..7e6e0d08 --- /dev/null +++ b/v2rayN/ServiceLib/Common/QRCodeHelper.cs @@ -0,0 +1,15 @@ +using QRCoder; + +namespace ServiceLib.Common +{ + public class QRCodeHelper + { + public static byte[]? GenQRCode(string? url) + { + using QRCodeGenerator qrGenerator = new(); + using QRCodeData qrCodeData = qrGenerator.CreateQrCode(url ?? string.Empty, QRCodeGenerator.ECCLevel.Q); + using PngByteQRCode qrCode = new(qrCodeData); + return qrCode.GetGraphic(20); + } + } +} \ No newline at end of file diff --git a/v2rayN/ServiceLib/ServiceLib.csproj b/v2rayN/ServiceLib/ServiceLib.csproj index 7cb089c3..bc138c00 100644 --- a/v2rayN/ServiceLib/ServiceLib.csproj +++ b/v2rayN/ServiceLib/ServiceLib.csproj @@ -13,6 +13,7 @@ + diff --git a/v2rayN/v2rayN/Common/QRCodeHelper.cs b/v2rayN/v2rayN/Common/QRCodeHelper.cs index 255ab475..cad39dba 100644 --- a/v2rayN/v2rayN/Common/QRCodeHelper.cs +++ b/v2rayN/v2rayN/Common/QRCodeHelper.cs @@ -1,9 +1,9 @@ -using QRCoder; -using QRCoder.Xaml; -using System.Drawing; +using System.Drawing; +using System.IO; using System.Windows; using System.Windows.Interop; using System.Windows.Media; +using System.Windows.Media.Imaging; using ZXing; using ZXing.Common; using ZXing.QrCode; @@ -16,7 +16,7 @@ namespace v2rayN /// public class QRCodeHelper { - public static DrawingImage? GetQRCode(string? strContent) + public static ImageSource? GetQRCode(string? strContent) { if (strContent is null) { @@ -24,11 +24,12 @@ namespace v2rayN } try { - QRCodeGenerator qrGenerator = new(); - QRCodeData qrCodeData = qrGenerator.CreateQrCode(strContent, QRCodeGenerator.ECCLevel.H); - XamlQRCode qrCode = new(qrCodeData); - DrawingImage qrCodeAsXaml = qrCode.GetGraphic(40); - return qrCodeAsXaml; + var qrCodeImage = ServiceLib.Common.QRCodeHelper.GenQRCode(strContent); + if (qrCodeImage is null) + { + return null; + } + return ByteToImage(qrCodeImage); } catch { @@ -36,6 +37,19 @@ namespace v2rayN } } + private static ImageSource ByteToImage(byte[] imageData) + { + BitmapImage biImg = new(); + MemoryStream ms = new(imageData); + biImg.BeginInit(); + biImg.StreamSource = ms; + biImg.EndInit(); + + ImageSource imgSrc = biImg as ImageSource; + + return imgSrc; + } + public static string ScanScreen(float dpiX, float dpiY) { try diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index 09467700..848ec9b6 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -17,7 +17,6 @@ -