mirror of
https://github.com/maker-community/ElectronBot.DotNet.git
synced 2026-10-29 13:19:30 +08:00
update eb player
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Verdure.Braincase.WinUI.Common.Models;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Verdure.Braincase.WinUI.Common.Models;
|
||||
|
||||
namespace Verdure.Braincase.WinUI.Common;
|
||||
public interface IElectronBotPlayer
|
||||
@@ -11,4 +12,5 @@ public interface IElectronBotPlayer
|
||||
Task PlayImageAsync(string path);
|
||||
Task PlayImageAsync(Stream stream);
|
||||
Task PlayImageAsync(byte[] bytes);
|
||||
Task PlayImageAsync(UIElement element);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
using SixLabors.ImageSharp;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using Verdure.Braincase.Core.Models;
|
||||
@@ -43,6 +46,50 @@ public partial class ElectronBotPlayer
|
||||
|
||||
_ = await _actionFrameService.SendToUsbDeviceAsync(frameData);
|
||||
}
|
||||
public async Task PlayImageAsync(UIElement element)
|
||||
{
|
||||
try
|
||||
{
|
||||
var bitmap = new RenderTargetBitmap();
|
||||
|
||||
await bitmap.RenderAsync(element);
|
||||
|
||||
var pixels = await bitmap.GetPixelsAsync();
|
||||
|
||||
var pixelBytes = pixels.ToArray();
|
||||
|
||||
//使用ImageSharp处理帧数据
|
||||
using var image = Image
|
||||
.LoadPixelData<Rgba32>(pixelBytes, bitmap.PixelWidth, bitmap.PixelHeight);
|
||||
|
||||
image.Mutate(x =>
|
||||
{
|
||||
x.Resize(240, 240);
|
||||
});
|
||||
|
||||
// 获取转换后的数据
|
||||
var rgbData = new byte[image.Width * image.Height * 3];
|
||||
|
||||
// 遍历每个像素,将Rgba32转换为Bgr24
|
||||
for (var y = 0; y < image.Height; y++)
|
||||
{
|
||||
for (var x = 0; x < image.Width; x++)
|
||||
{
|
||||
var rgbaPixel = image[x, y];
|
||||
var rgbIndex = (y * image.Width + x) * 3;
|
||||
rgbData[rgbIndex] = rgbaPixel.B;
|
||||
rgbData[rgbIndex + 1] = rgbaPixel.G;
|
||||
rgbData[rgbIndex + 2] = rgbaPixel.R;
|
||||
}
|
||||
}
|
||||
|
||||
var frameData = new EmoticonActionFrame(rgbData);
|
||||
_ = await _actionFrameService.SendToUsbDeviceAsync(frameData);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<Image<Rgba32>> LoadImageAsync(string imagePath)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Diagnostics;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using Microsoft.UI.Xaml;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using Verdure.Braincase.Core.Models;
|
||||
using Verdure.Braincase.WinUI.Common.Models;
|
||||
|
||||
@@ -4,6 +4,7 @@ using Verdure.Braincase.ViewModels;
|
||||
namespace ViewModels;
|
||||
public partial class MiniModeViewModel : ObservableRecipient
|
||||
{
|
||||
private readonly IElectronBotPlayer _electronBotPlayer;
|
||||
private readonly DispatcherTimer _timer = new()
|
||||
{
|
||||
Interval = TimeSpan.FromMilliseconds(200)
|
||||
@@ -11,10 +12,11 @@ public partial class MiniModeViewModel : ObservableRecipient
|
||||
|
||||
[ObservableProperty] private string _voiceResult = string.Empty;
|
||||
|
||||
public MiniModeViewModel()
|
||||
public MiniModeViewModel(IElectronBotPlayer electronBotPlayer)
|
||||
{
|
||||
|
||||
_timer.Tick += Timer_Tick;
|
||||
_electronBotPlayer = electronBotPlayer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -25,7 +27,7 @@ public partial class MiniModeViewModel : ObservableRecipient
|
||||
|
||||
private async void Timer_Tick(object sender, object e)
|
||||
{
|
||||
|
||||
await _electronBotPlayer.PlayImageAsync(Element);
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
@@ -54,7 +56,7 @@ public partial class MiniModeViewModel : ObservableRecipient
|
||||
_timer.Tick -= Timer_Tick;
|
||||
_timer.Stop();
|
||||
|
||||
if(Element is UserControl userControl)
|
||||
if (Element is UserControl userControl)
|
||||
{
|
||||
var viewModel = userControl.DataContext as ClockViewModel;
|
||||
viewModel?.UnLoadedCommand.Execute(null);
|
||||
|
||||
Reference in New Issue
Block a user