Subversion Repositories Projects

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel.Composition;

namespace Touchless.Vision.Camera
{
    public static class CameraService
    {
        private static WebCamLib.CameraMethods _cameraMethods;

        private static WebCamLib.CameraMethods CameraMethods
        {
            get
            {
                if (_cameraMethods == null)
                {
                    try
                    {
                        _cameraMethods = new WebCamLib.CameraMethods();

                    }
                    catch (Exception)
                    {
                    }

                }

                return _cameraMethods;
            }
        }

        [Export(ExportInterfaceNames.DefaultCamera)]
        public static Camera DefaultCamera
        {
            get { return AvailableCameras.FirstOrDefault(); }
        }

        private static List<Camera> _availableCameras;
        public static IList<Camera> AvailableCameras
        {
            get
            {
                try
                {
                    if (_availableCameras == null && CameraMethods != null)
                    {
                        _availableCameras = BuildCameraList().ToList();
                    }

                }
                catch (Exception)
                {

                }


                return _availableCameras;
            }
        }
        public static void ClearCameraList()
        {
            _availableCameras = null;
            _cameraMethods = null;
        }

        private static IEnumerable<Camera> BuildCameraList()
        {
            if (CameraMethods != null)
            {
                for (int i = 0; i < CameraMethods.Count; i++)
                {
                    WebCamLib.CameraInfo cameraInfo = CameraMethods.GetCameraInfo(i);
                    yield return new Camera(CameraMethods, cameraInfo.Name, cameraInfo.Index);
                }
            }
        }
    }
}