網頁

2013年6月17日 星期一

DIY - 智能小車:用 Microsoft Visual C++/CLI 編寫 Web Cam 程式碼 (四十)


DIY - 智能小車:用 Microsoft Visual C++/CLI 編寫 Web Cam 程式碼 (四十) 

AForge.NET 是計算機視覺和人工智能庫 Library),筆者使用 Microsoft Visual C++/CLI 編寫Web Cam 程式碼,直接呼叫 AForge 的動態連結程式庫(DLL),創建 MJPEG Stream ,打開 Video Source 便可以取得影像。

Web Cam 程式
03_CLI_AForge_ipcam C++/CLI Form1.c 程式:
// Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using AForge.Video;
using AForge.Video.DirectShow;

namespace _02_AFroge_ipcam
{
    public partial class Form1 : Form
    {
         private Stopwatch stopWatch = null;

        // Class constructor
        public Form1()
        {
            InitializeComponent();
        }
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            CloseCurrentVideoSource();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // create video source
            MJPEGStream mjpegSource = new MJPEGStream("http://192.168.0.108:8080/?action=stream");

            // open it
            OpenVideoSource(mjpegSource);
        }

        // Open video source
        private void OpenVideoSource( IVideoSource source )
        {
            // set busy cursor
            this.Cursor = Cursors.WaitCursor;

            // stop current video source
            CloseCurrentVideoSource( );

            // start new video source
            videoSourcePlayer.VideoSource = source;
            videoSourcePlayer.Start( );

            // reset stop watch
            stopWatch = null;

            // start timer
            timer.Start( );

            this.Cursor = Cursors.Default;
        }

        // Close video source if it is running
        private void CloseCurrentVideoSource( )
        {
            if ( videoSourcePlayer.VideoSource != null )
            {
                videoSourcePlayer.SignalToStop( );

                // wait ~ 3 seconds
                for ( int i = 0; i < 30; i++ )
                {
                    if ( !videoSourcePlayer.IsRunning )
                        break;
                    System.Threading.Thread.Sleep( 100 );
                }

                if ( videoSourcePlayer.IsRunning )
                {
                    videoSourcePlayer.Stop( );
                }

                videoSourcePlayer.VideoSource = null;
            }
        }

        // New frame received by the player
        private void videoSourcePlayer_NewFrame( object sender, ref Bitmap image )
        {
            DateTime now = DateTime.Now;
            Graphics g = Graphics.FromImage( image );

            // paint current time
            SolidBrush brush = new SolidBrush( Color.Red );
            g.DrawString( now.ToString( ), this.Font, brush, new PointF( 5, 5 ) );
            brush.Dispose( );

            g.Dispose( );
        }

        // On timer event - gather statistics
        private void timer_Tick( object sender, EventArgs e )
        {
            IVideoSource videoSource = videoSourcePlayer.VideoSource;

            if ( videoSource != null )
            {
                // get number of frames since the last timer tick
                int framesReceived = videoSource.FramesReceived;

                if ( stopWatch == null )
                {
                    stopWatch = new Stopwatch( );
                    stopWatch.Start( );
                }
                else
                {
                    stopWatch.Stop( );

                    float fps = 1000.0f * framesReceived / stopWatch.ElapsedMilliseconds;
                    //fpsLabel.Text = fps.ToString( "F2" ) + " fps";

                    stopWatch.Reset( );
                    stopWatch.Start( );
                }
            }
        }


    } // public partial class Form1 : Form
}

偵錯 開始偵錯(F5
顯示 Web Cam 的圖像!

2013617 天氣報告
氣溫:27.9 @ 22:10
相對濕度:百分之94%
天氣:微雨

沒有留言:

張貼留言