From 534c7ab444b5ec95e0a222f34fbbb69154cb5e24 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 21 Sep 2025 14:35:49 +0800 Subject: [PATCH] Optimize and improve QR code display --- v2rayN/ServiceLib/Common/QRCodeUtils.cs | 42 +++++++++++++++++-- v2rayN/v2rayN.Desktop/Views/QrcodeView.axaml | 16 ++++--- .../v2rayN.Desktop/Views/QrcodeView.axaml.cs | 12 +++++- v2rayN/v2rayN/Common/QRCodeUtils.cs | 3 +- v2rayN/v2rayN/Views/QrcodeView.xaml | 21 ++++++---- 5 files changed, 75 insertions(+), 19 deletions(-) diff --git a/v2rayN/ServiceLib/Common/QRCodeUtils.cs b/v2rayN/ServiceLib/Common/QRCodeUtils.cs index 3d3dc90b..d9015367 100644 --- a/v2rayN/ServiceLib/Common/QRCodeUtils.cs +++ b/v2rayN/ServiceLib/Common/QRCodeUtils.cs @@ -1,4 +1,5 @@ using QRCoder; +using QRCoder.Exceptions; using SkiaSharp; using ZXing.SkiaSharp; @@ -8,10 +9,45 @@ public class QRCodeUtils { public static byte[]? GenQRCode(string? url) { + if (url.IsNullOrEmpty()) + { + return null; + } using QRCodeGenerator qrGenerator = new(); - using var qrCodeData = qrGenerator.CreateQrCode(url ?? string.Empty, QRCodeGenerator.ECCLevel.Q); - using PngByteQRCode qrCode = new(qrCodeData); - return qrCode.GetGraphic(20); + DataTooLongException? lastDtle = null; + + var levels = new[] + { + QRCodeGenerator.ECCLevel.H, + QRCodeGenerator.ECCLevel.Q, + QRCodeGenerator.ECCLevel.M, + QRCodeGenerator.ECCLevel.L + }; + foreach (var level in levels) + { + try + { + using var qrCodeData = qrGenerator.CreateQrCode(url, level); + using PngByteQRCode qrCode = new(qrCodeData); + return qrCode.GetGraphic(20); + } + catch (DataTooLongException ex) + { + lastDtle = ex; + continue; + } + catch + { + throw; + } + } + + if (lastDtle != null) + { + throw lastDtle; + } + + return null; } public static string? ParseBarcode(string? fileName) diff --git a/v2rayN/v2rayN.Desktop/Views/QrcodeView.axaml b/v2rayN/v2rayN.Desktop/Views/QrcodeView.axaml index 90d94685..b67a7a1f 100644 --- a/v2rayN/v2rayN.Desktop/Views/QrcodeView.axaml +++ b/v2rayN/v2rayN.Desktop/Views/QrcodeView.axaml @@ -4,19 +4,25 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - d:DesignHeight="480" - d:DesignWidth="400" + xmlns:sys="clr-namespace:System;assembly=netstandard" + d:DesignHeight="600" + d:DesignWidth="600" mc:Ignorable="d"> + + + 500 + + + Width="{StaticResource QrcodeWidth}" + Height="{StaticResource QrcodeWidth}" /> + + 500 + + - + - \ No newline at end of file +