Beispiel #1
0
        /// <summary>
        /// Initializes camera, builds and runs graph for control.
        /// </summary>
        /// <param name="moniker">Moniker (device identification) of camera.</param>
        /// <param name="resolution">Resolution of camera's output.</param>
        public void SetCamera(IMoniker moniker, Resolution resolution)
        {
            // Close current if it was opened
            CloseCamera();

            if (moniker == null)
            {
                return;
            }

            // Create camera object
            _Camera = new Camera();

            if (!string.IsNullOrEmpty(_DirectShowLogFilepath))
            {
                _Camera.DirectShowLogFilepath = _DirectShowLogFilepath;
            }

            // select resolution
            //ResolutionList resolutions = Camera.GetResolutionList(moniker);

            if (resolution != null)
            {
                _Camera.Resolution = resolution;
            }
            else
            {
                // When there is no resolution has been specified, try to find the best one

                ResolutionList resolution_list = Camera.GetResolutionList(moniker);

                foreach (Resolution res in resolution_list)
                {
                    if (res.CompareTo(resolution) == 1)       // update resolution whenever res is larger than resolution (null is the smallest)
                    {
                        resolution = res;
                    }
                }
                _Camera.Resolution = resolution;
            }

            // Initialize
            _Camera.Initialize(this, moniker);

            // Build and Run graph
            _Camera.BuildGraph();
            _Camera.RunGraph();


            _Camera.OutputVideoSizeChanged += Camera_OutputVideoSizeChanged;
        }
Beispiel #2
0
        public static ResolutionList GetResolutionList(IMoniker moniker)
        {
            ResolutionList resolutionsAvailable = null;
            IFilterGraph2  graph    = new FilterGraph() as IFilterGraph2;
            IBaseFilter    ppFilter = null;

            try
            {
                DsError.ThrowExceptionForHR(graph.AddSourceFilterForMoniker(moniker, null, "Source Filter", out ppFilter));
                resolutionsAvailable = GetResolutionsAvailable(ppFilter);
            }
            finally
            {
                SafeReleaseComObject(graph);
                graph = null;
                SafeReleaseComObject(ppFilter);
                ppFilter = null;
            }
            return(resolutionsAvailable);
        }
Beispiel #3
0
        private static ResolutionList GetResolutionsAvailable(IBaseFilter captureFilter)
        {
            ResolutionList resolutionsAvailable = null;
            IPin           pinOutput            = null;

            try
            {
                pinOutput            = DsFindPin.ByDirection(captureFilter, PinDirection.Output, 0);
                resolutionsAvailable = GetResolutionsAvailable(pinOutput);
            }
            catch
            {
                throw;
            }
            finally
            {
                SafeReleaseComObject(pinOutput);
                pinOutput = null;
            }
            return(resolutionsAvailable);
        }
Beispiel #4
0
        private static ResolutionList GetResolutionsAvailable(IPin pinOutput)
        {
            ResolutionList list = new ResolutionList();
            AMMediaType    ppmt = null;
            IntPtr         zero = IntPtr.Zero;

            try
            {
                IAMStreamConfig config = pinOutput as IAMStreamConfig;
                DsError.ThrowExceptionForHR(config.SetFormat(null));
                int piCount = 0;
                int piSize  = 0;
                DsError.ThrowExceptionForHR(config.GetNumberOfCapabilities(out piCount, out piSize));
                for (int i = 0; i < piCount; i++)
                {
                    zero = Marshal.AllocCoTaskMem(piSize);
                    config.GetStreamCaps(i, out ppmt, zero);
                    if (IsBitCountAppropriate(GetBitCountForMediaType(ppmt)))
                    {
                        list.AddIfNew(GetResolutionForMediaType(ppmt));
                    }
                    FreeSCCMemory(ref zero);
                    FreeMediaType(ref ppmt);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                FreeSCCMemory(ref zero);
                FreeMediaType(ref ppmt);
            }
            return(list);
        }
Beispiel #5
0
 private void AddFilter_Source()
 {
     this.DX.CaptureFilter = null;
     DsError.ThrowExceptionForHR(this.DX.FilterGraph.AddSourceFilterForMoniker(this._Moniker, null, "Source Filter", out this.DX.CaptureFilter));
     this._ResolutionList = GetResolutionsAvailable(this.DX.CaptureFilter);
 }