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 @@ -