Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using AForge.Video;
- using AForge.Video.DirectShow;
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Diagnostics;
- namespace Services.CameraServices
- {
- /// <summary>
- /// Provides a stream of <see cref="Bitmap"/> images from a Microsoft Kinect.
- /// </summary>
- public class RgbStream
- {
- private const int CONNECT_SLEEP_TIME = 1000;
- private const int MAX_SLEEPS = 10;
- private static VideoCaptureDevice videoSource;
- private static Bitmap currentImage;
- private static object updaterLock;
- /// <summary>
- /// Gets the last bitmap captured by the camera.
- /// Notice that the bitmap is automatically disposed when a new image is ready - use <see cref="ImageUpdated"/> for notifications.
- /// </summary>
- public Bitmap Bitmap
- {
- get
- {
- return currentImage;
- }
- }
- private static RgbStream instance;
- /// <summary>
- /// Gets the singleton instance of <see cref="RgbStream"/>.
- /// </summary>
- public static RgbStream Instance
- {
- get
- {
- if (instance == null)
- {
- //List all available video sources. (That can be webcams as well as tv cards, etc)
- FilterInfoCollection videosources = new FilterInfoCollection(FilterCategory.VideoInputDevice);
- //Check if atleast one video source is available
- if (videosources != null)
- {
- //For example use first video device. You may check if this is your webcam.
- videoSource = new VideoCaptureDevice(videosources[0].MonikerString);
- try
- {
- //Check if the video device provides a list of supported resolutions
- if (videoSource.VideoCapabilities.Length > 0)
- {
- string highestSolution = "0;0";
- int res = 0;
- //Search for the highest resolution
- for (int i = 0; i < videoSource.VideoCapabilities.Length; i++)
- {
- //if (videoSource.VideoCapabilities[i].FrameSize.Width > Convert.ToInt32(highestSolution.Split(';')[0]))
- // highestSolution = videoSource.VideoCapabilities[i].FrameSize.Width.ToString() + ";" + i.ToString();
- if (videoSource.VideoCapabilities[i].FrameSize.Width == 640 && videoSource.VideoCapabilities[i].FrameSize.Height == 480)
- res = i;
- }
- //Set the highest resolution as active
- videoSource.VideoResolution = videoSource.VideoCapabilities[res];//[Convert.ToInt32(highestSolution.Split(';')[1])];
- }
- }
- catch { }
- //Start recording
- if (videoSource != null)
- {
- videoSource.Stop();
- }
- }
- instance = new RgbStream(videoSource);
- }
- return instance;
- }
- }
- private RgbStream(VideoCaptureDevice video)
- {
- videoSource = video;
- currentImage = null;
- updaterLock = new object();
- if (videoSource == null)
- return;
- //Start the sensor and wait for it to be ready
- videoSource.Start();
- while (!videoSource.IsRunning) { }
- //Event for when a frame from the video is ready
- videoSource.NewFrame += (s, e) =>
- {
- if (System.Threading.Monitor.TryEnter(updaterLock, 20))
- {
- Bitmap old = currentImage;
- currentImage = (Bitmap)e.Frame.Clone();
- currentImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
- if (currentImage != null)
- {
- if (ImageUpdated != null)
- ImageUpdated(this, EventArgs.Empty);
- if (old != null)
- {
- old.Dispose();
- old = null;
- }
- }
- else
- currentImage = old;
- System.Threading.Monitor.Exit(updaterLock);
- }
- };
- }
- /// <summary>
- /// Occurs when the <see cref="RgbStream.Bitmap"/> property is updated with a new image.
- /// After this event occurs, the previous image is automatically disposed.
- /// </summary>
- public event EventHandler ImageUpdated;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement