RealSenceD455摄像头采集彩色照片(帧)

发布时间 2023-12-11 23:02:21作者: 八爪鱼~

使用.Net6.0 + WPF,操作RealSence D455摄像头,获取帧数据,未处理,未使用前后端分离模式,相关代码直接写入后台.cs文件中。

 

前台.Xaml代码
MainWindow.xaml
<Window
    x:Class="RealSenceCameraD455_Test03.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:RealSenceCameraD455_Test03"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="测试彩色图像(帧)"
    Width="800"
    Height="450"
    FontSize="22"
    mc:Ignorable="d">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="100" />
        </Grid.RowDefinitions><StackPanel Orientation="Vertical">
            <Button
                x:Name="Start_Button"
                Margin="5"
                Click="Start_Button_Click"
                Content="Start"
                FontWeight="Bold"
                Foreground="Green" /><Button
                x:Name="Stop_Button"
                Margin="5"
                Click="Stop_Button_Click"
                Content="Stop"
                FontWeight="Bold"
                Foreground="Red" />
        </StackPanel><Border Grid.Row="1" Grid.ColumnSpan="2">
            <StackPanel Orientation="Vertical">
                <DockPanel>
                    <TextBlock Width="160" Text="摄像头名称:" />
                    <TextBlock x:Name="CameraName_TextBlock" Background="AliceBlue" />
                </DockPanel>
                <DockPanel>
                    <TextBlock Width="160" Text="摄像头序列号:" />
                    <TextBlock x:Name="CameraSerialNumber" Background="AliceBlue" />
                </DockPanel>
            </StackPanel>
        </Border><Grid Grid.Column="1" Margin="5">
            <Border BorderBrush="Red" BorderThickness="1">
                <Image x:Name="ColorImage" />
            </Border>
        </Grid></Grid>
</Window>

 

F5启动项目,内容显示如下:

image-20231209234603181

 

后台.cs文件

MainWindow.cs

using Intel.RealSense;
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
​
namespace RealSenceCameraD455_Test03
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //  连接电脑与摄像头的管道
        private Pipeline? _pipeline;
        //  内存中处理图像的像素
        private WriteableBitmap? _colorWriteableBitmap;
​
        public MainWindow()
        {
            InitializeComponent();
        }
​
        /// <summary>
        /// 启动按钮点击逻辑,启动摄像头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Start_Button_Click(object sender, RoutedEventArgs e)
        {
            //  创建RealSence上下文,用于查找RealSence摄像头的物理设备
            using Context realsenceContext = new Context();
​
            //  查找当前电脑中连接的所有RealSence摄像头
            var cameraList = realsenceContext.QueryDevices();
​
            if (cameraList.Count <= 0) return;
​
            //  取摄像头列表中的第一个摄像头
            using Device camera = cameraList[0];
​
            //  显示摄像头的名称
            CameraName_TextBlock.Text = camera.Info[CameraInfo.Name].ToString();
            //  显示摄像头的序列号
            CameraSerialNumber.Text = camera.Info[CameraInfo.SerialNumber].ToString();
​
            //  创建Config实例,用于配置摄像头
            using Config cameraConfig = new Config();
​
            //  配置摄像头将采用的图像流,图像篇幅,图像格式,及帧率
            cameraConfig.EnableStream(Stream.Color, 640, 480, Format.Rgb8, 30);
​
            //  初始化管道并启动
            _pipeline = new Pipeline(realsenceContext);
            _pipeline.Start(cameraConfig);
​
            //  初始化WriteablBitmap实例,用于处理内存中的图像
            _colorWriteableBitmap = new WriteableBitmap(640, 480, 96.0, 96.0, PixelFormats.Rgb24, null);
            ColorImage.Source = _colorWriteableBitmap;
​
            //  使用显示器的刷新事件,挂载相关方法,进行图像显示
            CompositionTarget.Rendering += CompositionTarget_Rendering;
​
        }
​
        /// <summary>
        /// 根据显示器的帧率,定时刷新画面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CompositionTarget_Rendering(object? sender, EventArgs e)
        {
            //  等待管道中的帧数据
            using var frames = _pipeline?.WaitForFrames();
​
            //  获取彩色图像帧
            using var colorFrame = frames?.ColorFrame;
​
            //  处理帧数据,将获取的帧数据填充至WriteableBitmap实例,并刷新至显示器中
            if(colorFrame != null)
            {
                _colorWriteableBitmap?.Lock();
                colorFrame.CopyTo(_colorWriteableBitmap!.BackBuffer);
                _colorWriteableBitmap.AddDirtyRect(new Int32Rect(0,0,_colorWriteableBitmap.PixelWidth, _colorWriteableBitmap.PixelHeight));
                _colorWriteableBitmap.Unlock();
            }
        }
​
        /// <summary>
        /// 停止按钮点击逻辑,将帧率刷新事件解除挂载
        /// 释放_pipeLine对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Stop_Button_Click(object sender, RoutedEventArgs e)
        {
            if(_pipeline != null)
            {
                _pipeline.Stop();
            }
​
            CompositionTarget.Rendering -= CompositionTarget_Rendering;
        }
    }
}
 

点击Start按钮,显示如下: