Skip to content

Commit

Permalink
修复Arm64版本二维码生成错误
Browse files Browse the repository at this point in the history
  • Loading branch information
ywmoyue committed Dec 10, 2023
1 parent ef74dbc commit 98995d8
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 45 deletions.
15 changes: 11 additions & 4 deletions src/BiliLite.UWP/BiliLite.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<DefineConstants>TRACE;DEBUG;NETFX_CORE;WINDOWS_UWP;CODE_ANALYSIS;X86</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
Expand All @@ -58,7 +58,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\ARM\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<DefineConstants>TRACE;DEBUG;NETFX_CORE;WINDOWS_UWP;CODE_ANALYSIS;ARM</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>ARM</PlatformTarget>
Expand All @@ -83,7 +83,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\ARM64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<DefineConstants>TRACE;DEBUG;NETFX_CORE;WINDOWS_UWP;CODE_ANALYSIS;ARM64</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>ARM64</PlatformTarget>
Expand All @@ -109,7 +109,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<DefineConstants>TRACE;DEBUG;NETFX_CORE;WINDOWS_UWP;CODE_ANALYSIS;X64</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
Expand All @@ -136,6 +136,7 @@
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<ItemGroup>
<Compile Include="Extensions\QrCodeExtensions.cs" />
<Compile Include="Models\Common\Anime\ISeasonItem.cs" />
<Compile Include="Models\Common\Live\InteractWordModel.cs" />
<Compile Include="Models\Common\Live\LiveMessageHandleActionsMap.cs" />
Expand Down Expand Up @@ -223,7 +224,10 @@
<Compile Include="Player\States\PlayStates\PlayingPlayState.cs" />
<Compile Include="Player\States\PlayStates\PlayStateHandler.cs" />
<Compile Include="Player\States\PlayStates\StopPlayState.cs" />
<Compile Include="Services\Interfaces\IQrCodeService.cs" />
<Compile Include="Services\QrCoderQrCodeService.cs" />
<Compile Include="Services\WbiKeyService.cs" />
<Compile Include="Services\ZXingQrCodeService.cs" />
<Compile Include="ViewModels\Live\LiveDetailPageViewModel.cs" />
<Compile Include="ViewModels\Live\LiveRoomLotteryViewModel.cs" />
<Compile Include="ViewModels\Live\SuperChatMsgViewModel.cs" />
Expand Down Expand Up @@ -1228,6 +1232,9 @@
<PackageReference Include="PropertyChanged.Fody">
<Version>4.1.0</Version>
</PackageReference>
<PackageReference Include="QRCoder">
<Version>1.4.3</Version>
</PackageReference>
<PackageReference Include="RestSharp">
<Version>107.3.0</Version>
</PackageReference>
Expand Down
19 changes: 19 additions & 0 deletions src/BiliLite.UWP/Extensions/QrCodeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using BiliLite.Services;
using BiliLite.Services.Interfaces;
using Microsoft.Extensions.DependencyInjection;

namespace BiliLite.Extensions
{
public static class QrCodeExtensions
{
public static IServiceCollection AddQrCodeService(this IServiceCollection services)
{
#if ARM64
services.AddTransient<IQrCodeService, QrCoderQrCodeService>();
#else
services.AddTransient<IQrCodeService, ZXingQrCodeService>();
#endif
return services;
}
}
}
15 changes: 6 additions & 9 deletions src/BiliLite.UWP/Modules/User/LoginVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using BiliLite.Models.Common;
using BiliLite.Models.Requests.Api;
using BiliLite.Services;
using BiliLite.Services.Interfaces;
using Microsoft.Extensions.DependencyInjection;

namespace BiliLite.Modules.User
{
Expand Down Expand Up @@ -600,15 +602,10 @@ private async void GetQRAuthInfo()
if (result.success)
{
qrAuthInfo = result.data;
ZXing.BarcodeWriter barcodeWriter = new ZXing.BarcodeWriter();
barcodeWriter.Format = ZXing.BarcodeFormat.QR_CODE;
barcodeWriter.Options = new ZXing.Common.EncodingOptions()
{
Margin = 1,
Height = 200,
Width = 200
};
var img = barcodeWriter.Write(qrAuthInfo.url);

var qrCodeService = App.ServiceProvider.GetRequiredService<IQrCodeService>();
var img = await qrCodeService.GenerateQrCode(qrAuthInfo.url);

QRImageSource = img;
qrTimer = new Timer();
qrTimer.Interval = 3000;
Expand Down
34 changes: 15 additions & 19 deletions src/BiliLite.UWP/Pages/SeasonDetailPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using BiliLite.Models.Common.Video;
using BiliLite.Models.Download;
using BiliLite.ViewModels.Season;
using BiliLite.Services.Interfaces;
using Microsoft.Extensions.DependencyInjection;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

Expand Down Expand Up @@ -134,7 +136,7 @@ private async Task InitSeasonDetail()
seasonReviewVM.MediaID = m_viewModel.Detail.MediaId;

InitializePlayInfo();
CreateQR();
await CreateQR();

}
}
Expand Down Expand Up @@ -298,7 +300,8 @@ private void PlayerControl_FullWindowEvent(object sender, bool e)
}
}
bool changedFlag = false;
private void PlayerControl_ChangeEpisodeEvent(object sender, int e)

private async void PlayerControl_ChangeEpisodeEvent(object sender, int e)
{
changedFlag = true;
var aid = "";
Expand All @@ -315,7 +318,7 @@ private void PlayerControl_ChangeEpisodeEvent(object sender, int e)
aid = m_viewModel.Episodes[e].Aid;
}

CreateQR();
await CreateQR();
comment.LoadComment(new LoadCommentInfo()
{
CommentMode = (int)CommentApi.CommentType.Video,
Expand All @@ -325,7 +328,7 @@ private void PlayerControl_ChangeEpisodeEvent(object sender, int e)
changedFlag = false;
}

private void listEpisode_SelectionChanged(object sender, SelectionChangedEventArgs e)
private async void listEpisode_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (changedFlag || listEpisode.SelectedIndex == -1)
{
Expand All @@ -345,9 +348,10 @@ private void listEpisode_SelectionChanged(object sender, SelectionChangedEventAr
CommentSort = CommentApi.CommentSort.Hot,
Oid = m_viewModel.Episodes[listEpisode.SelectedIndex].Aid
});
CreateQR();
await CreateQR();
}
private void listPreview_SelectionChanged(object sender, SelectionChangedEventArgs e)

private async void listPreview_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (changedFlag || listPreview.SelectedIndex == -1)
{
Expand All @@ -367,24 +371,16 @@ private void listPreview_SelectionChanged(object sender, SelectionChangedEventAr
CommentSort = CommentApi.CommentSort.Hot,
Oid = m_viewModel.Episodes[listPreview.SelectedIndex].Aid
});
CreateQR();
await CreateQR();
}


private void CreateQR()
private async Task CreateQR()
{
try
{
ZXing.BarcodeWriter barcodeWriter = new ZXing.BarcodeWriter();
barcodeWriter.Format = ZXing.BarcodeFormat.QR_CODE;
barcodeWriter.Options = new ZXing.Common.EncodingOptions()
{
Margin = 1,
Height = 200,
Width = 200
};
var data = barcodeWriter.Write("https://b23.tv/ep" + ep_id);
imgQR.Source = data;
var qrCodeService = App.ServiceProvider.GetRequiredService<IQrCodeService>();
var img = await qrCodeService.GenerateQrCode("https://b23.tv/ep" + ep_id);
imgQR.Source = img;
}
catch (Exception ex)
{
Expand Down
20 changes: 7 additions & 13 deletions src/BiliLite.UWP/Pages/VideoDetailPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using BiliLite.Models.Common.Video.Detail;
using BiliLite.Models.Download;
using BiliLite.ViewModels.Video;
using BiliLite.Services.Interfaces;
using Microsoft.Extensions.DependencyInjection;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

Expand Down Expand Up @@ -182,7 +184,7 @@ private async Task InitializeVideo(string id)

contentDesc.Content = desc;
ChangeTitle(m_viewModel.VideoInfo.Title);
CreateQR();
await CreateQR();
if (!string.IsNullOrEmpty(m_viewModel.VideoInfo.RedirectUrl))
{
var result = await MessageCenter.HandelSeasonID(m_viewModel.VideoInfo.RedirectUrl);
Expand Down Expand Up @@ -275,27 +277,19 @@ private void InitUgcSeason(string id)
pivot.Items.Insert(0, element);
}

private void CreateQR()
private async Task CreateQR()
{
try
{
ZXing.BarcodeWriter barcodeWriter = new ZXing.BarcodeWriter();
barcodeWriter.Format = ZXing.BarcodeFormat.QR_CODE;
barcodeWriter.Options = new ZXing.Common.EncodingOptions()
{
Margin = 1,
Height = 200,
Width = 200
};
var data = barcodeWriter.Write(m_viewModel.VideoInfo.ShortLink);
imgQR.Source = data;
var qrCodeService = App.ServiceProvider.GetRequiredService<IQrCodeService>();
var img = await qrCodeService.GenerateQrCode(m_viewModel.VideoInfo.ShortLink);
imgQR.Source = img;
}
catch (Exception ex)
{
logger.Log("创建二维码失败avid" + avid, LogType.Error, ex);
Notify.ShowMessageToast("创建二维码失败");
}

}

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
Expand Down
10 changes: 10 additions & 0 deletions src/BiliLite.UWP/Services/Interfaces/IQrCodeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading.Tasks;
using Windows.UI.Xaml.Media;

namespace BiliLite.Services.Interfaces
{
public interface IQrCodeService
{
public Task<ImageSource> GenerateQrCode(string content, int size = 200);
}
}
30 changes: 30 additions & 0 deletions src/BiliLite.UWP/Services/QrCoderQrCodeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media;
using BiliLite.Services.Interfaces;
using QRCoder;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;

namespace BiliLite.Services
{
public class QrCoderQrCodeService : IQrCodeService
{
public async Task<ImageSource> GenerateQrCode(string content, int size = 200)
{
var qrGenerator = new QRCodeGenerator();
var qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q);

var qrCodeBmp = new BitmapByteQRCode(qrCodeData);
var qrCodeImageBmp = qrCodeBmp.GetGraphic(size);

using var stream = new InMemoryRandomAccessStream();
using var writer = new DataWriter(stream.GetOutputStreamAt(0));
writer.WriteBytes(qrCodeImageBmp);
await writer.StoreAsync();
var image = new BitmapImage();
await image.SetSourceAsync(stream);
return image;
}
}
}
25 changes: 25 additions & 0 deletions src/BiliLite.UWP/Services/ZXingQrCodeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Threading.Tasks;
using Windows.UI.Xaml.Media;
using BiliLite.Services.Interfaces;

namespace BiliLite.Services
{
public class ZXingQrCodeService : IQrCodeService
{
public Task<ImageSource> GenerateQrCode(string content,int size=200)
{
var barcodeWriter = new ZXing.BarcodeWriter
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new ZXing.Common.EncodingOptions()
{
Margin = 1,
Height = size,
Width = size
}
};
var img = barcodeWriter.Write(content);
return Task.FromResult<ImageSource>(img);
}
}
}
1 change: 1 addition & 0 deletions src/BiliLite.UWP/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public void ConfigureServices(HostBuilderContext context, IServiceCollection ser
services.AddViewModels();

services.AddSingleton<CookieService>();
services.AddQrCodeService();

services.AddSingleton<GrpcService>();
}
Expand Down

0 comments on commit 98995d8

Please sign in to comment.