Beispiel #1
0
        /// <summary>
        /// カメラ制御とビデオ品質制御
        /// </summary>
        public static void Sample14()
        {
            string __FUNCTION__ = MethodBase.GetCurrentMethod().Name;

            Console.WriteLine(__FUNCTION__);

            IGraphBuilder         graph         = null;
            ICaptureGraphBuilder2 builder       = null;
            IBaseFilter           videoSource   = null;
            IBaseFilter           videoGrabber  = null;
            IBaseFilter           videoRenderer = null;
            var videoGrabberCB = new CxSampleGrabberCB();

            try
            {
                #region グラフビルダーの生成:
                {
                    // 必須:
                    graph = (IGraphBuilder)Axi.CoCreateInstance(GUID.CLSID_FilterGraph);
                    if (graph == null)
                    {
                        throw new System.IO.IOException("Failed to create a GraphBuilder.");
                    }

                    // 任意: 当例に於いては必須ではありませんが、後述のフィルタ接続を簡潔に記述できます.
                    builder = (ICaptureGraphBuilder2)Axi.CoCreateInstance(GUID.CLSID_CaptureGraphBuilder2);
                    if (builder == null)
                    {
                        throw new System.IO.IOException("Failed to create a GraphBuilder.");
                    }
                    builder.SetFiltergraph(graph);
                }
                #endregion

                #region  像入力用: ソースフィルタを生成します.
                {
                    videoSource = Axi.CreateFilter(GUID.CLSID_VideoInputDeviceCategory, null, 0);
                    if (videoSource == null)
                    {
                        throw new System.IO.IOException("Failed to create a videoSource.");
                    }
                    graph.AddFilter(videoSource, "VideoSource");

                    // フレームサイズを設定します.
                    // ※注) この操作は、ピンを接続する前に行う必要があります.
                    IPin pin = Axi.FindPin(videoSource, 0, PIN_DIRECTION.PINDIR_OUTPUT);
                    Axi.SetFormatSize(pin, 640, 480);
                }
                #endregion

                #region  像捕獲用: サンプルグラバーを生成します.
                {
                    videoGrabber = (IBaseFilter)Axi.CoCreateInstance(GUID.CLSID_SampleGrabber);
                    if (videoGrabber == null)
                    {
                        throw new System.IO.IOException("Failed to create a videoGrabber.");
                    }
                    graph.AddFilter(videoGrabber, "videoGrabber");

                    // サンプルグラバフィルタの入力形式設定.
                    // SetMediaType で必要なメディア タイプを指定します。
                    //   http://msdn.microsoft.com/ja-jp/library/cc369546.aspx
                    // ※AM_MEDIA_TYPE 構造体のメンバをすべて設定する必要はない。
                    // ※デフォルトでは、サンプル グラバに優先メディア タイプはない。
                    // ※サンプル グラバを正しいフィルタに確実に接続するには、フィルタ グラフを作成する前にこのメソッドを呼び出す。
                    // majortype: http://msdn.microsoft.com/ja-jp/library/cc370108.aspx
                    // subtype  : http://msdn.microsoft.com/ja-jp/library/cc371040.aspx
                    {
                        var grabber = (ISampleGrabber)videoGrabber;

                        var mt = new AM_MEDIA_TYPE();
                        mt.majortype  = new Guid(GUID.MEDIATYPE_Video);
                        mt.subtype    = new Guid(GUID.MEDIASUBTYPE_RGB24);
                        mt.formattype = new Guid(GUID.FORMAT_VideoInfo);
                        grabber.SetMediaType(mt);
                        grabber.SetBufferSamples(false);                                        // サンプルコピー 無効.
                        grabber.SetOneShot(false);                                              // One Shot 無効.
                        //grabber.SetCallback(videoGrabberCB, 0);	// 0:SampleCB メソッドを呼び出すよう指示する.
                        grabber.SetCallback(videoGrabberCB, 1);                                 // 1:BufferCB メソッドを呼び出すよう指示する.
                    }
                }
                #endregion

                #region  像出力用: レンダラーを生成します.
                {
                    videoRenderer = (IBaseFilter)Axi.CoCreateInstance(GUID.CLSID_NullRenderer);
                    if (videoRenderer == null)
                    {
                        throw new System.IO.IOException("Failed to create a videoRenderer.");
                    }
                    graph.AddFilter(videoRenderer, "videoRenderer");
                }
                #endregion

                #region フィルタの接続:
                if (builder != null)
                {
                    unsafe
                    {
                        var mediatype = new Guid(GUID.MEDIATYPE_Video);
                        var hr        = (HRESULT)builder.RenderStream(IntPtr.Zero, new IntPtr(&mediatype), videoSource, videoGrabber, videoRenderer);
                        if (hr < HRESULT.S_OK)
                        {
                            throw new CxDSException(hr);
                        }
                    }
                }
                else
                {
                    // ピンの取得.
                    IPin videoSourceOutput  = Axi.FindPin(videoSource, 0, PIN_DIRECTION.PINDIR_OUTPUT);
                    IPin videoGrabberInput  = Axi.FindPin(videoGrabber, 0, PIN_DIRECTION.PINDIR_INPUT);
                    IPin videoGrabberOutput = Axi.FindPin(videoGrabber, 0, PIN_DIRECTION.PINDIR_OUTPUT);
                    IPin videoRendererInput = Axi.FindPin(videoRenderer, 0, PIN_DIRECTION.PINDIR_INPUT);

                    // ピンの接続.
                    graph.Connect(videoSourceOutput, videoGrabberInput);
                    graph.Connect(videoGrabberOutput, videoRendererInput);
                }
                #endregion

                // ------------------------------

                #region カメラ制御:
                Console.WriteLine("");
                Console.WriteLine("CameraControlProperty");
                {
                    #region 取得:
                    var watch = new Stopwatch();
                    watch.Start();
                    var ranges  = new Dictionary <CameraControlProperty, TxPropertyRange>();
                    var control = Axi.GetInterface <IAMCameraControl>(graph);
                    foreach (CameraControlProperty prop in Enum.GetValues(typeof(CameraControlProperty)))
                    {
                        HRESULT hr;
                        var     range = TxPropertyRange.Reset();

                        try
                        {
                            // レンジの取得.
                            hr = (HRESULT)control.GetRange(
                                prop,
                                ref range.Min,
                                ref range.Max,
                                ref range.SteppingDelta,
                                ref range.DefaultValue,
                                ref range.CapsFlag
                                );
                            if (hr < HRESULT.S_OK)
                            {
                                continue;
                            }

                            // 現在値の取得.
                            hr = (HRESULT)control.Get(
                                prop,
                                ref range.Value,
                                ref range.Flag
                                );
                            if (hr < HRESULT.S_OK)
                            {
                                continue;
                            }

                            range.IsSupported = true;
                        }
                        catch (System.Exception)
                        {
                        }
                        finally
                        {
                            ranges[prop] = range;
                        }
                    }
                    Console.WriteLine("Completed. {0:F3} msec", watch.Elapsed.TotalMilliseconds);
                    #endregion

                    #region 視覚的な確認の為の出力:
                    Console.WriteLine("--- {0,-22}: {1,5}, {2,5}, {3,6}, {4,4}, {5,5} ~ {6,5}",
                                      "Name", "Value", "Def", "Flag", "Step", "Lower", "Upper");

                    foreach (var item in ranges)
                    {
                        var name  = item.Key.ToString();
                        var range = item.Value;
                        if (range.IsSupported)
                        {
                            Console.WriteLine("[O] {0,-22}: {1,5}, {2,5}, 0x{3:X4}, {4,4}, {5,5} ~ {6,5}",
                                              name,
                                              range.Value,
                                              range.DefaultValue,
                                              range.CapsFlag,
                                              range.SteppingDelta,
                                              range.Min,
                                              range.Max
                                              );
                        }
                        else
                        {
                            Console.WriteLine("[-] {0,-22}:", name);
                        }
                    }
                    #endregion
                }
                #endregion

                #region ビデオ品質制御:
                Console.WriteLine("");
                Console.WriteLine("VideoProcAmpProperty");
                {
                    #region 取得:
                    var watch = new Stopwatch();
                    watch.Start();
                    var ranges  = new Dictionary <VideoProcAmpProperty, TxPropertyRange>();
                    var control = Axi.GetInterface <IAMVideoProcAmp>(graph);
                    foreach (VideoProcAmpProperty prop in Enum.GetValues(typeof(VideoProcAmpProperty)))
                    {
                        HRESULT hr;
                        var     range = TxPropertyRange.Reset();

                        try
                        {
                            // レンジ.
                            hr = (HRESULT)control.GetRange(
                                prop,
                                ref range.Min,
                                ref range.Max,
                                ref range.SteppingDelta,
                                ref range.DefaultValue,
                                ref range.CapsFlag
                                );
                            if (hr >= HRESULT.S_OK)
                            {
                                // 現在値.
                                hr = (HRESULT)control.Get(
                                    prop,
                                    ref range.Value,
                                    ref range.Flag
                                    );
                                if (hr >= HRESULT.S_OK)
                                {
                                    range.IsSupported = true;
                                }
                            }
                        }
                        catch (System.Exception)
                        {
                        }

                        ranges[prop] = range;
                    }
                    Console.WriteLine("Completed. {0:F3} msec", watch.Elapsed.TotalMilliseconds);
                    #endregion

                    #region 視覚的な確認の為の出力:
                    Console.WriteLine("--- {0,-22}: {1,5}, {2,5}, {3,6}, {4,4}, {5,5} ~ {6,5}",
                                      "Name", "Value", "Def", "Flag", "Step", "Lower", "Upper");

                    foreach (var item in ranges)
                    {
                        var name  = item.Key.ToString();
                        var range = item.Value;
                        if (range.IsSupported)
                        {
                            Console.WriteLine("[O] {0,-22}: {1,5}, {2,5}, 0x{3:X4}, {4,4}, {5,5} ~ {6,5}",
                                              name,
                                              range.Value,
                                              range.DefaultValue,
                                              range.CapsFlag,
                                              range.SteppingDelta,
                                              range.Min,
                                              range.Max
                                              );
                        }
                        else
                        {
                            Console.WriteLine("[-] {0,-22}:", name);
                        }
                    }
                    #endregion
                }
                #endregion
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("{0}", ex.StackTrace);
            }
            finally
            {
                #region 解放:
                if (videoSource != null)
                {
                    Marshal.ReleaseComObject(videoSource);
                }
                videoSource = null;

                if (videoGrabber != null)
                {
                    Marshal.ReleaseComObject(videoGrabber);
                }
                videoGrabber = null;

                if (videoRenderer != null)
                {
                    Marshal.ReleaseComObject(videoRenderer);
                }
                videoRenderer = null;

                if (builder != null)
                {
                    Marshal.ReleaseComObject(builder);
                }
                builder = null;

                if (graph != null)
                {
                    Marshal.ReleaseComObject(graph);
                }
                graph = null;
                #endregion
            }
        }
Beispiel #2
0
        /// <summary>
        /// カメラから画像を取り込み、動画ファイル(wmv)へ保存する処理
        /// </summary>
        /// <remarks>
        ///		WMV 形式で保存する場合は映像と音声の両方が必要です。<br/>
        ///		<br/>
        ///		RenderStream (NULL, MEDIATYPE_Audio, audio, null, renderner)<br/>
        ///		RenderStream (NULL, MEDIATYPE_Video, video, grabber, renderner)<br/>
        ///		<pre>
        ///		 audio                       renderer
        ///		+-------+                   +-------+
        ///		|       0 ----------------- 0       |
        ///		+-------+                   |       |
        ///		 video         grabber      |       |
        ///		+-------+     +-------+     |       |
        ///		|       0 --- 0       0 --- 1       |
        ///		|       1     |       |     +-------+
        ///		+-------+     +-------+
        ///		</pre>
        ///		※ 本例では grabber は省略しています。<br/>
        /// </remarks>
        public static void Sample13()
        {
            string __FUNCTION__ = MethodBase.GetCurrentMethod().Name;

            Console.WriteLine(__FUNCTION__);

            IGraphBuilder         graph         = null;
            ICaptureGraphBuilder2 builder       = null;
            IBaseFilter           videoSource   = null;
            IBaseFilter           audioSource   = null;
            IBaseFilter           videoRenderer = null;
            IFileSinkFilter       fileSink      = null;
            string filename  = Path.Combine(Results, __FUNCTION__ + ".wmv");
            var    frameSize = new Size(640, 480);

            try
            {
                #region グラフビルダーの生成:
                {
                    graph = (IGraphBuilder)Axi.CoCreateInstance(GUID.CLSID_FilterGraph);
                    if (graph == null)
                    {
                        throw new System.IO.IOException("Failed to create a GraphBuilder.");
                    }

                    builder = (ICaptureGraphBuilder2)Axi.CoCreateInstance(GUID.CLSID_CaptureGraphBuilder2);
                    if (builder == null)
                    {
                        throw new System.IO.IOException("Failed to create a GraphBuilder.");
                    }
                    builder.SetFiltergraph(graph);
                }
                #endregion

                #region  像入力用: ソースフィルタを生成します.
                {
                    videoSource = Axi.CreateFilter(GUID.CLSID_VideoInputDeviceCategory, null, 0);
                    if (videoSource == null)
                    {
                        throw new System.IO.IOException("Failed to create a videoSource.");
                    }
                    graph.AddFilter(videoSource, "VideoSource");

                    // フレームサイズを設定します.
                    // ※注) この操作は、ピンを接続する前に行う必要があります.
                    IPin pin = Axi.FindPin(videoSource, 0, PIN_DIRECTION.PINDIR_OUTPUT);
                    Axi.SetFormatSize(pin, frameSize.Width, frameSize.Height);
                }
                #endregion

                #region 音声入力用: ソースフィルタを生成します.
                {
                    audioSource = Axi.CreateFilter(GUID.CLSID_AudioInputDeviceCategory, null, 0);
                    if (audioSource == null)
                    {
                        throw new System.IO.IOException("Failed to create a audioSource.");
                    }
                    graph.AddFilter(audioSource, "audioSource");
                }
                #endregion

                #region  像出力用: 保存する動画ファイル名を設定します.
                unsafe
                {
                    // 動画ファイルを保存する設定:
                    var     filetype = new Guid(GUID.MEDIASUBTYPE_Asf);
                    HRESULT hr       = (HRESULT)builder.SetOutputFileName(new IntPtr(&filetype), filename, ref videoRenderer, ref fileSink);
                    if (hr < HRESULT.S_OK)
                    {
                        throw new CxDSException(hr);
                    }

                    /*
                     * Capturing Video to a Windows Media File
                     * https://msdn.microsoft.com/en-us/library/windows/desktop/dd318630(v=vs.85).aspx
                     */
                    var config = (IConfigAsfWriter)videoRenderer;

                    // WMProfile
                    {
                        // WMProfile_V80_256Video が取得されます.
                        Guid currentProfileGuid;
                        hr = (HRESULT)config.GetCurrentProfileGuid(out currentProfileGuid);
                        if (hr < HRESULT.S_OK)
                        {
                            throw new CxDSException(hr);
                        }

                        // WMProfile_V80_BESTVBRVideo に書き替えます.
                        Guid newProfileGuid = new Guid(GUID.WMProfile_V80_BESTVBRVideo);
                        hr = (HRESULT)config.ConfigureFilterUsingProfileGuid(newProfileGuid);
                        if (hr < HRESULT.S_OK)
                        {
                            throw new CxDSException(hr);
                        }
                    }
                }
                #endregion

                #region フィルタの接続:
                unsafe
                {
                    HRESULT hr;

                    // フィルタの接続: (映像入力)
                    var mediatype_video = new Guid(GUID.MEDIATYPE_Video);
                    hr = (HRESULT)builder.RenderStream(IntPtr.Zero, new IntPtr(&mediatype_video), videoSource, null, videoRenderer);
                    if (hr < HRESULT.S_OK)
                    {
                        throw new CxDSException(hr);
                    }

                    // フィルタの接続: (音声入力)
                    var mediatype_audio = new Guid(GUID.MEDIATYPE_Audio);
                    hr = (HRESULT)builder.RenderStream(IntPtr.Zero, new IntPtr(&mediatype_audio), audioSource, null, videoRenderer);
                    if (hr < HRESULT.S_OK)
                    {
                        throw new CxDSException(hr);
                    }
                }
                #endregion

                #region  像出力用: 保存する動画ファイルのフレームサイズを設定します.
                unsafe
                {
                    /*
                     * Capturing Video to a Windows Media File
                     * https://msdn.microsoft.com/en-us/library/windows/desktop/dd318630(v=vs.85).aspx
                     */
                    var config = (IConfigAsfWriter)videoRenderer;

                    // フレームサイズの変更:
                    Axi.SetVideoFrameSize(config, frameSize);
                }
                #endregion

                // ------------------------------

                #region 取り込み処理:
                {
                    var mediaControl = (IMediaControl)graph;
                    var mediaEvent   = (IMediaEvent)graph;

                    var watch = new Stopwatch();
                    watch.Start();

                    // 露光開始.
                    Console.WriteLine("Run ...");
                    {
                        HRESULT hr;
                        int     state;
                        hr = (HRESULT)mediaControl.Run();
                        hr = (HRESULT)mediaControl.GetState(1000, out state);
                    }
                    Console.WriteLine("Running ... {0:F3} msec", watch.Elapsed.TotalMilliseconds);

                    // 取り込み待機.
                    while (watch.ElapsedMilliseconds < 5000)
                    {
                        System.Threading.Thread.Sleep(1);
                    }

                    // 露光停止.
                    Console.WriteLine("Stop ... {0:F3} msec", watch.Elapsed.TotalMilliseconds);
                    {
                        HRESULT hr;
                        int     state;
                        hr = (HRESULT)mediaControl.Stop();
                        hr = (HRESULT)mediaControl.GetState(1000, out state);
                    }

                    Console.WriteLine("Completed. {0:F3} msec", watch.Elapsed.TotalMilliseconds);
                }
                #endregion
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("{0}", ex.StackTrace);
            }
            finally
            {
                #region 解放:
                if (videoSource != null)
                {
                    Marshal.ReleaseComObject(videoSource);
                }
                videoSource = null;

                if (videoRenderer != null)
                {
                    Marshal.ReleaseComObject(videoRenderer);
                }
                videoRenderer = null;

                if (fileSink != null)
                {
                    Marshal.ReleaseComObject(fileSink);
                }
                fileSink = null;

                if (builder != null)
                {
                    Marshal.ReleaseComObject(builder);
                }
                builder = null;

                if (graph != null)
                {
                    Marshal.ReleaseComObject(graph);
                }
                graph = null;
                #endregion
            }
        }
Beispiel #3
0
        /// <summary>
        /// カメラの接続
        /// </summary>
        /// <param name="filterInfo"></param>
        /// <param name="pinno"></param>
        /// <param name="frameSize"></param>
        private void Camera_Connect(CxFilterInfo filterInfo, int pinno, Size frameSize)
        {
            #region グラフビルダーの生成:
            {
                Graph = (IGraphBuilder)Axi.CoCreateInstance(GUID.CLSID_FilterGraph);
                if (Graph == null)
                {
                    throw new System.IO.IOException("Failed to create a GraphBuilder.");
                }

                Builder = (ICaptureGraphBuilder2)Axi.CoCreateInstance(GUID.CLSID_CaptureGraphBuilder2);
                if (Builder == null)
                {
                    throw new System.IO.IOException("Failed to create a GraphBuilder.");
                }
                Builder.SetFiltergraph(Graph);
            }
            #endregion

            #region  像入力用: ソースフィルタを生成します.
            {
                VideoSource = Axi.CreateFilter(GUID.CLSID_VideoInputDeviceCategory, filterInfo.CLSID, filterInfo.Index);
                if (VideoSource == null)
                {
                    throw new System.IO.IOException("Failed to create a VideoSource.");
                }
                Graph.AddFilter(VideoSource, "VideoSource");

                // フレームサイズを設定します.
                // ※注) この操作は、ピンを接続する前に行う必要があります.
                IPin pin = Axi.FindPin(VideoSource, pinno, PIN_DIRECTION.PINDIR_OUTPUT);
                Axi.SetFormatSize(pin, frameSize.Width, frameSize.Height);
            }
            #endregion

            #region  像捕獲用: サンプルグラバーを生成します.
            {
                VideoGrabber = (IBaseFilter)Axi.CoCreateInstance(GUID.CLSID_SampleGrabber);
                if (VideoGrabber == null)
                {
                    throw new System.IO.IOException("Failed to create a VideoGrabber.");
                }
                Graph.AddFilter(VideoGrabber, "VideoGrabber");

                // サンプルグラバフィルタの入力形式設定.
                // SetMediaType で必要なメディア タイプを指定します。
                //   http://msdn.microsoft.com/ja-jp/library/cc369546.aspx
                // ※AM_MEDIA_TYPE 構造体のメンバをすべて設定する必要はない。
                // ※デフォルトでは、サンプル グラバに優先メディア タイプはない。
                // ※サンプル グラバを正しいフィルタに確実に接続するには、フィルタ グラフを作成する前にこのメソッドを呼び出す。
                // majortype: http://msdn.microsoft.com/ja-jp/library/cc370108.aspx
                // subtype  : http://msdn.microsoft.com/ja-jp/library/cc371040.aspx
                {
                    var grabber = (ISampleGrabber)VideoGrabber;

                    var mt = new AM_MEDIA_TYPE();
                    mt.majortype  = new Guid(GUID.MEDIATYPE_Video);
                    mt.subtype    = new Guid(GUID.MEDIASUBTYPE_RGB24);
                    mt.formattype = new Guid(GUID.FORMAT_VideoInfo);
                    grabber.SetMediaType(mt);
                    grabber.SetBufferSamples(false);                                    // サンプルコピー 無効.
                    grabber.SetOneShot(false);                                          // One Shot 無効.
                    //grabber.SetCallback(VideoGrabberCB, 0);	// 0:SampleCB メソッドを呼び出すよう指示する.
                    grabber.SetCallback(VideoGrabberCB, 1);                             // 1:BufferCB メソッドを呼び出すよう指示する.
                }
            }
            #endregion

            #region  像出力用: レンダラーを生成します.
            {
                VideoRenderer = (IBaseFilter)Axi.CoCreateInstance(GUID.CLSID_NullRenderer);
                if (VideoRenderer == null)
                {
                    throw new System.IO.IOException("Failed to create a VideoRenderer.");
                }
                Graph.AddFilter(VideoRenderer, "VideoRenderer");
            }
            #endregion

            #region フィルタの接続:
            unsafe
            {
                var mediatype = new Guid(GUID.MEDIATYPE_Video);
                var hr        = (HRESULT)Builder.RenderStream(IntPtr.Zero, new IntPtr(&mediatype), VideoSource, VideoGrabber, VideoRenderer);
                if (hr < HRESULT.S_OK)
                {
                    throw new CxDSException(hr);
                }
            }
            #endregion

            // 同期用: サンプルグラバーのイベント登録:
            VideoGrabberCB.Enable  = true;
            VideoGrabberCB.Notify += VideoGrabberCB_Notify;
            VideoInfoHeader        = Axi.GetVideoInfo((ISampleGrabber)VideoGrabber);

            // カメラ制御インターフェースの抽出.
            CameraControl = Axi.GetInterface <IAMCameraControl>(this.Graph);
        }
Beispiel #4
0
        /// <summary>
        /// カメラから画像を取り込み、動画ファイル(avi)へ保存する処理
        /// </summary>
        /// <remarks>
        ///		AVI 形式で保存する場合は音声入力は省略可能です。<br/>
        ///		<br/>
        ///		RenderStream (NULL, MEDIATYPE_Video, source, grabber, renderner)<br/>
        ///		<pre>
        ///		 source        grabber       renderer
        ///		+-------+     +-------+     +-------+
        ///		|       0 --- 0       0 --- 0       |
        ///		|       1     |       |     |       |
        ///		+-------+     +-------+     +-------+
        ///		</pre>
        ///		※ 本例では grabber は省略しています。<br/>
        /// </remarks>
        public static void Sample12()
        {
            string __FUNCTION__ = MethodBase.GetCurrentMethod().Name;

            Console.WriteLine(__FUNCTION__);

            IGraphBuilder         graph         = null;
            ICaptureGraphBuilder2 builder       = null;
            IBaseFilter           videoSource   = null;
            IBaseFilter           videoRenderer = null;
            IFileSinkFilter       fileSink      = null;
            string filename = Path.Combine(Results, __FUNCTION__ + ".avi");

            try
            {
                #region グラフビルダーの生成:
                {
                    graph = (IGraphBuilder)Axi.CoCreateInstance(GUID.CLSID_FilterGraph);
                    if (graph == null)
                    {
                        throw new System.IO.IOException("Failed to create a GraphBuilder.");
                    }

                    builder = (ICaptureGraphBuilder2)Axi.CoCreateInstance(GUID.CLSID_CaptureGraphBuilder2);
                    if (builder == null)
                    {
                        throw new System.IO.IOException("Failed to create a GraphBuilder.");
                    }
                    builder.SetFiltergraph(graph);
                }
                #endregion

                #region  像入力用: ソースフィルタを生成します.
                {
                    videoSource = Axi.CreateFilter(GUID.CLSID_VideoInputDeviceCategory, null, 0);
                    if (videoSource == null)
                    {
                        throw new System.IO.IOException("Failed to create a videoSource.");
                    }
                    graph.AddFilter(videoSource, "VideoSource");

                    // フレームサイズを設定します.
                    // ※注) この操作は、ピンを接続する前に行う必要があります.
                    IPin pin = Axi.FindPin(videoSource, 0, PIN_DIRECTION.PINDIR_OUTPUT);
                    Axi.SetFormatSize(pin, 640, 480);
                }
                #endregion

                #region  像出力用: 保存する動画ファイル名を設定します.
                unsafe
                {
                    // 動画ファイルを保存する設定:
                    var     filetype = new Guid(GUID.MEDIASUBTYPE_Avi);
                    HRESULT hr       = (HRESULT)builder.SetOutputFileName(new IntPtr(&filetype), filename, ref videoRenderer, ref fileSink);
                    if (hr < HRESULT.S_OK)
                    {
                        throw new CxDSException(hr);
                    }
                }
                #endregion

                #region フィルタの接続:
                unsafe
                {
                    // フィルタの接続:
                    var     mediatype = new Guid(GUID.MEDIATYPE_Video);
                    HRESULT hr        = (HRESULT)builder.RenderStream(IntPtr.Zero, new IntPtr(&mediatype), videoSource, null, videoRenderer);
                    if (hr < HRESULT.S_OK)
                    {
                        throw new CxDSException(hr);
                    }
                }
                #endregion

                // ------------------------------

                #region 取り込み処理:
                {
                    var mediaControl = (IMediaControl)graph;
                    var mediaEvent   = (IMediaEvent)graph;

                    var watch = new Stopwatch();
                    watch.Start();

                    // 露光開始.
                    Console.WriteLine("Run ...");
                    {
                        HRESULT hr;
                        int     state;
                        hr = (HRESULT)mediaControl.Run();
                        hr = (HRESULT)mediaControl.GetState(1000, out state);
                    }
                    Console.WriteLine("Running ... {0:F3} msec", watch.Elapsed.TotalMilliseconds);

                    // 取り込み待機.
                    while (watch.ElapsedMilliseconds < 3000)
                    {
                        System.Threading.Thread.Sleep(1);
                    }

                    // 露光停止.
                    Console.WriteLine("Stop ... {0:F3} msec", watch.Elapsed.TotalMilliseconds);
                    {
                        HRESULT hr;
                        int     state;
                        hr = (HRESULT)mediaControl.Stop();
                        hr = (HRESULT)mediaControl.GetState(1000, out state);
                    }

                    Console.WriteLine("Completed. {0:F3} msec", watch.Elapsed.TotalMilliseconds);
                }
                #endregion
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("{0}", ex.StackTrace);
            }
            finally
            {
                #region 解放:
                if (videoSource != null)
                {
                    Marshal.ReleaseComObject(videoSource);
                }
                videoSource = null;

                if (videoRenderer != null)
                {
                    Marshal.ReleaseComObject(videoRenderer);
                }
                videoRenderer = null;

                if (fileSink != null)
                {
                    Marshal.ReleaseComObject(fileSink);
                }
                fileSink = null;

                if (builder != null)
                {
                    Marshal.ReleaseComObject(builder);
                }
                builder = null;

                if (graph != null)
                {
                    Marshal.ReleaseComObject(graph);
                }
                graph = null;
                #endregion
            }
        }
Beispiel #5
0
        /// <summary>
        /// カメラから画像を取り込む処理
        /// </summary>
        /// <remarks>
        ///		この方法が最も単純です。<br/>
        ///		<br/>
        ///		RenderStream (NULL, MEDIATYPE_Video, source, grabber, renderner)<br/>
        ///		<pre>
        ///		 source        grabber       renderer
        ///		+-------+     +-------+     +-------+
        ///		|       0 --- 0       0 --- 0       |
        ///		|       1     |       |     |       |
        ///		+-------+     +-------+     +-------+
        ///		</pre>
        /// </remarks>
        public static void Sample11()
        {
            string __FUNCTION__ = MethodBase.GetCurrentMethod().Name;

            Console.WriteLine(__FUNCTION__);

            IGraphBuilder         graph         = null;
            ICaptureGraphBuilder2 builder       = null;
            IBaseFilter           videoSource   = null;
            IBaseFilter           videoGrabber  = null;
            IBaseFilter           videoRenderer = null;
            var videoGrabberCB = new CxSampleGrabberCB();

            try
            {
                #region グラフビルダーの生成:
                {
                    // 必須:
                    graph = (IGraphBuilder)Axi.CoCreateInstance(GUID.CLSID_FilterGraph);
                    if (graph == null)
                    {
                        throw new System.IO.IOException("Failed to create a GraphBuilder.");
                    }

                    // 任意: 当例に於いては必須ではありませんが、後述のフィルタ接続を簡潔に記述できます.
                    builder = (ICaptureGraphBuilder2)Axi.CoCreateInstance(GUID.CLSID_CaptureGraphBuilder2);
                    if (builder == null)
                    {
                        throw new System.IO.IOException("Failed to create a GraphBuilder.");
                    }
                    builder.SetFiltergraph(graph);
                }
                #endregion

                #region  像入力用: ソースフィルタを生成します.
                {
                    videoSource = Axi.CreateFilter(GUID.CLSID_VideoInputDeviceCategory, null, 0);
                    if (videoSource == null)
                    {
                        throw new System.IO.IOException("Failed to create a videoSource.");
                    }
                    graph.AddFilter(videoSource, "VideoSource");

                    // フレームサイズを設定します.
                    // ※注) この操作は、ピンを接続する前に行う必要があります.
                    IPin pin = Axi.FindPin(videoSource, 0, PIN_DIRECTION.PINDIR_OUTPUT);
                    Axi.SetFormatSize(pin, 640, 480);
                }
                #endregion

                #region  像捕獲用: サンプルグラバーを生成します.
                {
                    videoGrabber = (IBaseFilter)Axi.CoCreateInstance(GUID.CLSID_SampleGrabber);
                    if (videoGrabber == null)
                    {
                        throw new System.IO.IOException("Failed to create a videoGrabber.");
                    }
                    graph.AddFilter(videoGrabber, "videoGrabber");

                    // サンプルグラバフィルタの入力形式設定.
                    // SetMediaType で必要なメディア タイプを指定します。
                    //   http://msdn.microsoft.com/ja-jp/library/cc369546.aspx
                    // ※AM_MEDIA_TYPE 構造体のメンバをすべて設定する必要はない。
                    // ※デフォルトでは、サンプル グラバに優先メディア タイプはない。
                    // ※サンプル グラバを正しいフィルタに確実に接続するには、フィルタ グラフを作成する前にこのメソッドを呼び出す。
                    // majortype: http://msdn.microsoft.com/ja-jp/library/cc370108.aspx
                    // subtype  : http://msdn.microsoft.com/ja-jp/library/cc371040.aspx
                    {
                        var grabber = (ISampleGrabber)videoGrabber;

                        var mt = new AM_MEDIA_TYPE();
                        mt.majortype  = new Guid(GUID.MEDIATYPE_Video);
                        mt.subtype    = new Guid(GUID.MEDIASUBTYPE_RGB24);
                        mt.formattype = new Guid(GUID.FORMAT_VideoInfo);
                        grabber.SetMediaType(mt);
                        grabber.SetBufferSamples(false);                                        // サンプルコピー 無効.
                        grabber.SetOneShot(false);                                              // One Shot 無効.
                        //grabber.SetCallback(videoGrabberCB, 0);	// 0:SampleCB メソッドを呼び出すよう指示する.
                        grabber.SetCallback(videoGrabberCB, 1);                                 // 1:BufferCB メソッドを呼び出すよう指示する.
                    }
                }
                #endregion

                #region  像出力用: レンダラーを生成します.
                {
                    videoRenderer = (IBaseFilter)Axi.CoCreateInstance(GUID.CLSID_NullRenderer);
                    if (videoRenderer == null)
                    {
                        throw new System.IO.IOException("Failed to create a videoRenderer.");
                    }
                    graph.AddFilter(videoRenderer, "videoRenderer");
                }
                #endregion

                #region フィルタの接続:
                if (builder != null)
                {
                    unsafe
                    {
                        var mediatype = new Guid(GUID.MEDIATYPE_Video);
                        var hr        = (HRESULT)builder.RenderStream(IntPtr.Zero, new IntPtr(&mediatype), videoSource, videoGrabber, videoRenderer);
                        if (hr < HRESULT.S_OK)
                        {
                            throw new CxDSException(hr);
                        }
                    }
                }
                else
                {
                    // ピンの取得.
                    IPin videoSourceOutput  = Axi.FindPin(videoSource, 0, PIN_DIRECTION.PINDIR_OUTPUT);
                    IPin videoGrabberInput  = Axi.FindPin(videoGrabber, 0, PIN_DIRECTION.PINDIR_INPUT);
                    IPin videoGrabberOutput = Axi.FindPin(videoGrabber, 0, PIN_DIRECTION.PINDIR_OUTPUT);
                    IPin videoRendererInput = Axi.FindPin(videoRenderer, 0, PIN_DIRECTION.PINDIR_INPUT);

                    // ピンの接続.
                    graph.Connect(videoSourceOutput, videoGrabberInput);
                    graph.Connect(videoGrabberOutput, videoRendererInput);
                }
                #endregion

                // ------------------------------

                #region 取り込み処理:
                {
                    var mediaControl = (IMediaControl)graph;

                    // 映像サイズの取得.
                    var vih    = Axi.GetVideoInfo((ISampleGrabber)videoGrabber);
                    var images = new List <Bitmap>();

                    var watch = new Stopwatch();
                    watch.Start();

                    // 取り込み処理.
                    videoGrabberCB.Notify += delegate(object _sender, CxSampleGrabberEventArgs _e)
                    {
                        Console.WriteLine("{0}: SampleTime={1:F6}", images.Count, _e.SampleTime);
                        images.Add(_e.ToImage(vih));
                    };

                    // 露光開始.
                    Console.WriteLine("Run ...");
                    {
                        HRESULT hr;
                        int     state;
                        hr = (HRESULT)mediaControl.Run();
                        hr = (HRESULT)mediaControl.GetState(1000, out state);
                    }
                    Console.WriteLine("Running ... {0:F3} msec", watch.Elapsed.TotalMilliseconds);

                    // 取り込み待機.
                    while (watch.ElapsedMilliseconds < 3000)
                    {
                        System.Threading.Thread.Sleep(1);
                    }

                    // 露光停止.
                    Console.WriteLine("Stop ... {0:F3} msec", watch.Elapsed.TotalMilliseconds);
                    {
                        HRESULT hr;
                        int     state;
                        hr = (HRESULT)mediaControl.Stop();
                        hr = (HRESULT)mediaControl.GetState(1000, out state);
                    }

                    // 確認用:
                    Console.WriteLine("Save ... {0:F3} msec", watch.Elapsed.TotalMilliseconds);
                    {
                        string subdir = Path.Combine(Results, __FUNCTION__);
                        if (Directory.Exists(subdir) == false)
                        {
                            Directory.CreateDirectory(subdir);
                        }

                        for (int i = 0; i < images.Count; i++)
                        {
                            var filename = string.Format("image{0}.png", i);
                            images[i].Save(Path.Combine(subdir, filename));
                        }
                    }

                    Console.WriteLine("Completed. {0:F3} msec", watch.Elapsed.TotalMilliseconds);
                }
                #endregion
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("{0}", ex.StackTrace);
            }
            finally
            {
                #region 解放:
                if (videoSource != null)
                {
                    Marshal.ReleaseComObject(videoSource);
                }
                videoSource = null;

                if (videoGrabber != null)
                {
                    Marshal.ReleaseComObject(videoGrabber);
                }
                videoGrabber = null;

                if (videoRenderer != null)
                {
                    Marshal.ReleaseComObject(videoRenderer);
                }
                videoRenderer = null;

                if (builder != null)
                {
                    Marshal.ReleaseComObject(builder);
                }
                builder = null;

                if (graph != null)
                {
                    Marshal.ReleaseComObject(graph);
                }
                graph = null;
                #endregion
            }
        }
Beispiel #6
0
        /// <summary>
        /// 接続されているデバイスを列挙する処理
        /// </summary>
        /// <remarks>
        ///		<pre>
        ///		           in    filter   out
        ///		+--------+     +--------+     +--------+
        ///		|        | --- 0        0 --- |        |
        ///		+--------+     |        |     +--------+
        ///		               |        |     +--------+
        ///		               |        1 --- |        |
        ///		               +--------+     +--------+
        ///		</pre>
        /// </remarks>
        public static void Sample01()
        {
            string __FUNCTION__ = MethodBase.GetCurrentMethod().Name;

            Console.WriteLine(__FUNCTION__);

            try
            {
                #region  像入力デバイスの一覧: (カメラ等)
                {
                    var category    = new Guid(GUID.CLSID_VideoInputDeviceCategory);
                    var filterInfos = Axi.GetFilterList(category);

                    Console.WriteLine("Video Input Devices ({0})", filterInfos.Count);
                    for (int iii = 0; iii < filterInfos.Count; iii++)
                    {
                        // フィルタ情報:
                        var filterInfo = filterInfos[iii];
                        Console.WriteLine("|- {0}", iii);
                        Console.WriteLine("|  |- {0}", filterInfo.Name);
                        Console.WriteLine("|  |- {0} ({1})", filterInfo.CLSID, filterInfo.Index);

                        IBaseFilter filter = null;
                        try
                        {
                            filter = Axi.CreateFilter(category, filterInfo.CLSID, filterInfo.Index);

                            #region ピン情報:
                            var pinInfos   = Axi.GetPinList(filter);
                            int outpin_num = 0;
                            Console.WriteLine("|  |- {0} ({1})", "Pins", pinInfos.Count);
                            for (int ppp = 0; ppp < pinInfos.Count; ppp++)
                            {
                                var pinInfo = pinInfos[ppp];
                                Console.WriteLine("|  |  |- {0}: {1,-15} = {2}", ppp, pinInfo.Direction, pinInfo.Name);

                                if (pinInfo.Direction == PIN_DIRECTION.PINDIR_OUTPUT)
                                {
                                    outpin_num++;
                                }
                            }
                            #endregion

                            #region フォーマット情報: (※出力ピンに限定しています)
                            Console.WriteLine("|  |  |- {0}", "Outpin Details");
                            for (int outpin_index = 0; outpin_index < outpin_num; outpin_index++)
                            {
                                IPin pin = null;
                                try
                                {
                                    // 出力ピンからフォーマット情報を抽出します
                                    pin = Axi.FindPin(filter, outpin_index, PIN_DIRECTION.PINDIR_OUTPUT);
                                    var formatInfos = Axi.GetFormatList(pin);

                                    // 映像の情報のみ抽出します.
                                    var videoInfos = formatInfos.FindAll(
                                        delegate(CxFormatInfo item)
                                    {
                                        return(GUID.Compare(item.FormatType, GUID.FORMAT_VideoInfo));
                                    });

                                    Console.WriteLine("|  |  |  |- {0}[{1}]: {2} {3}", "Outpin", outpin_index, videoInfos.Count, "formats");
                                    for (int fff = 0; fff < videoInfos.Count; fff++)
                                    {
                                        var videoInfo = videoInfos[fff];
                                        Console.WriteLine("|  |  |  |  |- {0,2}: {1,4},{2,4}", fff, videoInfo.VideoSize.Width, videoInfo.VideoSize.Height);
                                    }
                                }
                                catch (System.Exception ex)
                                {
                                    Console.WriteLine("{0}", ex.StackTrace);
                                }
                                finally
                                {
                                    if (pin != null)
                                    {
                                        Marshal.ReleaseComObject(pin);
                                    }
                                    pin = null;
                                }
                            }
                            #endregion
                        }
                        catch (System.Exception ex)
                        {
                            Console.WriteLine("{0}", ex.StackTrace);
                        }
                        finally
                        {
                            if (filter != null)
                            {
                                Marshal.ReleaseComObject(filter);
                            }
                            filter = null;
                        }
                    }
                }
                #endregion

                #region 音声入力デバイスの一覧: (マイク等)
                {
                    var category    = new Guid(GUID.CLSID_AudioInputDeviceCategory);
                    var filterInfos = Axi.GetFilterList(category);

                    Console.WriteLine("Audio Input Devices ({0})", filterInfos.Count);
                    for (int iii = 0; iii < filterInfos.Count; iii++)
                    {
                        // フィルタ情報:
                        var filterInfo = filterInfos[iii];
                        Console.WriteLine("|- {0}", iii);
                        Console.WriteLine("|  |- {0}", filterInfo.Name);
                        Console.WriteLine("|  |- {0} ({1})", filterInfo.CLSID, filterInfo.Index);

                        IBaseFilter filter = null;
                        try
                        {
                            filter = Axi.CreateFilter(category, filterInfo.CLSID, filterInfo.Index);

                            #region ピン情報:
                            var pinInfos = Axi.GetPinList(filter);
                            Console.WriteLine("|  |- {0} ({1})", "Pins", pinInfos.Count);
                            for (int ppp = 0; ppp < pinInfos.Count; ppp++)
                            {
                                var pinInfo = pinInfos[ppp];
                                Console.WriteLine("|  |  |- {0}: {1,-15} = {2}", ppp, pinInfo.Direction, pinInfo.Name);
                            }
                            #endregion
                        }
                        catch (System.Exception ex)
                        {
                            Console.WriteLine("{0}", ex.StackTrace);
                        }
                        finally
                        {
                            if (filter != null)
                            {
                                Marshal.ReleaseComObject(filter);
                            }
                            filter = null;
                        }
                    }
                }
                #endregion

                #region 音声出力デバイスの一覧: (スピーカー等)
                {
                    var category    = new Guid(GUID.CLSID_AudioRendererCategory);
                    var filterInfos = Axi.GetFilterList(category);

                    Console.WriteLine("Audio Output Devices ({0})", filterInfos.Count);
                    for (int iii = 0; iii < filterInfos.Count; iii++)
                    {
                        // フィルタ情報:
                        var filterInfo = filterInfos[iii];
                        Console.WriteLine("|- {0}", iii);
                        Console.WriteLine("|  |- {0}", filterInfo.Name);
                        Console.WriteLine("|  |- {0} ({1})", filterInfo.CLSID, filterInfo.Index);

                        IBaseFilter filter = null;
                        try
                        {
                            filter = Axi.CreateFilter(category, filterInfo.CLSID, filterInfo.Index);

                            #region ピン情報:
                            var pinInfos = Axi.GetPinList(filter);
                            Console.WriteLine("|  |- {0} ({1})", "Pins", pinInfos.Count);
                            for (int ppp = 0; ppp < pinInfos.Count; ppp++)
                            {
                                var pinInfo = pinInfos[ppp];
                                Console.WriteLine("|  |  |- {0}: {1,-15} = {2}", ppp, pinInfo.Direction, pinInfo.Name);
                            }
                            #endregion
                        }
                        catch (System.Exception ex)
                        {
                            Console.WriteLine("{0}", ex.StackTrace);
                        }
                        finally
                        {
                            if (filter != null)
                            {
                                Marshal.ReleaseComObject(filter);
                            }
                            filter = null;
                        }
                    }
                }
                #endregion
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("{0}", ex.StackTrace);
            }
            finally
            {
            }
        }