增加相机界面
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<UserControl x:Class="SicUI.Controls.CameraControl"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:SicUI.Controls"
|
||||
xmlns:deviceControl="clr-namespace:Aitex.Core.UI.DeviceControl;assembly=MECF.Framework.UI.Core"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="480" d:DesignWidth="520" Unloaded="CameraControl_Unloaded" Loaded="CameraControl_loaded">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="35"/>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="35"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="CCD采集" Style="{DynamicResource Table_TitleStyle}" />
|
||||
<Border Grid.Row="1" Background="{DynamicResource Table_BG_Title}" BorderBrush="{DynamicResource Table_BD}" BorderThickness="1,0,1,1">
|
||||
<Image x:Name="imageControl" Width="480" Height="360"/>
|
||||
</Border>
|
||||
<Border Grid.Row="2" Background="{DynamicResource Table_BG_Content}" BorderBrush="{DynamicResource Table_BD}" BorderThickness="1,0,1,1">
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="Connection:" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="12 3"/>
|
||||
<deviceControl:AITSensor Margin="12 3" HorizontalAlignment="Center" VerticalAlignment="Center" CustomColorOff="Gray" CustomColorOn="Lime" LightOnValue="{Binding CamConnection}" IsCustomRender="True" />
|
||||
<Button Content="采集" Width="80" Margin="12 3" Click="ButtonGrab_Click"/>
|
||||
<Button Content="连续采集" Width="80" Click="ButtonGrabbing_Click" Margin="12 3"/>
|
||||
<Button Content="停止" Width="80" Click="ButtonStop_Click" Margin="12 3"/>
|
||||
<TextBlock Text="{Binding CamMessage}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="12 3"/>
|
||||
</StackPanel>
|
||||
</Border>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,160 @@
|
||||
using Aitex.Core.RT.Log;
|
||||
using CommandLine;
|
||||
using MECF.Framework.Common.DataCenter;
|
||||
using SicUI.Controls.M2C4Parts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
using System.Windows.Threading;
|
||||
using VM.PlatformSDKCS;
|
||||
|
||||
namespace SicUI.Controls
|
||||
{
|
||||
/// <summary>
|
||||
/// CameraControl.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class CameraControl : UserControl
|
||||
{
|
||||
|
||||
private WriteableBitmap _bitmap;
|
||||
private Int32Rect _rect;
|
||||
private CancellationTokenSource _cts;
|
||||
|
||||
public CameraControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
// 获取摄像头分辨率
|
||||
var width = (int)QueryDataClient.Instance.Service.GetConfig("HikGigeCamera.Width");
|
||||
var height = (int)QueryDataClient.Instance.Service.GetConfig("HikGigeCamera.Height");
|
||||
|
||||
// 创建 WriteableBitmap,注意像素格式需要与摄像头输出匹配(例如 Bgr24)
|
||||
_bitmap = new WriteableBitmap(width, height, 96, 96, System.Windows.Media.PixelFormats.Bgr24, null);
|
||||
_rect = new Int32Rect(0, 0, _bitmap.PixelWidth, _bitmap.PixelHeight);
|
||||
// 只需设置一次 Source,不必每帧重新赋值
|
||||
imageControl.Source = _bitmap;
|
||||
|
||||
}
|
||||
|
||||
private async Task CaptureLoop(CancellationToken token)
|
||||
{
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
var obj = QueryDataClient.Instance.Service.GetData($"LoadLock.HikGigeCamera.CamBitmap");
|
||||
if (obj != null)
|
||||
{
|
||||
Bitmap b = BytesToBitmap((byte[])obj);
|
||||
// 在 UI 线程更新图像数据
|
||||
#pragma warning disable CS4014
|
||||
Application.Current.Dispatcher.BeginInvoke(
|
||||
DispatcherPriority.Background,
|
||||
new Action(() => UpdateBitmap(b)));
|
||||
#pragma warning restore CS4014
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Warning($"Camera Capture failed{ex}");
|
||||
}
|
||||
|
||||
// 根据需要调整延时,防止 CPU 占用过高
|
||||
// 异步延时,释放后台线程资源,并响应取消请求
|
||||
try
|
||||
{
|
||||
await Task.Delay(20, token);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Bitmap BytesToBitmap(byte[] bytes)
|
||||
{
|
||||
using (MemoryStream ms = new MemoryStream(bytes))
|
||||
{
|
||||
return new Bitmap(ms); // 或使用Image.FromStream:ml-citation{ref="1,18" data="citationList"}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新预先创建的 WriteableBitmap 的像素数据
|
||||
/// </summary>
|
||||
private void UpdateBitmap(Bitmap frame)
|
||||
{
|
||||
if (frame == null || _bitmap == null) return;
|
||||
|
||||
_bitmap.Lock();
|
||||
try
|
||||
{
|
||||
// 3. 移除类级别的 _bitmapData,改为局部变量
|
||||
var rect = new System.Drawing.Rectangle(0, 0, frame.Width, frame.Height);
|
||||
var bitmapData = frame.LockBits(rect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
|
||||
try
|
||||
{
|
||||
int size = bitmapData.Height * bitmapData.Stride;
|
||||
_bitmap.WritePixels(_rect, bitmapData.Scan0, size, bitmapData.Stride, 0, 0);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// 4. 无论如何必须解锁 GDI 数据
|
||||
frame.UnlockBits(bitmapData);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
// 5. 无论如何必须释放 Bitmap 和解锁 WriteableBitmap
|
||||
frame.Dispose();
|
||||
_bitmap.Unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ButtonGrab_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
InvokeClient.Instance.Service.DoOperation($"LoadLock.HikGigeCamera.Grab","UIGrab");
|
||||
}
|
||||
|
||||
private void ButtonGrabbing_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
InvokeClient.Instance.Service.DoOperation($"LoadLock.HikGigeCamera.Grabbing");
|
||||
}
|
||||
|
||||
private void CameraControl_Unloaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_cts?.Cancel();
|
||||
InvokeClient.Instance.Service.DoOperation($"LoadLock.HikGigeCamera.Stop");
|
||||
}
|
||||
|
||||
private void CameraControl_loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_cts = new CancellationTokenSource();
|
||||
_ = CaptureLoop(_cts.Token);
|
||||
}
|
||||
|
||||
private void ButtonStop_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
InvokeClient.Instance.Service.DoOperation($"LoadLock.HikGigeCamera.Stop");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user