Optimize and improve QR code display

pull/8007/head
2dust 2025-09-21 14:35:49 +08:00
parent c2c13ad318
commit 534c7ab444
5 changed files with 75 additions and 19 deletions

View File

@ -1,4 +1,5 @@
using QRCoder; using QRCoder;
using QRCoder.Exceptions;
using SkiaSharp; using SkiaSharp;
using ZXing.SkiaSharp; using ZXing.SkiaSharp;
@ -8,10 +9,45 @@ public class QRCodeUtils
{ {
public static byte[]? GenQRCode(string? url) public static byte[]? GenQRCode(string? url)
{ {
if (url.IsNullOrEmpty())
{
return null;
}
using QRCodeGenerator qrGenerator = new(); using QRCodeGenerator qrGenerator = new();
using var qrCodeData = qrGenerator.CreateQrCode(url ?? string.Empty, QRCodeGenerator.ECCLevel.Q); DataTooLongException? lastDtle = null;
using PngByteQRCode qrCode = new(qrCodeData);
return qrCode.GetGraphic(20); 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) public static string? ParseBarcode(string? fileName)

View File

@ -4,19 +4,25 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="480" xmlns:sys="clr-namespace:System;assembly=netstandard"
d:DesignWidth="400" d:DesignHeight="600"
d:DesignWidth="600"
mc:Ignorable="d"> mc:Ignorable="d">
<UserControl.Resources>
<sys:Double x:Key="QrcodeWidth">500</sys:Double>
</UserControl.Resources>
<Grid Margin="32" RowDefinitions="Auto,Auto"> <Grid Margin="32" RowDefinitions="Auto,Auto">
<Image <Image
Name="imgQrcode" Name="imgQrcode"
Width="300" Width="{StaticResource QrcodeWidth}"
Height="300" /> Height="{StaticResource QrcodeWidth}" />
<TextBox <TextBox
x:Name="txtContent" x:Name="txtContent"
Grid.Row="1" Grid.Row="1"
Width="300" Width="{StaticResource QrcodeWidth}"
MaxHeight="100" MaxHeight="100"
Margin="{StaticResource MarginTb8}" Margin="{StaticResource MarginTb8}"
VerticalAlignment="Center" VerticalAlignment="Center"

View File

@ -23,8 +23,16 @@ public partial class QrcodeView : UserControl
private Bitmap? GetQRCode(string? url) private Bitmap? GetQRCode(string? url)
{ {
var bytes = QRCodeUtils.GenQRCode(url); try
return ByteToBitmap(bytes); {
var bytes = QRCodeUtils.GenQRCode(url);
return ByteToBitmap(bytes);
}
catch (Exception ex)
{
Logging.SaveLog("GetQRCode", ex);
return null;
}
} }
private Bitmap? ByteToBitmap(byte[]? bytes) private Bitmap? ByteToBitmap(byte[]? bytes)

View File

@ -20,8 +20,9 @@ public class QRCodeUtils
var qrCodeImage = ServiceLib.Common.QRCodeUtils.GenQRCode(strContent); var qrCodeImage = ServiceLib.Common.QRCodeUtils.GenQRCode(strContent);
return qrCodeImage is null ? null : ByteToImage(qrCodeImage); return qrCodeImage is null ? null : ByteToImage(qrCodeImage);
} }
catch catch (Exception ex)
{ {
Logging.SaveLog("GetQRCode", ex);
return null; return null;
} }
} }

View File

@ -1,4 +1,4 @@
<UserControl <UserControl
x:Class="v2rayN.Views.QrcodeView" x:Class="v2rayN.Views.QrcodeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@ -6,28 +6,33 @@
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib" xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib"
d:DesignHeight="300" xmlns:sys="clr-namespace:System;assembly=mscorlib"
d:DesignWidth="300" d:DesignHeight="600"
d:DesignWidth="600"
Style="{StaticResource ViewGlobal}" Style="{StaticResource ViewGlobal}"
mc:Ignorable="d"> mc:Ignorable="d">
<UserControl.Resources>
<sys:Double x:Key="QrcodeWidth">500</sys:Double>
</UserControl.Resources>
<Grid Margin="32"> <Grid Margin="32">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="60" /> <RowDefinition Height="80" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Image <Image
x:Name="imgQrcode" x:Name="imgQrcode"
Grid.Row="0" Grid.Row="0"
Width="300" Width="{StaticResource QrcodeWidth}"
Height="300" Height="{StaticResource QrcodeWidth}"
Stretch="UniformToFill" /> Stretch="UniformToFill" />
<TextBox <TextBox
x:Name="txtContent" x:Name="txtContent"
Grid.Row="1" Grid.Row="1"
Width="300" Width="{StaticResource QrcodeWidth}"
Margin="0,8" Margin="0,8"
VerticalAlignment="Center" VerticalAlignment="Center"
IsReadOnly="True" IsReadOnly="True"
@ -46,4 +51,4 @@
IsDefault="True" IsDefault="True"
Style="{StaticResource DefButton}" /> Style="{StaticResource DefButton}" />
</Grid> </Grid>
</UserControl> </UserControl>