Exemplo n.º 1
0
 public FrameGrabber(DsDevice camDevice)
 {
     IFilterGraph2 filterGraph;
     ICaptureGraphBuilder2 graphBuilder;
     IBaseFilter camBase, nullRenderer;
     ISampleGrabber sampleGrabber;
     filterGraph = new FilterGraph() as IFilterGraph2;
     mediaCtrl = filterGraph as IMediaControl;
     graphBuilder = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;
     HRCheck(graphBuilder.SetFiltergraph(filterGraph));
     // Add camera
     HRCheck(filterGraph.AddSourceFilterForMoniker(
         camDevice.Mon, null, camDevice.Name, out camBase));
     // Add sample grabber
     sampleGrabber = new SampleGrabber() as ISampleGrabber;
     var mType = new AMMediaType()
     {
         majorType = MediaType.Video,
         subType = MediaSubType.RGB24,
         formatType = FormatType.VideoInfo
     };
     HRCheck(sampleGrabber.SetMediaType(mType));
     DsUtils.FreeAMMediaType(mType);
     HRCheck(sampleGrabber.SetCallback(this, 1));
     HRCheck(filterGraph.AddFilter(sampleGrabber as IBaseFilter, "CamGrabber"));
     // Add null renderer
     nullRenderer = new NullRenderer() as IBaseFilter;
     HRCheck(filterGraph.AddFilter(nullRenderer, "Null renderer"));
     // Render the webcam through the grabber and the renderer
     HRCheck(graphBuilder.RenderStream(PinCategory.Capture, MediaType.Video,
         camBase, sampleGrabber as IBaseFilter, nullRenderer));
     // Get resulting picture size
     mType = new AMMediaType();
     HRCheck(sampleGrabber.GetConnectedMediaType(mType));
     if (mType.formatType != FormatType.VideoInfo || mType.formatPtr == IntPtr.Zero)
     {
         throw new NotSupportedException("Unknown grabber media format");
     }
     var videoInfoHeader = Marshal.PtrToStructure(mType.formatPtr,
         typeof(VideoInfoHeader)) as VideoInfoHeader;
     width = videoInfoHeader.BmiHeader.Width;
     height = videoInfoHeader.BmiHeader.Height;
     Console.WriteLine("{0} x {1}", width, height); 
     stride = width * (videoInfoHeader.BmiHeader.BitCount / 8);
     DsUtils.FreeAMMediaType(mType);
     HRCheck(mediaCtrl.Run());
 }
Exemplo n.º 2
0
        public void PrepareCapture(int i_width, int i_height, float i_frame_rate)
        {
            const int BBP = 32;

            //既にキャプチャ中なら諦める。
            if (this.m_graphi_active)
            {
                throw new Exception();
            }
            //現在確保中のグラフインスタンスを全て削除
            CleanupGraphiObjects();

            int            hr;
            ISampleGrabber sampGrabber = null;
            IBaseFilter    capFilter   = null;
            IPin           pSampleIn   = null;

            //グラフビルダを作る。
            this.m_FilterGraph = new FilterGraph() as IFilterGraph2;

            try
            {
                //フィルタグラフにキャプチャを追加して、capFilterにピンを受け取る。
                hr = m_FilterGraph.AddSourceFilterForMoniker(this.m_dev.Mon, null, this.m_dev.Name, out capFilter);
                DsError.ThrowExceptionForHR(hr);

                //stillピンを探す
                m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0);
                //stillピンが無ければPreviewを探す。
                if (m_pinStill == null)
                {
                    m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0);
                }
                // Still haven't found one.  Need to put a splitter in so we have
                // one stream to capture the bitmap from, and one to display.  Ok, we
                // don't *have* to do it that way, but we are going to anyway.
                if (m_pinStill == null)
                {
                    // There is no still pin
                    m_VidControl = null;
                    m_pinStill   = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);
                }
                else
                {
                    // Get a control pointer (used in Click())
                    m_VidControl = capFilter as IAMVideoControl;

                    m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);
                }

                if (i_height + i_width + BBP > 0)
                {
                    SetConfigParms(m_pinStill, i_width, i_height, i_frame_rate, BBP);
                }

                // Get the SampleGrabber interface
                sampGrabber = new SampleGrabber() as ISampleGrabber;

                //sampGrabberの設定
                IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter;
                ConfigureSampleGrabber(sampGrabber);

                pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);

                // Add the sample grabber to the graph
                hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber");
                DsError.ThrowExceptionForHR(hr);

                if (m_VidControl == null)
                {
                    // Connect the Still pin to the sample grabber
                    hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
                    DsError.ThrowExceptionForHR(hr);
                }
                else
                {
                    // Connect the Still pin to the sample grabber
                    hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
                    DsError.ThrowExceptionForHR(hr);
                }
                hr = sampGrabber.GetConnectedMediaType(this._capture_mediatype);
                DsError.ThrowExceptionForHR(hr);
                //ビデオフォーマット等の更新
                upateVideoInfo(sampGrabber);
            }
            finally
            {
                if (sampGrabber != null)
                {
                    Marshal.ReleaseComObject(sampGrabber);
                    sampGrabber = null;
                }
                if (pSampleIn != null)
                {
                    Marshal.ReleaseComObject(pSampleIn);
                    pSampleIn = null;
                }
            }
        }
Exemplo n.º 3
0
        public void PrepareCapture(int i_width, int i_height,float i_frame_rate)
        {
            const int BBP = 32;
            //既にキャプチャ中なら諦める。
            if (this.m_graphi_active)
            {
                throw new Exception();
            }
            //現在確保中のグラフインスタンスを全て削除
            CleanupGraphiObjects();

            int hr;
            ISampleGrabber sampGrabber = null;
            IBaseFilter capFilter = null;
            IPin pSampleIn = null;

            //グラフビルダを作る。
            this.m_FilterGraph = new FilterGraph() as IFilterGraph2;

            try
            {
                //フィルタグラフにキャプチャを追加して、capFilterにピンを受け取る。
                hr = m_FilterGraph.AddSourceFilterForMoniker(this.m_dev.Mon, null, this.m_dev.Name, out capFilter);
                DsError.ThrowExceptionForHR(hr);

                //stillピンを探す
                m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0);
                //stillピンが無ければPreviewを探す。
                if (m_pinStill == null)
                {
                    m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0);
                }
                // Still haven't found one.  Need to put a splitter in so we have
                // one stream to capture the bitmap from, and one to display.  Ok, we
                // don't *have* to do it that way, but we are going to anyway.
                if (m_pinStill == null)
                {
                    // There is no still pin
                    m_VidControl = null;
                    m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);

                }else{
                    // Get a control pointer (used in Click())
                    m_VidControl = capFilter as IAMVideoControl;

                    m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);
                }

                if (i_height + i_width + BBP > 0)
                {
                    SetConfigParms(m_pinStill, i_width, i_height,i_frame_rate, BBP);
                }

                // Get the SampleGrabber interface
                sampGrabber = new SampleGrabber() as ISampleGrabber;

                //sampGrabberの設定
                IBaseFilter baseGrabFlt = sampGrabber as IBaseFilter;
                ConfigureSampleGrabber(sampGrabber);

                pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);

                // Add the sample grabber to the graph
                hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber");
                DsError.ThrowExceptionForHR(hr);

                if (m_VidControl == null)
                {
                    // Connect the Still pin to the sample grabber
                    hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
                    DsError.ThrowExceptionForHR(hr);

                }else{
                    // Connect the Still pin to the sample grabber
                    hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
                    DsError.ThrowExceptionForHR(hr);
                }
                hr = sampGrabber.GetConnectedMediaType(this._capture_mediatype);
                DsError.ThrowExceptionForHR(hr);
                //ビデオフォーマット等の更新
                upateVideoInfo(sampGrabber);
            }
            finally
            {
                if (sampGrabber != null)
                {
                    Marshal.ReleaseComObject(sampGrabber);
                    sampGrabber = null;
                }
                if (pSampleIn != null)
                {
                    Marshal.ReleaseComObject(pSampleIn);
                    pSampleIn = null;
                }
            }
        }