2013-12-05 3 views
1

Я использую класс WebCam из библиотеки MetriCam, и мне нужно периодически получать кадры с моего веб-камеры, кто-нибудь знает, как это сделать? Вот код у меня уже есть:Получение фреймов из WebCam

namespace MetriCam_Coding_Example 
{ 
    public partial class Form1 : Form 
    { 
     #region Private Fields 
     private WebCam camera; 
     #endregion 

     #region Constructor 
     public Form1() 
     { 
      InitializeComponent(); 
      camera = new WebCam(); 
     } 
     #endregion 

     #region Private Methods 
     private void buttonConnect_Click(object sender, EventArgs e) 
     { 
      if (!camera.IsConnected()) 
      { 
       camera.Connect(); 
       buttonConnect.Text = "&Disconnect"; 
       backgroundWorker1.RunWorkerAsync(); 
      } 
      else 
      { 
       backgroundWorker1.CancelAsync(); 
      } 
     } 

     private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
     { 
      while (!backgroundWorker1.CancellationPending) 
      { 
       camera.Update(); 
       pictureBox1.Image = camera.CalcBitmap(); 
      } 
     } 

     private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
     { 
      camera.Disconnect(); 
      buttonConnect.Text = "&Connect"; 
     } 
     #endregion 
    } 
} 
+0

Я нашел ответ. – Aleksa

+0

Я создал таймер, который каждые 200 мс принимает кадр из pictureBox1.Image. – Aleksa

ответ

0

Вы, вероятно, столкнуться с проблемами при попытке нарисовать Bitmaps слишком быстро. Задержка в 200 мс делает эти проблемы менее вероятными, но на самом деле не решает проблему. Вы можете попробовать этот код, чтобы пропускать кадры, которые не могут быть сделаны:

namespace TestGUI 
{ 
public partial class TestGUIForm : Form 
{ 
    private IAsyncResult setBMPResult; 
    private Webcam; 

    private delegate void SetBmpDelegate(Bitmap b); 

    /// <summary> 
    /// Standard constructor. 
    /// </summary> 
    public TestGUIForm() 
    { 
     InitializeComponent(); 
     this.FormClosing += TestGUIForm_FormClosing; 

     cam = new WebCam(); 
    } 

    private void TestGUIForm_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     backgroundWorkerGetFrames.CancelAsync(); 
    } 

    private void buttonConnect_Click(object sender, EventArgs e) 
    { 
     if (cam.IsConnected()) 
     { 
      // if we are already connected, just disable the button and cancel the display thread, the actual disconnection takes place in the *_RunWorkerCompleted method. 
      buttonConnect.Enabled = false; 
      backgroundWorkerGetFrames.CancelAsync(); 
     } 
     else 
     { 
      // connect the camera and start the display background worker. 
      buttonConnect.Enabled = false; 
      try 
      { 
       cam.Connect(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Connection error: " + ex.Message); 
       buttonConnect.Enabled = true; 
       return; 
      } 
      buttonConnect.Text = "Disconnect"; 
      backgroundWorkerGetFrames.RunWorkerAsync(); 
      buttonConnect.Enabled = true; 
     } 
    } 

    private void backgroundWorkerGetFrames_DoWork(object sender, DoWorkEventArgs e) 
    { 
     while (!backgroundWorkerGetFrames.CancellationPending) 
     { 
      // capture a new frame 
      cam.Update(); 
      // get the current frame 
      Bitmap bitmap = cam.CalcBitmap(); 
      // set the picturebox-bitmap in the main thread to avoid concurrency issues (a few helper methods required, easier/nicer solutions welcome). 
      this.InvokeSetBmp(bitmap); 
     } 
    } 
    private void InvokeSetBmp(Bitmap bmp) 
    { 
     if (setBMPResult == null || setBMPResult.IsCompleted) 
     { 
      setBMPResult = this.BeginInvoke(new SetBmpDelegate(this.SetImage), bmp); 
     } 
    } 

    private void SetImage(Bitmap bitmap) 
    { 
     Bitmap oldBitmap = (Bitmap)pictureBoxImageStream.Image; 
     pictureBoxImageStream.Image = bitmap; 
     if (oldBitmap != null && oldBitmap != bitmap) 
      oldBitmap.Dispose(); 
    } 

    private void backgroundWorkerGetFrames_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     // disconnect camera and re-enable button. 
     cam.Disconnect(); 
     buttonConnect.Text = "Connect"; 
     buttonConnect.Enabled = true; 
    } 
} 
} 
0

Вы можете создать DispatcherTimer со временем вы whant. Его прерывание таймера.

DispatcherTimer timer = new DispatcherTimer(); 

timer.Interval = new TimeSpan(0, 0, 0, 0, 20); // 20ms 
timer.Tick += new EventHandler(timer_Tick); 
timer.Start(); 

Тогда, когда таймер пометит, он достигнет места, и вы получите изображение с вашей камеры.

void timer_Tick(object sender, EventArgs e) 
{ 
    camera.Update(); 
    pictureBox1.Image = camera.CalcBitmap(); 
}