Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Emgu.CV.CvEnum;
  11. using Emgu.CV.Structure;
  12. using Emgu.Util;
  13. using Emgu.CV;
  14.  
  15. namespace Emgu_Cam
  16. {
  17.     public partial class MainWindow : Form
  18.     {
  19.         //  declaring global variables
  20.         private Capture capture;            //takes images from camera as image frames
  21.         private bool captureInProgress;     //checks if capture is executing
  22.  
  23.         public MainWindow()
  24.         {
  25.             InitializeComponent();
  26.         }
  27.  
  28.         private void captureButton_Click(object sender, EventArgs e)
  29.         {
  30.             CaptureImage();
  31.             captureButton.Enabled = false;
  32.             stopCaptureButton.Enabled = true;
  33.         }
  34.  
  35.  
  36.  
  37.         private void ProcessFrame(object sender, EventArgs arg)
  38.         {
  39.             //we start from taking a frame from the camera
  40.             Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
  41.  
  42.             //now we must scale the image
  43.             double scale;
  44.             if((camImageBox.Width / (double)ImageFrame.Width) >= (camImageBox.Height / (double)ImageFrame.Height))
  45.             {
  46.                 scale = camImageBox.Width / (double)ImageFrame.Width;
  47.             }
  48.             else
  49.             {
  50.                 scale = camImageBox.Height / (double)ImageFrame.Height;
  51.             }
  52.  
  53.             ImageFrame = ImageFrame.Resize(scale, INTER.CV_INTER_LINEAR);
  54.  
  55.             //insert the image
  56.             camImageBox.Image = ImageFrame;
  57.         }
  58.         private void CaptureImage()
  59.         {
  60.             #region if capture is not created, create it now
  61.             if (capture == null)
  62.             {
  63.                 try
  64.                 {
  65.                     capture = new Capture();
  66.  
  67.                 }
  68.                 catch (NullReferenceException excpt)
  69.                 {
  70.                     MessageBox.Show(excpt.Message);
  71.                 }
  72.             }
  73.             #endregion
  74.  
  75.             if (capture != null)
  76.             {              
  77.                 if(captureInProgress == false)
  78.                 {
  79.                     Application.Idle += ProcessFrame;
  80.                     captureInProgress = true;
  81.                 }
  82.                 else
  83.                 {
  84.                     Application.Idle -= ProcessFrame;
  85.                     captureInProgress = false;
  86.                 }
  87.                
  88.             }
  89.         }
  90.  
  91.         private void stopCaptureButton_Click(object sender, EventArgs e)
  92.         {
  93.             CaptureImage();
  94.             captureButton.Enabled = true;
  95.             stopCaptureButton.Enabled = false;
  96.         }
  97.  
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement