首页 > 其他分享 >wpf实现FFmpeg获取摄像头实时画面

wpf实现FFmpeg获取摄像头实时画面

时间:2023-03-21 15:33:39浏览次数:37  
标签:thPlayer FFmpeg Windows System ClassHelp Instance using wpf 摄像头


  1. gitee地址如下

  1. 如何获取摄像头验证码和ip


wpf实现FFmpeg获取摄像头实时画面_wpf


首先获取摄像头底部的验证码及ip(测试使用的是萤石摄像头,需要PC下载萤石客户端查看ip)



wpf实现FFmpeg获取摄像头实时画面_Powered by 金山文档_02


  1. 未连接之前可以通过VLC进行测试


wpf实现FFmpeg获取摄像头实时画面_Powered by 金山文档_03


在左上角(媒体)--》(流)--》(网络)--》输入rtsp链接

如下就是rtsp链接

rtsp://admin:​​[email protected]​​:554/h264/ch1/main/av_stream

测试还可以使用该rtsp链接

​​rtmp://mobliestream.c3tv.com:554/live/goodtv.sdp​​

  1. 项目结构


wpf实现FFmpeg获取摄像头实时画面_ffmpeg_04


  1. 源码

(1)MainWindow.xaml.cs

using FFmpeg.AutoGen;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
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.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FFmpegPlayRtspDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
player1.RtspUrl = "rtsp://admin:[email protected]:554/h264/ch1/main/av_stream";
//player1.RtspUrl = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4";
}
}
}

(2)MainWindow.xaml

<Window x:Class="FFmpegPlayRtspDemo.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:FFmpegPlayRtspDemo"
mc:Ignorable="d"
Title="FFmpeg播放" Height="480" Width="800" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
<Grid>
<local:FFmpegPlayer x:Name="player1"/>


</Grid>
</Window>

(3)FFmpegPlayer.xaml

<UserControl x:Class="FFmpegPlayRtspDemo.FFmpegPlayer"
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:FFmpegPlayRtspDemo"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Image Name="videodest01" Tag="01" Margin="386,210,0,-3"></Image>
<Image Name="videodest02" Tag="02" Margin="0,211,427,3"></Image>
<Image Name="videodest03" Tag="03" Margin="385,4,0,234"></Image>
<Image Name="videodest04" Tag="04" Margin="0,0,428,236"></Image>
</Grid>
</UserControl>

(4)FfmpegPlayer.xaml.cs

using FFmpeg.AutoGen;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
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.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FFmpegPlayRtspDemo
{
/// <summary>
/// FFmpegPlayer.xaml 的交互逻辑
/// </summary>
public partial class FFmpegPlayer : UserControl
{
private string _RtspUrl;

public string RtspUrl
{
get { return _RtspUrl; }
set
{
_RtspUrl = value;
Play();
}
}

public bool CanRun;
Thread thPlayer;

public FFmpegPlayer()
{
InitializeComponent();
}

private void Play()
{
if (string.IsNullOrEmpty(RtspUrl))
return;
thPlayer = new Thread(DeCoding);
thPlayer.SetApartmentState(ApartmentState.MTA);//设置单线程
thPlayer.IsBackground = true;
thPlayer.Start();
}

object _bitMapLocker = new object();
private unsafe void DeCoding()
{
try
{
//if (ClassHelp.Instance.SysSettingModel.Camera1 != 0)
//{
ClassHelp.Instance.FFmpegHelp = new FFmpegHelp();
ClassHelp.Instance.FFmpegHelp.Register();
//}
string strResult = "";
lock (_bitMapLocker)
{
// 更新图片显示
FFmpegHelp.ShowBitmap show = (bmp) =>
{
Dispatcher.BeginInvoke(new Action(() =>
{
Bitmap oldBitMap;
Bitmap autobitmap;

if (bmp != null)
{
oldBitMap = bmp;
autobitmap = bmp;
ImageSource videSource = ImageSourceForBitmap(oldBitMap);
this.videodest01.Source = videSource;
this.videodest02.Source = videSource;
this.videodest03.Source = videSource;
this.videodest04.Source = videSource;
ClassHelp.Instance.IsAlert = false;
}
else
{
ClassHelp.Instance.IsAlert = true;
ClassHelp.Instance.IsNetwork = true;
}
}));
};
ClassHelp.Instance.FFmpegHelp.Start(show, RtspUrl, out strResult);
//Start(show, RtspUrl, out strResult);
if (!string.IsNullOrEmpty(strResult) && ClassHelp.Instance.IsAlert == true)
{
ClassHelp.Instance.IsAlert = false;
ClassHelp.Instance.IsNetwork = false;
this.Dispatcher.Invoke(new Action(() =>
{
MessageBox.Show($"错误信息:{strResult}");
}));
}
}
}
finally
{
thPlayer.DisableComObjectEagerCleanup();
thPlayer = null;
thPlayer = new Thread(DeCoding);
thPlayer.IsBackground = true;
thPlayer.Start();
}
}

[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);

public ImageSource ImageSourceForBitmap(Bitmap bmp)
{
var handle = bmp.GetHbitmap();
try
{
ImageSource newSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
DeleteObject(handle);
return newSource;
}
catch (Exception ex)
{
DeleteObject(handle);
return null;
}
}
}
}
  1. 截图


wpf实现FFmpeg获取摄像头实时画面_wpf_05


标签:thPlayer,FFmpeg,Windows,System,ClassHelp,Instance,using,wpf,摄像头
From: https://blog.51cto.com/u_15738297/6140246

相关文章

  • WPF命令绑定汇总
    1、Button等自带Command属性的控件,直接绑定命令<ButtonHorizontalAlignment="Left"Width="105"Height="32"Command="{BindingClickCommand}"><TextBlockText=......
  • wpf不在主线程的话放到主线程执行
    ///<summary>///刷新页面状态///</summary>///<paramname="overViewState"></param>///<paramname="strValue"></param>......
  • wpf自定义行为库(一)
    起因:我有个相对简单的WPF上位机程序,逻辑还是事件驱动那一套,虽然写的时候方便,但是代码的复用性太差了,并且各个模块的耦合度也很高,修改难度较大,于是我萌生了一个想法,将其改造......
  • Mac mini使用iphone做摄像头
    前言前段时间我将自己的Mac换成了M1芯片的MacMini,由于是个裸机,除了主机啥也没有,所以我都是需要自己diy配件的。键盘鼠标音响耳机啥的都好说,但是摄像头捏? 正言之前就......
  • .NET中的winform、wpf、winui和maui你都知道吗?
    前言    年初.NET工程师的求职者反馈不好找工作,尤其是B/S开发,C/S开发稍微好点。这种情况下有好多小伙伴都想转行了,于是了解了一下JAVA,比.NET还卷,还是走.NET内部转行吧......
  • 能快速构建和定制网络拓扑图的WPF开源项目-NodeNetwork
    大家好,我是沙漠尽头的狼,今天介绍一个WPF开源项目-NodeNetwork,它可以帮助我们快速构建和定制网络拓扑图。一、前言在现代软件开发中,数据可视化和可交互性越来越受到关注。......
  • [linux]uvc摄像头调试
    uvc摄像头调试改分辨率:源码中v412fmt.fmt.pix.width图像宽度v412fmt.fmt.pix.height图像高度/**capturingfromUVC*platform:rk3568*/#include<stdi......
  • ffmpeg 音视频解码 - 封兴旺
    前言:不管用哪种计算机语言,思路总是一样的,主要关注其解码部分,渲染图像的方式有很多种,看自己喜欢。基本知识:FFmpeg库简介: avcodec:音视频编解码核心库;avformat:音视频......
  • WPF 在 MVVM 模式下实现窗口后台代码与ViewModel交互
    在WPFMVVM模式中,UI层基本上与ViewModel通过依赖属性和命令绑定交互。有时候互联网上提供的第三方控件不支持绑定,只能在后台代码中赋值和更新,如何在MVVM模式中对这种......
  • python利用ffmpeg实现声音视频传输
    1.背景由于项目需求,需要用到视频音频同步传输到服务器并获取播放,这里用到了推流的知识,由于项目是python项目,自己django框架还不熟悉,这里代码等着后续给补上2.介绍直播......