公司的产品需要人脸比对,摄像头相关的需求如下(突然发现除了英文不太好外,实际上中文也不太好,所以直接上一个接口)

using System;
using System.Drawing;
using System.Windows.Media;
namespace YK
{
public enum ECameraAngle
{
A0,
A90,
A180,
A270
}
public interface IVideo
{
/// <summary>
/// 摄像头初始化
/// </summary>
/// <param name="index">摄像头序号</param>
/// <param name="angle">摄像头角度</param>
/// <param name="frameWidth">视频输出的宽度</param>
/// <param name="frameHeight">视频输出的高度</param>
/// <param name="errorMessage">初始化不成功,返回错误信息</param>
/// <returns></returns>
void Init(int index, int frameWidth = 640, int frameHeight = 480, ECameraAngle angle = ECameraAngle.A0);
/// <summary>
/// 打开摄像头
/// </summary>
void Play();
/// <summary>
/// 关闭摄像头
/// </summary>
void Stop();
/// <summary>
/// 新帧事件
/// </summary>
event Action<ImageSource> NewFrame; /// <summary>
/// 获取当前帧
/// </summary>
Bitmap GetCurrentFrame();
}
}

接口补充说明如下:

  1. 命名空间yk:是我公司的简称,见笑。
  2. 摄像头序号:公司的产品有时候有多个摄像头
  3. 摄像头角度:公司的产品为了外观好看一点,有时候会转个90度

对应的Wpf的Xaml代码如下:

<Window x:Class="WpfAppVideoCapture.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:WpfAppVideoCapture"
mc:Ignorable="d"
WindowState="Maximized"
Title="MainWindow" Height="800" Width="800">
<Grid>
<Image Stretch="None" Source="{Binding Images}" Name="Video"></Image>
<Button Content="Play" Click="Button_Click_1" Width="200" Height="100" VerticalAlignment="Bottom" Margin="0,0,500,0"></Button>
<Button Content="Capture" Click="Button_Click_2" Width="200" Height="100" VerticalAlignment="Bottom" Margin="0,0,0,0"></Button>
<Button Content="Stop" Click="Button_Click_3" Width="200" Height="100" VerticalAlignment="Bottom" Margin="500,0,0,0"></Button>
</Grid>
</Window>

很简单,就是一个Image控件加三个Button控件(打开、拍照、停止)

对应的后台CS代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media; using YK; namespace WpfAppVideoCapture
{
public partial class MainWindow : Window
{
IVideo VC = new VideoCapture();
VMMain VMMain = new VMMain(); public MainWindow()
{
InitializeComponent();
this.DataContext = VMMain; VC.Init(0,640,480, ECameraAngle.A180);
VC.NewFrame += VC_NewVideoSample;
VC.Play();
} private void VC_NewVideoSample(ImageSource imageSouce) => VMMain.Images = imageSouce; private void Button_Click_1(object sender, RoutedEventArgs e) => VC.Play(); private void Button_Click_2(object sender, RoutedEventArgs e)
{
using var bitmap = VC.GetCurrentFrame();
//人脸识别...
bitmap.Save($@"d:\test\{DateTime.Now.Ticks}.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
} private void Button_Click_3(object sender, RoutedEventArgs e) => VC.Stop(); } public class VMMain : VMBase
{
public ImageSource Images
{
get => G<ImageSource>();
set => S(value); }
}
public class VMBase : INotifyPropertyChanged
{
private readonly Dictionary<string, object> CDField = new Dictionary<string, object>(); public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName] string propertyName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
public T G<T>([CallerMemberName] string propertyName = "")
{
if (!CDField.TryGetValue(propertyName, out object _propertyValue))
{
_propertyValue = default(T);
CDField.Add(propertyName, _propertyValue);
}
return (T)_propertyValue;
}
public void S(object value, [CallerMemberName] string propertyName = "")
{
if (!CDField.ContainsKey(propertyName) || CDField[propertyName] != (object)value)
{
CDField[propertyName] = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}

没太多要说的,关键是实现了IVideo接口的VideoCapture类,下面来详细说说。

一、VideoCapture要用的一些类和接口(大部分对DirectShow封装,从DirectShowLib-2008复制并删减的)

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Security;
namespace YK
{
[ComImport, SuppressUnmanagedCodeSecurity, Guid("56a86891-0ad4-11ce-b03a-0020af0ba770"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IPin
{
}
[ComImport, SuppressUnmanagedCodeSecurity, Guid("56a86895-0ad4-11ce-b03a-0020af0ba770"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IBaseFilter
{
}
[ComImport, SuppressUnmanagedCodeSecurity, Guid("56a868b1-0ad4-11ce-b03a-0020af0ba770"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IMediaControl
{
[PreserveSig]
int Run();
[PreserveSig]
int Stop();
}
[ComImport, SuppressUnmanagedCodeSecurity, Guid("56a8689f-0ad4-11ce-b03a-0020af0ba770"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IFilterGraph
{
[PreserveSig]
int AddFilter([In] IBaseFilter pFilter, [In, MarshalAs(UnmanagedType.LPWStr)] string pName);
[PreserveSig]
int RemoveFilter([In] IBaseFilter pFilter);
[PreserveSig]
int EnumFilters([Out] out object ppEnum);
[PreserveSig]
int FindFilterByName([In, MarshalAs(UnmanagedType.LPWStr)] string pName, [Out] out IBaseFilter ppFilter);
[PreserveSig]
int ConnectDirect([In] IPin ppinOut, [In] IPin ppinIn, [In, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt);
[PreserveSig]
[Obsolete("This method is obsolete; use the IFilterGraph2.ReconnectEx method instead.")]
int Reconnect([In] IPin ppin);
[PreserveSig]
int Disconnect([In] IPin ppin);
[PreserveSig]
int SetDefaultSyncSource();
}
[ComImport, SuppressUnmanagedCodeSecurity, Guid("56a868a9-0ad4-11ce-b03a-0020af0ba770"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IGraphBuilder : IFilterGraph
{
#region IFilterGraph Methods
[PreserveSig]
new int AddFilter([In] IBaseFilter pFilter, [In, MarshalAs(UnmanagedType.LPWStr)] string pName);
[PreserveSig]
new int RemoveFilter([In] IBaseFilter pFilter);
[PreserveSig]
new int EnumFilters([Out] out object ppEnum);
[PreserveSig]
new int FindFilterByName([In, MarshalAs(UnmanagedType.LPWStr)] string pName, [Out] out IBaseFilter ppFilter);
[PreserveSig]
new int ConnectDirect([In] IPin ppinOut, [In] IPin ppinIn, [In, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt);
[PreserveSig]
new int Reconnect([In] IPin ppin);
[PreserveSig]
new int Disconnect([In] IPin ppin);
[PreserveSig]
new int SetDefaultSyncSource();
#endregion
[PreserveSig]
int Connect([In] IPin ppinOut, [In] IPin ppinIn);
[PreserveSig]
int Render([In] IPin ppinOut);
[PreserveSig]
int RenderFile([In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFile, [In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrPlayList);
[PreserveSig]
int AddSourceFilter([In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFileName, [In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFilterName, [Out] out IBaseFilter ppFilter);
[PreserveSig]
int SetLogFile(IntPtr hFile); // DWORD_PTR
[PreserveSig]
int Abort();
[PreserveSig]
int ShouldOperationContinue();
}
[ComImport, SuppressUnmanagedCodeSecurity, Guid("36b73882-c2c8-11cf-8b46-00805f6cef60"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IGraphBuilder2 : IGraphBuilder
{
#region IFilterGraph Methods
[PreserveSig]
new int AddFilter([In] IBaseFilter pFilter, [In, MarshalAs(UnmanagedType.LPWStr)] string pName);
[PreserveSig]
new int RemoveFilter([In] IBaseFilter pFilter);
[PreserveSig]
new int EnumFilters([Out] out object ppEnum);
[PreserveSig]
new int FindFilterByName([In, MarshalAs(UnmanagedType.LPWStr)] string pName, [Out] out IBaseFilter ppFilter);
[PreserveSig]
new int ConnectDirect([In] IPin ppinOut, [In] IPin ppinIn, [In, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt);
[PreserveSig]
new int Reconnect([In] IPin ppin);
[PreserveSig]
new int Disconnect([In] IPin ppin);
[PreserveSig]
new int SetDefaultSyncSource();
#endregion
#region IGraphBuilder Method
[PreserveSig]
new int Connect([In] IPin ppinOut, [In] IPin ppinIn);
[PreserveSig]
new int Render([In] IPin ppinOut);
[PreserveSig]
new int RenderFile([In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFile, [In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrPlayList);
[PreserveSig]
new int AddSourceFilter([In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFileName, [In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFilterName, [Out] out IBaseFilter ppFilter);
[PreserveSig]
new int SetLogFile(IntPtr hFile); // DWORD_PTR
[PreserveSig]
new int Abort();
[PreserveSig]
new int ShouldOperationContinue();
#endregion
[PreserveSig]
int AddSourceFilterForMoniker([In] IMoniker pMoniker, [In] IBindCtx pCtx, [In, MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFilterName, [Out] out IBaseFilter ppFilter);
}
[ComImport, SuppressUnmanagedCodeSecurity, Guid("93E5A4E0-2D50-11d2-ABFA-00A0C9C6E38D"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ICaptureGraphBuilder2
{
[PreserveSig]
int SetFiltergraph([In] IGraphBuilder pfg);
[PreserveSig]
int GetFiltergraph([Out] out IGraphBuilder ppfg);
[PreserveSig]
int SetOutputFileName([In, MarshalAs(UnmanagedType.LPStruct)] Guid pType, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrFile, [Out] out IBaseFilter ppbf, [Out] out object ppSink);
[PreserveSig]
int FindInterface([In, MarshalAs(UnmanagedType.LPStruct)] Guid pCategory, [In, MarshalAs(UnmanagedType.LPStruct)] Guid pType, [In] IBaseFilter pbf, [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid, [Out, MarshalAs(UnmanagedType.IUnknown)] out object ppint);
[PreserveSig]
int RenderStream([In, MarshalAs(UnmanagedType.LPStruct)] Guid PinCategory, [In, MarshalAs(UnmanagedType.LPStruct)] Guid MediaType, [In, MarshalAs(UnmanagedType.IUnknown)] object pSource, [In] IBaseFilter pfCompressor, [In] IBaseFilter pfRenderer);
} [ComImport, SuppressUnmanagedCodeSecurity, Guid("6B652FFF-11FE-4fce-92AD-0266B5D7C78F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISampleGrabber
{
[PreserveSig]
int SetOneShot([In, MarshalAs(UnmanagedType.Bool)] bool OneShot);
[PreserveSig]
int SetMediaType([In, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt);
[PreserveSig]
int GetConnectedMediaType([Out, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt);
[PreserveSig]
int SetBufferSamples([In, MarshalAs(UnmanagedType.Bool)] bool BufferThem);
[PreserveSig]
int GetCurrentBuffer(ref int pBufferSize, IntPtr pBuffer);
[PreserveSig]
int GetCurrentSample(out IntPtr ppSample);
[PreserveSig]
int SetCallback(ISampleGrabberCB pCallback, int WhichMethodToCallback);
}
[ComImport, SuppressUnmanagedCodeSecurity, Guid("0579154A-2B53-4994-B0D0-E773148EFF85"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISampleGrabberCB
{
[PreserveSig]
int SampleCB(double SampleTime, IntPtr pSample);
[PreserveSig]
int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen);
}
[ComImport, SuppressUnmanagedCodeSecurity, Guid("29840822-5B84-11D0-BD3B-00A0C911CE86"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ICreateDevEnum
{
[PreserveSig]
int CreateClassEnumerator([In, MarshalAs(UnmanagedType.LPStruct)] Guid pType, [Out] out IEnumMoniker ppEnumMoniker, [In] int dwFlags);
}
[ComImport, SuppressUnmanagedCodeSecurity, Guid("C6E13340-30AC-11d0-A18C-00A0C9118956"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IAMStreamConfig
{
[PreserveSig]
int SetFormat([In, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt);
[PreserveSig]
int GetFormat([Out] out AMMediaType pmt);
}
[StructLayout(LayoutKind.Sequential)]
public class AMMediaType
{
public Guid majorType;
public Guid subType;
[MarshalAs(UnmanagedType.Bool)] public bool fixedSizeSamples;
[MarshalAs(UnmanagedType.Bool)] public bool temporalCompression;
public int sampleSize;
public Guid formatType;
public IntPtr unkPtr; // IUnknown Pointer
public int formatSize;
public IntPtr formatPtr; // Pointer to a buff determined by formatType 指向VideoInfo
}
[ComImport, Guid("e436ebb8-524f-11ce-9f53-0020af0ba770")]
public class FilterGraphNoThread
{
}
[ComImport, Guid("BF87B6E1-8C27-11d0-B3F0-00AA003761C5")]
public class CaptureGraphBuilder2
{
}
[StructLayout(LayoutKind.Sequential)]
public class DsRect
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
public class VideoInfoHeader
{
public DsRect SrcRect;
public DsRect TargetRect;
public int BitRate;
public int BitErrorRate;
public long AvgTimePerFrame;
public BitmapInfoHeader BmiHeader;
}
[StructLayout(LayoutKind.Sequential, Pack = 2)]
public class BitmapInfoHeader
{
public int Size;//本结构大小40字节
public int Width;
public int Height;
public short Planes;
public short BitCount;
public int Compression;
public int ImageSize;
public int XPelsPerMeter;
public int YPelsPerMeter;
public int ClrUsed;
public int ClrImportant;
}
[ComImport, Guid("C1F400A0-3F08-11d3-9F0B-006008039E37")]
public class SampleGrabber
{
}
[ComImport, Guid("62BE5D10-60EB-11d0-BD3B-00A0C911CE86")]
public class CreateDevEnum
{
}
}

二、VideoCapture的using

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

三、VideoCapture的继承的接口

    public class VideoCapture : ISampleGrabberCB, IDisposable, IVideo

四、VideoCapture的一些字段和一个Com封装

        [DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory")]
private static extern void CopyMemory(IntPtr destination, IntPtr source, [MarshalAs(UnmanagedType.U4)] int length); private readonly Guid Capture = new Guid(0xfb6c4281, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba);
private readonly Guid Video = new Guid(0x73646976, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
private readonly Guid VideoInfo = new Guid(0x05589f80, 0xc356, 0x11ce, 0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a);
private readonly Guid RGB24 = new Guid(0xe436eb7d, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70);
private readonly Guid VideoInputDevice = new Guid(0x860BB310, 0x5D01, 0x11d0, 0xBD, 0x3B, 0x00, 0xA0, 0xC9, 0x11, 0xCE, 0x86);
private readonly IMediaControl _MediaControl = (IMediaControl)new FilterGraphNoThread();
private readonly ISampleGrabber _SampleGrabber = (ISampleGrabber)new SampleGrabber();
private readonly BitmapPalette _BitmapPalette = new BitmapPalette(new List<System.Windows.Media.Color> { Colors.Red, Colors.Blue, Colors.Green }); private int _FrameWidth, _FrameHeight, _FrameBufferSize;
private IntPtr _PSampleBuffer;
private Rectangle _RectangleSource, _RectangleDestination;
private RotateFlipType _RotateFlipType; private bool _Grabbing; public event Action<ImageSource> NewFrame;

五、实现IVideo的Init方法

        public void Init(int cameraIndex, int frameWidth = 640, int frameHeight = 480, ECameraAngle angle = ECameraAngle.A0)
{
_FrameWidth = frameWidth;
_FrameHeight = frameHeight;
_FrameBufferSize = frameWidth * frameHeight * 3;
_PSampleBuffer = Marshal.AllocCoTaskMem(_FrameBufferSize);
_RectangleSource = new Rectangle(0, 0, frameWidth, frameHeight);
switch (angle)
{
case ECameraAngle.A0:
_RectangleDestination = _RectangleSource;
_RotateFlipType = RotateFlipType.RotateNoneFlipXY;
break;
case ECameraAngle.A90:
_RotateFlipType = RotateFlipType.Rotate90FlipNone;
_RectangleDestination = new Rectangle(0, 0, _FrameHeight, _FrameWidth);
break;
case ECameraAngle.A270:
_RotateFlipType = RotateFlipType.Rotate270FlipNone;
_RectangleDestination = new Rectangle(0, 0, _FrameHeight, _FrameWidth);
break;
case ECameraAngle.A180:
_RotateFlipType = RotateFlipType.RotateNoneFlipNone;
break;
} var graphBuilder = (IGraphBuilder)_MediaControl;
var captureGraphBuilder2 = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
int hr = captureGraphBuilder2.SetFiltergraph(graphBuilder);
ThrowExceptionForHR(hr); //此方法可以将 source filter 上的 output pin 连接到 sink filter,也可以通过中间 filter 连接。
hr = captureGraphBuilder2.RenderStream(Capture,//[in]指向 AMPROPERTY_PIN_CATEGORY 属性集中的 pin 类别的指针(请参见 Pin属性集)。PIN_CATEGORY_CAPTURE、PIN_CATEGORY_PREVIEW、PIN_CATEGORY_CC
Video,//[in]指向主要类型 GUID 的指针,该 GUID 指定输出 pin 的媒体类型;或 NULL 以使用任何 pin,而与媒体类型无关。有关可能值的列表,请参考 Media Types and Sub Types。
SetupCamera(cameraIndex, captureGraphBuilder2, (IGraphBuilder2)graphBuilder),//[in]指定一个指向连接的起始 filter 或输出 pin 的指针。
SetupSampleGrabber(graphBuilder, _SampleGrabber),//[in]指向中间 filter (例如压缩 filter)的 IBaseFilter 接口的指针。可以为 NULL。
null);//[in]指向 sink filter(例如 renderer 或 mux filter)的 IBaseFilter 接口的指针。如果值为 NULL,则该方法使用默认的 renderer(渲染器)。
ThrowExceptionForHR(hr);
Marshal.ReleaseComObject(captureGraphBuilder2);
}

除了一些字段的初始化以外,就是创建CamerFilter和SampleGrabberFilter,并把这两个Filter加入到FilterGraphNoThread,最后由CaptureGraphBuilder2揉一揉。

  1. CamerFilter创建的代码是这样的
        private IBaseFilter SetupCamera(int cameraIndex, ICaptureGraphBuilder2 captureGraphBuilder2, IGraphBuilder2 graphBuilder2)
{
ICreateDevEnum enumDev = (ICreateDevEnum)new CreateDevEnum();
int hr = enumDev.CreateClassEnumerator(VideoInputDevice, out IEnumMoniker enumMon, 0);
ThrowExceptionForHR(hr); IMoniker[] mon = new IMoniker[1];
if (cameraIndex > 0)
{
hr = enumMon.Skip(cameraIndex);
ThrowExceptionForHR(hr);
}
hr = enumMon.Next(1, mon, IntPtr.Zero);
ThrowExceptionForHR(hr); hr = graphBuilder2.AddSourceFilterForMoniker(mon[0], null, "Camera", out IBaseFilter filterCamera);
ThrowExceptionForHR(hr); if (filterCamera is null)
throw new Exception($"无法连接摄像头{cameraIndex}"); hr = captureGraphBuilder2.FindInterface(Capture, Video, filterCamera, typeof(IAMStreamConfig).GUID, out var streamConfig);
ThrowExceptionForHR(hr); var videoStreamConfig = streamConfig as IAMStreamConfig; hr = videoStreamConfig.GetFormat(out var media);
ThrowExceptionForHR(hr); var videoInfo = new VideoInfoHeader();
Marshal.PtrToStructure(media.formatPtr, videoInfo);
videoInfo.BmiHeader.Width = _FrameWidth;
videoInfo.BmiHeader.Height = _FrameHeight;
Marshal.StructureToPtr(videoInfo, media.formatPtr, false); hr = videoStreamConfig.SetFormat(media);
Marshal.FreeHGlobal(media.formatPtr);
if (hr < 0)
throw new Exception($"摄像头不支持{_FrameWidth}*{_FrameHeight}"); return filterCamera; }
  1. SampleGrabberFilter创建的代码是这样的
        private IBaseFilter SetupSampleGrabber(IGraphBuilder graphBuilder, ISampleGrabber sampleGrabber)
{
var mediaType = new AMMediaType
{
majorType = Video,
subType = RGB24,
formatType = VideoInfo
}; int hr = sampleGrabber.SetMediaType(mediaType);
ThrowExceptionForHR(hr); hr = sampleGrabber.SetBufferSamples(true);
ThrowExceptionForHR(hr); var filterGrabber = _SampleGrabber as IBaseFilter;
hr = graphBuilder.AddFilter(filterGrabber, "SampleGrabber");
ThrowExceptionForHR(hr); return filterGrabber;
}

3.还有一个报异常方法(讲真,只要是错误的hr我肯定不知道什么意思)

        private void ThrowExceptionForHR(int hr, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0)
{
if (hr < 0)
throw new Exception($"{memberName}/{lineNumber}错误:" + hr);
}

六、实现IVideo的Play和Stop方法

        public void Play()
{
if (!Application.Current.Dispatcher.CheckAccess())
{
Application.Current.Dispatcher.Invoke(() => Play());
return;
}
_SampleGrabber.SetCallback(this, 1);
_MediaControl.Run();
}
public async void Stop()
{
if (!Application.Current.Dispatcher.CheckAccess())
{
Application.Current.Dispatcher.Invoke(() => Stop());
return;
}
_SampleGrabber.SetCallback(null, 1); while (_Grabbing)
await Task.Delay(5);
NewFrame?.Invoke(null);
_MediaControl.Stop();
}

六、实现ISampleGrabberCB的第0个方法SampleCB(VideoCapture不调用,所以直接返回0)

public int SampleCB(double SampleTime, IntPtr pSample) => 0;

七、实现ISampleGrabberCB的第1个方法BufferCB

        public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
if (NewFrame != null)
{
_Grabbing = true;
if (_RotateFlipType == RotateFlipType.RotateNoneFlipNone)
Application.Current.Dispatcher.Invoke(() => NewFrame?.Invoke(BitmapSource.Create(_FrameWidth, _FrameHeight, 96, 96, PixelFormats.Bgr24, _BitmapPalette, pBuffer, _FrameBufferSize, _FrameWidth * 3)));
else
{
using var bitmap = new Bitmap(_FrameWidth, _FrameHeight);
var bmpData = bitmap.LockBits(_RectangleSource, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
CopyMemory(bmpData.Scan0, pBuffer, _FrameBufferSize);
bitmap.UnlockBits(bmpData);
bitmap.RotateFlip(_RotateFlipType);
bmpData = bitmap.LockBits(_RectangleDestination, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Application.Current.Dispatcher.Invoke(() => NewFrame?.Invoke(BitmapSource.Create(_RectangleDestination.Width, _RectangleDestination.Height, 96, 96, PixelFormats.Bgr24, _BitmapPalette, bmpData.Scan0, _FrameBufferSize, _RectangleDestination.Width * 3)));
bitmap.UnlockBits(bmpData);
}
_Grabbing = false;
}
return 0;
}

八、最后实现IDisposable的Dispose方法

        public void Dispose()
{
Marshal.ReleaseComObject(_MediaControl);
Marshal.ReleaseComObject(_SampleGrabber);
if (_PSampleBuffer != IntPtr.Zero)
Marshal.FreeCoTaskMem(_PSampleBuffer);
}

看了一下,VideoCapture类不到600行,复制起来也容易;编译后17K,相当小巧。虽然说是WPF的,其实稍加改动Winform也能用。

哦,整个项目发布在https://gitee.com/catzhou/video-capture,欢迎点赞!

[WPF 学习] 18. 摄像头(肢解DirectShow)的更多相关文章

  1. WPF学习之资源-Resources

    WPF学习之资源-Resources WPF通过资源来保存一些可以被重复利用的样式,对象定义以及一些传统的资源如二进制数据,图片等等,而在其支持上也更能体现出这些资源定义的优越性.比如通过Resour ...

  2. WPF学习之路初识

    WPF学习之路初识   WPF 介绍 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 Windows Presentation Found ...

  3. WPF学习资源整理

    WPF(WindowsPresentation Foundation)是微软推出的基于Windows Vista的用户界面框架,属于.NET Framework 3.0的一部分.它提供了统一的编程模型 ...

  4. WPF学习07:MVVM 预备知识之数据绑定

    MVVM是一种模式,而WPF的数据绑定机制是一种WPF内建的功能集,两者是不相关的. 但是,借助WPF各种内建功能集,如数据绑定.命令.数据模板,我们可以高效的在WPF上实现MVVM.因此,我们需要对 ...

  5. C# 基于Directshow.Net lib库 USB摄像头使用DirectShow.NET获取摄像头视频流

    https://blog.csdn.net/u010118312/article/details/91766787 https://download.csdn.net/download/u010118 ...

  6. 【WPF学习】第五十七章 使用代码创建故事板

    在“[WPF学习]第五十章 故事板”中讨论了如何使用代码创建简单动画,以及如何使用XAML标记构建更复杂的故事板——具有多个动画以及播放控制功能.但有时采用更复杂的故事板例程,并在代码中实现全部复杂功 ...

  7. WPF学习开发客户端软件-任务助手(下 2015年2月4日代码更新)

    时光如梭,距离第一次写的 WPF学习开发客户端软件-任务助手(已上传源码)  已有三个多月,期间我断断续续地对该项目做了优化.完善等等工作,现在重新向大家介绍一下,希望各位可以使用,本软件以实用性为主 ...

  8. WPF学习05:2D绘图 使用Transform进行控件变形

    在WPF学习04:2D绘图 使用Shape绘基本图形中,我们了解了如何绘制基本的图形. 这一次,我们进一步,研究如何将图形变形. 例子 一个三角形,经Transform形成组合图形: XAML代码: ...

  9. WPF学习拾遗(二)TextBlock换行

    原文:WPF学习拾遗(二)TextBlock换行 下午在帮组里的同事解决一个小问题,为了以后方便,把就把它收集一下吧. 新建一个TextBlock作为最基础的一个控件,他所携带的功能相对于其他的控件要 ...

随机推荐

  1. Server 2012 R2 Standard 安装运行PCS7时出现“无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-l1-1-0.dll”解决方法

    网上看到了这篇文章https://www.jianshu.com/p/21f4bb8b5502,根据思路自己尝试,解决了丢失的问题.提示[计算机中丢失api-ms-win-crt-runtime-l1 ...

  2. 【Linux】实现端口转发的rinetd

    Linux下端口转发一般都使用iptables来实现,使用iptables可以很容易将TCP和UDP端口从防火墙转发到内部主机上.但是如果需要将流量从专用地址转发到不在您当前网络上的机器上,可尝试另一 ...

  3. CWE 4.3:强化你的数据自我保护能力

    摘要:如何通过软件自动的检查法规中涉及的数据保护, 新版的CWE 4.3 给出了一个解决途径. 1. 按照惯例,先说故事 用12月初在深圳参加的"全球C++及系统软件技术大会"里C ...

  4. 入门OJ:简单的网络游戏

    题目描述 在某款极具技术含量的网络游戏中,佳佳靠着他的聪明智慧垄断了游戏中的油田系统.油田里有许多油井,这些油井排成一个M*N的矩形.每个油井都有一个固定的采油量.每两个相邻的油井之间有一条公路,这些 ...

  5. 量子化学Gaussian技术实战课 2021年4月9号--12号 远程在线教学

    材料模拟分子动力学课程 3月19号--22号 远程在线课 lammps分子动力学课程 3月12号--15号 远程在线课 第一性原理VASP实战课 3月25号-28号 远程在线课 量子化学Gaussia ...

  6. 阿里云RDS物理备份恢复到本地

    一:业务场景 验证阿里云备份文件可用性 二:恢复到本地过程中遇到的问题 1.修改密码报错 2.自定义函数不可用 三:恢复步骤 1.xtrabackup安装使用 请参考:https://www.cnbl ...

  7. Redis 核心篇:唯快不破的秘密

    天下武功,无坚不摧,唯快不破! 学习一个技术,通常只接触了零散的技术点,没有在脑海里建立一个完整的知识框架和架构体系,没有系统观.这样会很吃力,而且会出现一看好像自己会,过后就忘记,一脸懵逼. 跟着「 ...

  8. MySQL调优之查询优化

    一.查询慢的原因 1.网络 (1)网络丢包,重传 这个比较容易理解.当SQL 从客户端发送到数据库,执行完毕,数据库将结果返回给客户端,这个将数据返回给客户端的过程本质是网络包传输.因为链路的不稳定性 ...

  9. 3、剑指offer-数组——数组中重复的数字

    *题目描述* **在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输 ...

  10. new() 和 make() 的区别 var arr1 = new([5]int) var arr2 [5]int

    Effective Go - The Go Programming Language https://golang.org/doc/effective_go.html#allocation_new A ...