WPF实现获取摄像头帧图的代码示例

 更新时间:2024年02月01日 08:46:19   作者:黑白分明  
这篇文章主要介绍了如何利用WPF实现获取摄像头帧图的代码示例,文中的示例代码讲解详细,对我们学习或工作有一定帮助,感兴趣的可以参考一下

一.前言

项目需求:支持uvc的摄像头,取出其画面帧图,进行相关叠加,并重新展示在image控件上

环境:用的是.net framework 4.8.1 。 当然.net6 也支持

使用的第三方插件:AForge.Video.DirectShow

备注:winform和uwp都可以进行参考

二.项目demo代码

MainWindow.xaml部分

<Window x:Class="WpfApp2.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
xmlns:vlc="clr-namespace:LibVLCSharp.WPF;assembly=LibVLCSharp.WPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> <Grid> <Image x:Name="video" Margin="0,40,0,0"/> <Button x:Name="StartButton" Content="start" HorizontalAlignment="Left" Margin="39,11,0,0" VerticalAlignment="Top" Click="StartButton_event"/> <Button x:Name="StopButton" Content="stop" HorizontalAlignment="Left" Margin="88,10,0,0" VerticalAlignment="Top" Click="StopButton_event"/> </Grid> </Window>

MainWindow.cs部分

using AForge.Video;
using AForge.Video.DirectShow;
using LibVLCSharp.Shared;
using LoggerServiceFK;
using SkiaSharp;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media.Imaging;

namespace WpfApp2 {
/// 
/// MainWindow.xaml 的交互逻辑
/// 
public partial class MainWindow : Window
{
private VideoCaptureDevice videoSource;
private bool isCapturing = false;

  public MainWindow()
    {
        InitializeComponent(); 
    }

    private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        if (isCapturing)
        {
            // Convert AForge.NET Framework's Bitmap to WPF's BitmapSource
            Bitmap aforgeBitmap = (Bitmap)eventArgs.Frame.Clone();
         
            // Display the video frame in an Image control
            Dispatcher.Invoke(() =>
            {
				//这里就可以对图片进行相关叠加操作,再赋值给source				
                video.Source = Convert(aforgeBitmap);
            });
        }
    }

    public static BitmapSource Convert(Bitmap bitmap)
    {
        if (bitmap == null)
        {
            throw new ArgumentNullException(nameof(bitmap));
        }

        // 获取 Bitmap 的 HBitmap 句柄
        IntPtr hBitmap = bitmap.GetHbitmap();

        // 使用 CreateBitmapSourceFromHBitmap 方法创建 BitmapSource
        BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
            hBitmap,
            IntPtr.Zero,
            Int32Rect.Empty,
            BitmapSizeOptions.FromEmptyOptions());

        // 释放 Bitmap 的 HBitmap 句柄
        NativeMethods.DeleteObject(hBitmap);

        return bitmapSource;
    }

    private static class NativeMethods
    {
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        public static extern bool DeleteObject(IntPtr hObject);
    }
    private void StartButton_event(object sender, RoutedEventArgs e)
    {
		//这里获取usb摄像头的地方
        FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        if (videoDevices.Count > 0)
        {
            //这里默认选择设备列表[0],如果是笔记本外接usb摄像头要取数组[1]
            videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
            videoSource.NewFrame += VideoSource_NewFrame;
            videoSource.Start();

            isCapturing = true;
        }
    }

    private void StopButton_event(object sender, RoutedEventArgs e)
    {
        if (videoSource != null && videoSource.IsRunning)
        {
		//SignalToStop()停止信号就行,使用stop()方法会导致更新线程卡死
            videoSource.SignalToStop();
            isCapturing = false;
        }
    }
}
}

上面这部分代码就是取图片帧的方法,基本延迟在200ms左右,比vlc插件调取更快。

三.需要定制化下,如何优化此代码

demo所展示的只是基本取图,如要在取图的基础上 叠加定制化内容并不影响取图效率,该如何考虑?

本人分享一个解决办法:

首先不使用Bitmap 类型,改为SKBitmap类型———SKBitmap 使用的是 SkiaSharp的库

把Bitmap转成SKBitmap类型

		var CurrentBitmap = new SKBitmap(640,480);
		CurrentBitmap=aforgeBitmap.ToSKBitmap();

需要定制化的内容 使用SKBitmap类型的指针进行处理,速度更快——此处根据项目本身进行处理

定制后的SKBitmap类型内容再转换成WriteableBitmap类型,使用WritePixels方法进行构成,video.source可以进行读取

//参考例子
var writeableBitmap = new WriteableBitmap(640, 480, 96, 96, PixelFormats.Bgr32, null);
writeableBitmap.WritePixels(new Int32Rect(0, 0, skbitmap.Width, skbitmap.Height), skbitmap.GetPixels(), skbitmap.RowBytes * skbitmap.Height, skbitmap.RowBytes);

这个定制转换的效果与demo展示的效率相当

四. demo展示

以上就是WPF实现获取摄像头帧图的代码示例的详细内容,更多关于WPF取摄像头帧图的资料请关注脚本之家其它相关文章!

相关文章

  • c#枚举值增加特性说明(推荐)

    c#枚举值增加特性说明(推荐)

    这篇文章主要介绍了c#枚举值增加特性说明(推荐),需要的朋友可以参考下
    2017-05-05
  • 一个可携带附加消息的增强消息框MessageBoxEx

    一个可携带附加消息的增强消息框MessageBoxEx

    一个可携带附加消息的增强消息框MessageBoxEx分享给大家,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • c#消息提示框messagebox的详解及使用

    c#消息提示框messagebox的详解及使用

    这篇文章主要介绍了c#消息提示框messagebox的详解及使用的相关资料,需要的朋友可以参考下
    2017-03-03
  • 解析如何使用反射调用类型成员 方法,字段,属性

    解析如何使用反射调用类型成员 方法,字段,属性

    本篇文章是对使用反射调用类型成员 方法,字段,属性进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • 使用C#发送Http请求实现模拟登陆实例

    使用C#发送Http请求实现模拟登陆实例

    本文主要介绍了使用C#发送Http请求实现模拟登陆实例,模拟登陆的原理简单,想要了解的朋友可以了解一下。
    2016-10-10
  • C#数据结构与算法揭秘二 线性结构

    C#数据结构与算法揭秘二 线性结构

    本文中,我们讨论了什么是线性结构,线性结构有哪些特点,并且详细介绍了一个最简单线性结构顺序表,并且通过源代码对她进行一些列的分析,最后还举了两个例子,让我们更好的理解顺序表
    2012-11-11
  • C#实现批量压缩和解压缩的示例代码

    C#实现批量压缩和解压缩的示例代码

    这篇文章主要为大家详细介绍了如何利用C#实现批量压缩和解压缩的功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
    2022-12-12
  • C#使用Tesseract进行中文识别的详细步骤

    C#使用Tesseract进行中文识别的详细步骤

    文章介绍了如何在C#中使用Tesseract进行中文识别,包括环境准备、安装NuGet包、配置语言数据文件、基本使用代码、核心API详解、高级功能与优化等步骤,需要的朋友可以参考下
    2026-03-03
  • C#集合之字典的用法

    C#集合之字典的用法

    这篇文章介绍了C#集合之字典的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C# SQLite库使用技巧

    C# SQLite库使用技巧

    SQLite是一个开源、免费的小型RDBMS(关系型数据库),能独立运行、无服务器、零配置、支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准。下面跟随小编一起看下C# SQLite库使用
    2022-01-01

最新评论