Advertisement
thilemann

AForge.Net

Feb 5th, 2014
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.27 KB | None | 0 0
  1. using AForge.Video;
  2. using AForge.Video.DirectShow;
  3. using System;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Diagnostics;
  11.  
  12. namespace Services.CameraServices
  13. {
  14.     /// <summary>
  15.     /// Provides a stream of <see cref="Bitmap"/> images from a Microsoft Kinect.
  16.     /// </summary>
  17.     public class RgbStream
  18.     {
  19.         private const int CONNECT_SLEEP_TIME = 1000;
  20.         private const int MAX_SLEEPS = 10;
  21.  
  22.         private static VideoCaptureDevice videoSource;
  23.         private static Bitmap currentImage;
  24.         private static object updaterLock;
  25.  
  26.         /// <summary>
  27.         /// Gets the last bitmap captured by the camera.
  28.         /// Notice that the bitmap is automatically disposed when a new image is ready - use <see cref="ImageUpdated"/> for notifications.
  29.         /// </summary>
  30.         public Bitmap Bitmap
  31.         {
  32.             get
  33.             {
  34.                 return currentImage;  
  35.             }
  36.         }
  37.  
  38.         private static RgbStream instance;
  39.         /// <summary>
  40.         /// Gets the singleton instance of <see cref="RgbStream"/>.
  41.         /// </summary>
  42.         public static RgbStream Instance
  43.         {
  44.             get
  45.             {
  46.                 if (instance == null)
  47.                 {
  48.                     //List all available video sources. (That can be webcams as well as tv cards, etc)
  49.                     FilterInfoCollection videosources = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  50.  
  51.                     //Check if atleast one video source is available
  52.                     if (videosources != null)
  53.                     {
  54.                         //For example use first video device. You may check if this is your webcam.
  55.                         videoSource = new VideoCaptureDevice(videosources[0].MonikerString);
  56.  
  57.                         try
  58.                         {
  59.                             //Check if the video device provides a list of supported resolutions
  60.                             if (videoSource.VideoCapabilities.Length > 0)
  61.                             {
  62.  
  63.                                 string highestSolution = "0;0";
  64.                                 int res = 0;
  65.                                 //Search for the highest resolution
  66.                                 for (int i = 0; i < videoSource.VideoCapabilities.Length; i++)
  67.                                 {
  68.                                     //if (videoSource.VideoCapabilities[i].FrameSize.Width > Convert.ToInt32(highestSolution.Split(';')[0]))
  69.                                     //    highestSolution = videoSource.VideoCapabilities[i].FrameSize.Width.ToString() + ";" + i.ToString();
  70.                                     if (videoSource.VideoCapabilities[i].FrameSize.Width == 640 && videoSource.VideoCapabilities[i].FrameSize.Height == 480)
  71.                                         res = i;
  72.                                 }
  73.  
  74.                                 //Set the highest resolution as active
  75.                                 videoSource.VideoResolution = videoSource.VideoCapabilities[res];//[Convert.ToInt32(highestSolution.Split(';')[1])];
  76.                             }
  77.                         }
  78.                         catch { }
  79.  
  80.                         //Start recording
  81.                         if (videoSource != null)
  82.                         {
  83.                             videoSource.Stop();
  84.                         }
  85.                     }
  86.  
  87.                     instance = new RgbStream(videoSource);
  88.                 }
  89.  
  90.                 return instance;
  91.             }
  92.         }
  93.  
  94.         private RgbStream(VideoCaptureDevice video)
  95.         {
  96.             videoSource = video;
  97.             currentImage = null;
  98.             updaterLock = new object();
  99.  
  100.             if (videoSource == null)
  101.                 return;
  102.  
  103.             //Start the sensor and wait for it to be ready
  104.             videoSource.Start();
  105.             while (!videoSource.IsRunning) { }
  106.  
  107.             //Event for when a frame from the video is ready
  108.             videoSource.NewFrame += (s, e) =>
  109.             {
  110.                 if (System.Threading.Monitor.TryEnter(updaterLock, 20))
  111.                 {
  112.                     Bitmap old = currentImage;
  113.  
  114.                     currentImage = (Bitmap)e.Frame.Clone();
  115.                     currentImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
  116.  
  117.                     if (currentImage != null)
  118.                     {
  119.                         if (ImageUpdated != null)
  120.                             ImageUpdated(this, EventArgs.Empty);
  121.  
  122.                         if (old != null)
  123.                         {
  124.                             old.Dispose();
  125.                             old = null;
  126.                         }
  127.                     }
  128.                     else
  129.                         currentImage = old;
  130.  
  131.                     System.Threading.Monitor.Exit(updaterLock);
  132.                 }
  133.             };
  134.         }
  135.  
  136.         /// <summary>
  137.         /// Occurs when the <see cref="RgbStream.Bitmap"/> property is updated with a new image.
  138.         /// After this event occurs, the previous image is automatically disposed.
  139.         /// </summary>
  140.         public event EventHandler ImageUpdated;
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement