主頁 > .NET開發 > [WPF 學習] 18. 攝像頭(肢解DirectShow)

[WPF 學習] 18. 攝像頭(肢解DirectShow)

2021-02-08 06:13:05 .NET開發

公司的產品需要人臉比對,攝像頭相關的需求如下(突然發現除了英文不太好外,實際上中文也不太好,所以直接上一個介面)

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:
        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 = https://www.cnblogs.com/catzhou/archive/2021/02/07/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 = https://www.cnblogs.com/catzhou/archive/2021/02/07/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],歡迎點贊!

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/257628.html

標籤:.NET技术

上一篇:搭建ASP.NET MVC5框架(2) 創建專案

下一篇:WPF中使用附加屬性解決PasswordBox的資料系結問題

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • WebAPI簡介

    Web體系結構: 有三個核心:資源(resource),URL(統一資源識別符號)和表示 他們的關系是這樣的:一個資源由一個URL進行標識,HTTP客戶端使用URL定位資源,表示是從資源回傳資料,媒體型別是資源回傳的資料格式。 接下來我們說下HTTP. HTTP協議的系統是一種無狀態的方式,使用請求/ ......

    uj5u.com 2020-09-09 22:07:47 more
  • asp.net core 3.1 入口:Program.cs中的Main函式

    本文分析Program.cs 中Main()函式中代碼的運行順序分析asp.net core程式的啟動,重點不是剖析原始碼,而是理清程式開始時執行的順序。到呼叫了哪些實體,哪些法方。asp.net core 3.1 的程式入口在專案Program.cs檔案里,如下。ususing System; us ......

    uj5u.com 2020-09-09 22:07:49 more
  • asp.net網站作為websocket服務端的應用該如何寫

    最近被websocket的一個問題困擾了很久,有一個需求是在web網站中搭建websocket服務。客戶端通過網頁與服務器建立連接,然后服務器根據ip給客戶端網頁發送資訊。 其實,這個需求并不難,只是剛開始對websocket的內容不太了解。上網搜索了一下,有通過asp.net core 實作的、有 ......

    uj5u.com 2020-09-09 22:08:02 more
  • ASP.NET 開源匯入匯出庫Magicodes.IE Docker中使用

    Magicodes.IE在Docker中使用 更新歷史 2019.02.13 【Nuget】版本更新到2.0.2 【匯入】修復單列匯入的Bug,單元測驗“OneColumnImporter_Test”。問題見(https://github.com/dotnetcore/Magicodes.IE/is ......

    uj5u.com 2020-09-09 22:08:05 more
  • 在webform中使用ajax

    如果你用過Asp.net webform, 說明你也算是.NET 開發的老兵了。WEBform應該是2011 2013左右,當時還用visual studio 2005、 visual studio 2008。后來基本都用的是MVC。 如果是新開發的專案,估計沒人會用webform技術。但是有些舊版 ......

    uj5u.com 2020-09-09 22:08:50 more
  • iis添加asp.net網站,訪問提示:由于擴展配置問題而無法提供您請求的

    今天在iis服務器配置asp.net網站,遇到一個問題,記錄一下: 問題:由于擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程式。如果應下載檔案,請添加 MIME 映射。 WindowServer2012服務器,添加角色安裝完.netframework和iis之后,運行aspx頁面 ......

    uj5u.com 2020-09-09 22:10:00 more
  • WebAPI-處理架構

    帶著問題去思考,大家好! 問題1:HTTP請求和回傳相應的HTTP回應資訊之間發生了什么? 1:首先是最底層,托管層,位于WebAPI和底層HTTP堆疊之間 2:其次是 訊息處理程式管道層,這里比如日志和快取。OWIN的參考是將訊息處理程式管道的一些功能下移到堆疊下端的OWIN中間件了。 3:控制器處理 ......

    uj5u.com 2020-09-09 22:11:13 more
  • 微信門戶開發框架-使用指導說明書

    微信門戶應用管理系統,采用基于 MVC + Bootstrap + Ajax + Enterprise Library的技術路線,界面層采用Boostrap + Metronic組合的前端框架,資料訪問層支持Oracle、SQLServer、MySQL、PostgreSQL等資料庫。框架以MVC5,... ......

    uj5u.com 2020-09-09 22:15:18 more
  • WebAPI-HTTP編程模型

    帶著問題去思考,大家好!它是什么?它包含什么?它能干什么? 訊息 HTTP編程模型的核心就是訊息抽象,表示為:HttPRequestMessage,HttpResponseMessage.用于客戶端和服務端之間交換請求和回應訊息。 HttpMethod類包含了一組靜態屬性: private stat ......

    uj5u.com 2020-09-09 22:15:23 more
  • 部署WebApi隨筆

    一、跨域 NuGet參考Microsoft.AspNet.WebApi.Cors WebApiConfig.cs中配置: // Web API 配置和服務 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 二、清除默認回傳XML格式 ......

    uj5u.com 2020-09-09 22:15:48 more
最新发布
  • C#多執行緒學習(二) 如何操縱一個執行緒

    <a href="https://www.cnblogs.com/x-zhi/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2943582/20220801082530.png" alt="" /></...

    uj5u.com 2023-04-19 09:17:20 more
  • C#多執行緒學習(二) 如何操縱一個執行緒

    C#多執行緒學習(二) 如何操縱一個執行緒 執行緒學習第一篇:C#多執行緒學習(一) 多執行緒的相關概念 下面我們就動手來創建一個執行緒,使用Thread類創建執行緒時,只需提供執行緒入口即可。(執行緒入口使程式知道該讓這個執行緒干什么事) 在C#中,執行緒入口是通過ThreadStart代理(delegate)來提供的 ......

    uj5u.com 2023-04-19 09:16:49 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    <a href="https://www.cnblogs.com/huangxincheng/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/214741/20200614104537.png" alt="" /&g...

    uj5u.com 2023-04-18 08:39:04 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    一:背景 1. 講故事 前段時間協助訓練營里的一位朋友分析了一個程式卡死的問題,回過頭來看這個案例比較經典,這篇稍微整理一下供后來者少踩坑吧。 二:WinDbg 分析 1. 為什么會卡死 因為是表單程式,理所當然就是看主執行緒此時正在做什么? 可以用 ~0s ; k 看一下便知。 0:000> k # ......

    uj5u.com 2023-04-18 08:33:10 more
  • SignalR, No Connection with that ID,IIS

    <a href="https://www.cnblogs.com/smartstar/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/u36196.jpg" alt="" /></a>...

    uj5u.com 2023-03-30 17:21:52 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:15:33 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:13:31 more
  • C#遍歷指定檔案夾中所有檔案的3種方法

    <a href="https://www.cnblogs.com/xbhp/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/957602/20230310105611.png" alt="" /></a&...

    uj5u.com 2023-03-27 14:46:55 more
  • C#/VB.NET:如何將PDF轉為PDF/A

    <a href="https://www.cnblogs.com/Carina-baby/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2859233/20220427162558.png" alt="" />...

    uj5u.com 2023-03-27 14:46:35 more
  • 武裝你的WEBAPI-OData聚合查詢

    <a href="https://www.cnblogs.com/podolski/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/616093/20140323000327.png" alt="" /><...

    uj5u.com 2023-03-27 14:46:16 more