Emgucv_C#_TimerThread_another_different_code_Style

The  effect of the program




The IDE of this exercise is in Visual Studio 2015

use the emgucv3.1  with it.

===============================================================


In  this time ,

I  want to introduce another code style of emgucv learned from my senior brother Hsu.

and thanks a lot for his teaching.

In the beginning of program ,

When we want to write a program to open camera.

Common and simple code style of Emgucv in C# is  like  below :


you can let your webcam works!!!

however , it is not a good code style.

We can try to write a Class combine with timer


The  more best code style


Main block

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.Structure;
using IOControl;

namespace emgucv3._1_ex1_timer
{
    public partial class Form1 : Form
    {
        private Capture webcam;

        //private Image<Bgr, Byte> frame; // old version emgucv variable type
        Mat frame = new Mat();

        private readonly int cameraIndex = 0;//Implementation phase constant ,the range of readonly is bigger than const
        /* const int cameraIndex = 0; */

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {            
            
        }

        //timer thread for webcam capture
        private void timerThread(Object sender, EventArgs e)
        {
            frame = webcam.QueryFrame();
            if (pImage.Image != null)
            {
                pImage.Image.Dispose();
                pImage.Image = null;
            }
            pImage.Image = frame.ToImage<Bgr,Byte>().ToBitmap();
            //because the frame in emgucv3.1 provide the Mat 
            //however , Image has to show on pictureBox so you need to convert to Image<Bgr,Byte>
            //Only old emgucv version Image<Bgr,Byte> variable type can use .ToBitMap() 
        }

        private void timerAddWebcam()
        {
            //throw new NotImplementedException();
            Timer webcamTimer = new Timer();
            webcamTimer.Interval = Convert.ToInt32(30);
            webcamTimer.Tick += new EventHandler(timerThread);
            webcamTimer.Start();
        }

        private void btnCamera_Click(object sender, EventArgs e)
        {
            webcam_Control.linkCamera(ref frame, ref webcam, cameraIndex);// to check the camera can be opened
            timerAddWebcam();//you can call the timerThread method when call the timerAddWebcam()
        }
    }
}

webcam_Control  Class  Block

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.Structure;
using IOControl;

namespace emgucv3._1_ex1_timer
{
    public partial class Form1 : Form
    {
        private Capture webcam;

        //private Image<Bgr, Byte> frame; // old version emgucv variable type
        Mat frame = new Mat();

        private readonly int cameraIndex = 0;//Implementation phase constant ,the range of readonly is bigger than const
        /* const int cameraIndex = 0; */

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {            
            
        }

        //timer thread for webcam capture
        private void timerThread(Object sender, EventArgs e)
        {
            frame = webcam.QueryFrame();
            if (pImage.Image != null)
            {
                pImage.Image.Dispose();
                pImage.Image = null;
            }
            pImage.Image = frame.ToImage<Bgr,Byte>().ToBitmap();
            //because the frame in emgucv3.1 provide the Mat 
            //however , Image has to show on pictureBox so you need to convert to Image<Bgr,Byte>
            //Only old emgucv version Image<Bgr,Byte> variable type can use .ToBitMap() 
        }

        private void timerAddWebcam()
        {
            //throw new NotImplementedException();
            Timer webcamTimer = new Timer();
            webcamTimer.Interval = Convert.ToInt32(30);
            webcamTimer.Tick += new EventHandler(timerThread);
            webcamTimer.Start();
        }

        private void btnCamera_Click(object sender, EventArgs e)
        {
            webcam_Control.linkCamera(ref frame, ref webcam, cameraIndex);// to check the camera can be opened
            timerAddWebcam();//you can call the timerThread method when call the timerAddWebcam()
        }
    }
}










We  can construct a namespace by ourself !!!!.

the namespace of  webcam_Control  class   we can rename it .


Just remember  if  we want to use the method of class webcam_Control

we need to using it !!!   Don't forget~~









The Form design:





留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題