• [WPF 学习] 18. 摄像头(肢解DirectShow)


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

    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>
            /// <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:	est{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也能用。

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

  • 相关阅读:
    WPF可视对象变换(RenderTransform)-----RotateTransform、TranslateTransform、ScaleTransform
    WPF 基本图形
    C#创建DataTable
    WPFの触发器详解
    WPF绑定のRelativeSource
    287. Find the Duplicate Number
    238. Product of Array Except Self
    127. Word Ladder
    674. Longest Continuous Increasing Subsequence
    621. Task Scheduler
  • 原文地址:https://www.cnblogs.com/catzhou/p/14385633.html
Copyright © 2020-2023  润新知