如何列出相机可用的视频分辨率

时间:2021-03-25 15:59:48

if I have more than one camera attached to my PC ... I want to know the best available resolutions for a specific camera ...

如果我的电脑上有多个摄像头……我想知道一架特定相机的最佳分辨率……

for example some cameras are HD or FullHD (1,280×720 pixels (720p) or 1,920×1,080 pixels (1080i/1080p)) or the most common are web cameras....

例如一些摄像机高清或FullHD(1280×720像素(720 p)或1920×1080像素(1080 i / 1080 p))或最常见的网络摄像头....

I want to know at least the best video mode that the camera work properly...(the mode that the camera made to work with)

我想至少知道相机正常工作的最佳视频模式……(相机的工作模式)

my work is on WPF using C# (I am using Directshow)

我的工作是使用c#处理WPF(我使用的是Directshow)

thanks in advance

谢谢提前

3 个解决方案

#1


9  

i use this to get max frame size, just change to suit your needs ;)

我用这个来得到最大的帧大小,只要根据你的需要改变即可)

private Point GetMaxFrameSize(IPin pStill)
    {
        VideoInfoHeader v;

        IAMStreamConfig videoStreamConfig = pStill as IAMStreamConfig;

        int iCount = 0, iSize = 0;
        videoStreamConfig.GetNumberOfCapabilities(out iCount, out iSize);

        IntPtr TaskMemPointer = Marshal.AllocCoTaskMem(iSize);

        int iMaxHeight = 0;
        int iMaxWidth = 0;

        for (int iFormat = 0; iFormat < iCount; iFormat++)
        {
            AMMediaType pmtConfig = null;
            IntPtr ptr = IntPtr.Zero;

            videoStreamConfig.GetStreamCaps(iFormat, out pmtConfig, TaskMemPointer);

            v = (VideoInfoHeader)Marshal.PtrToStructure(pmtConfig.formatPtr, typeof(VideoInfoHeader));
            if (v.BmiHeader.Width > iMaxWidth)
            {
                iMaxWidth = v.BmiHeader.Width;
                iMaxHeight = v.BmiHeader.Height;
            }
            DsUtils.FreeAMMediaType(pmtConfig);

        }

        Marshal.FreeCoTaskMem(TaskMemPointer);


        return new Point(iMaxWidth, iMaxHeight);
    }


    /// <summary>
    ///  Free the nested structures and release any
    ///  COM objects within an AMMediaType struct.
    /// </summary>
    public static void FreeAMMediaType(AMMediaType mediaType)
    {
        if (mediaType != null)
        {
            if (mediaType.formatSize != 0)
            {
                Marshal.FreeCoTaskMem(mediaType.formatPtr);
                mediaType.formatSize = 0;
                mediaType.formatPtr = IntPtr.Zero;
            }
            if (mediaType.unkPtr != IntPtr.Zero)
            {
                Marshal.Release(mediaType.unkPtr);
                mediaType.unkPtr = IntPtr.Zero;
            }
        }
    }

#2


7  

This is a code that I wrote, its working perfectly for me

这是我写的代码,它对我来说非常有用

    public static List<Resolution> GetAllAvailableResolution(DsDevice vidDev)
    {
        try
        {
            int hr;
            int max = 0;
            int bitCount = 0;

            IBaseFilter sourceFilter = null;

            var m_FilterGraph2 = new FilterGraph() as IFilterGraph2;

            hr = m_FilterGraph2.AddSourceFilterForMoniker(vidDev.Mon, null, vidDev.Name, out sourceFilter);

            var pRaw2 = DsFindPin.ByCategory(sourceFilter, PinCategory.Capture, 0);

            var AvailableResolutions = new List<Resolution>();

            VideoInfoHeader v = new VideoInfoHeader();
            IEnumMediaTypes mediaTypeEnum;
            hr = pRaw2.EnumMediaTypes(out mediaTypeEnum);

            AMMediaType[] mediaTypes = new AMMediaType[1];
            IntPtr fetched = IntPtr.Zero;
            hr = mediaTypeEnum.Next(1, mediaTypes, fetched);

            while (fetched != null && mediaTypes[0] != null)
            {
                Marshal.PtrToStructure(mediaTypes[0].formatPtr, v);
                if (v.BmiHeader.Size != 0 && v.BmiHeader.BitCount != 0)
                {
                    if (v.BmiHeader.BitCount > bitCount)
                    {
                        AvailableResolutions.Clear();
                        max = 0;
                        bitCount = v.BmiHeader.BitCount;


                    }
                    AvailableResolutions.Add(new Resolution(v.BmiHeader.Width, v.BmiHeader.Height));
                    if (v.BmiHeader.Width > max || v.BmiHeader.Height > max)
                        max = (Math.Max(v.BmiHeader.Width, v.BmiHeader.Height));
                }
                hr = mediaTypeEnum.Next(1, mediaTypes, fetched);
            }
            return AvailableResolutions;
        }

        catch (Exception ex)
        {
            Log(ex);
            return new List<Resolution>();
        }
    }

#3


2  

According to this webpage: http://www.e-consystems.com/blog/camera/?p=651, you should use this call for getting the capabilities of this device:

根据此网页:http://www.e-consystems.com/blog/camera/?p=651,您应该使用此调用来获取该设备的功能:

g_DShowCaptureGraph.GetNumberOfCapabilities(nStream, &iCount, &iSize);
g_DShowCaptureGraph.GetStreamCaps(nStream,iFormat, &pmtConfig, (BYTE*)&scc);

They are C++, however.

然而,他们是c++。

#1


9  

i use this to get max frame size, just change to suit your needs ;)

我用这个来得到最大的帧大小,只要根据你的需要改变即可)

private Point GetMaxFrameSize(IPin pStill)
    {
        VideoInfoHeader v;

        IAMStreamConfig videoStreamConfig = pStill as IAMStreamConfig;

        int iCount = 0, iSize = 0;
        videoStreamConfig.GetNumberOfCapabilities(out iCount, out iSize);

        IntPtr TaskMemPointer = Marshal.AllocCoTaskMem(iSize);

        int iMaxHeight = 0;
        int iMaxWidth = 0;

        for (int iFormat = 0; iFormat < iCount; iFormat++)
        {
            AMMediaType pmtConfig = null;
            IntPtr ptr = IntPtr.Zero;

            videoStreamConfig.GetStreamCaps(iFormat, out pmtConfig, TaskMemPointer);

            v = (VideoInfoHeader)Marshal.PtrToStructure(pmtConfig.formatPtr, typeof(VideoInfoHeader));
            if (v.BmiHeader.Width > iMaxWidth)
            {
                iMaxWidth = v.BmiHeader.Width;
                iMaxHeight = v.BmiHeader.Height;
            }
            DsUtils.FreeAMMediaType(pmtConfig);

        }

        Marshal.FreeCoTaskMem(TaskMemPointer);


        return new Point(iMaxWidth, iMaxHeight);
    }


    /// <summary>
    ///  Free the nested structures and release any
    ///  COM objects within an AMMediaType struct.
    /// </summary>
    public static void FreeAMMediaType(AMMediaType mediaType)
    {
        if (mediaType != null)
        {
            if (mediaType.formatSize != 0)
            {
                Marshal.FreeCoTaskMem(mediaType.formatPtr);
                mediaType.formatSize = 0;
                mediaType.formatPtr = IntPtr.Zero;
            }
            if (mediaType.unkPtr != IntPtr.Zero)
            {
                Marshal.Release(mediaType.unkPtr);
                mediaType.unkPtr = IntPtr.Zero;
            }
        }
    }

#2


7  

This is a code that I wrote, its working perfectly for me

这是我写的代码,它对我来说非常有用

    public static List<Resolution> GetAllAvailableResolution(DsDevice vidDev)
    {
        try
        {
            int hr;
            int max = 0;
            int bitCount = 0;

            IBaseFilter sourceFilter = null;

            var m_FilterGraph2 = new FilterGraph() as IFilterGraph2;

            hr = m_FilterGraph2.AddSourceFilterForMoniker(vidDev.Mon, null, vidDev.Name, out sourceFilter);

            var pRaw2 = DsFindPin.ByCategory(sourceFilter, PinCategory.Capture, 0);

            var AvailableResolutions = new List<Resolution>();

            VideoInfoHeader v = new VideoInfoHeader();
            IEnumMediaTypes mediaTypeEnum;
            hr = pRaw2.EnumMediaTypes(out mediaTypeEnum);

            AMMediaType[] mediaTypes = new AMMediaType[1];
            IntPtr fetched = IntPtr.Zero;
            hr = mediaTypeEnum.Next(1, mediaTypes, fetched);

            while (fetched != null && mediaTypes[0] != null)
            {
                Marshal.PtrToStructure(mediaTypes[0].formatPtr, v);
                if (v.BmiHeader.Size != 0 && v.BmiHeader.BitCount != 0)
                {
                    if (v.BmiHeader.BitCount > bitCount)
                    {
                        AvailableResolutions.Clear();
                        max = 0;
                        bitCount = v.BmiHeader.BitCount;


                    }
                    AvailableResolutions.Add(new Resolution(v.BmiHeader.Width, v.BmiHeader.Height));
                    if (v.BmiHeader.Width > max || v.BmiHeader.Height > max)
                        max = (Math.Max(v.BmiHeader.Width, v.BmiHeader.Height));
                }
                hr = mediaTypeEnum.Next(1, mediaTypes, fetched);
            }
            return AvailableResolutions;
        }

        catch (Exception ex)
        {
            Log(ex);
            return new List<Resolution>();
        }
    }

#3


2  

According to this webpage: http://www.e-consystems.com/blog/camera/?p=651, you should use this call for getting the capabilities of this device:

根据此网页:http://www.e-consystems.com/blog/camera/?p=651,您应该使用此调用来获取该设备的功能:

g_DShowCaptureGraph.GetNumberOfCapabilities(nStream, &iCount, &iSize);
g_DShowCaptureGraph.GetStreamCaps(nStream,iFormat, &pmtConfig, (BYTE*)&scc);

They are C++, however.

然而,他们是c++。