【问题标题】:How to list camera available video resolution如何列出相机可用的视频分辨率
【发布时间】:2011-09-21 09:30:43
【问题描述】:

如果我的 PC 上连接了多个摄像头...我想知道特定摄像头的最佳可用分辨率...

例如,一些摄像头是高清或全高清(1,280×720 像素 (720p) 或 1,920×1,080 像素 (1080i/1080p)),或者最常见的是网络摄像头......

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

我的工作是使用 C# 编写 WPF(我正在使用 Directshow)

提前致谢

【问题讨论】:

    标签: c# .net wpf camera directshow.net


    【解决方案1】:

    这是我编写的代码,它非常适合我

    public static List<Point> 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<Point>();
            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 Point(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<Point>();
        }
    }
    

    (例如,这可以添加到 WPF-MediaKit 中的 VideoCaptureElement)

    【讨论】:

      【解决方案2】:

      我用它来获得最大帧大小,只需根据您的需要进行更改;)

      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;
                  }
              }
          }
      

      【讨论】:

      • 我找不到 DsUtils.FreeAMMediaType(...) 的内容。你能指导我吗?
      • 您使用的是 2010 版本吗?
      • 我猜第一个验证是你没有 DsUtils 类 ;) 最好的办法就是获取当前版本...directshownet.sourceforge.net
      • 较旧的包装器在不同的类中具有实用程序...我不太记得它是什么...不过是一个非常古老的构建...如果您使用的是旧构建,您将拥有一个进行一些更改以设置图表。如果您有任何问题,请随时问我。
      • 我在上面添加了 FreeAMMediaType 子(就在新版本之外)...所以如果您使用旧包装器,您可以轻松添加此块...(不确定 AMMediaType 结构是否为旧版本相同)
      【解决方案3】:

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

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

      不过,它们是 C++。

      【讨论】:

      • 谁能告诉我如何在c#中使用它或者给我一个c++中的示例
      • 您在 DirectShow.net 中有一个代表您的相机的对象吗?
      猜你喜欢
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 2018-01-18
      相关资源
      最近更新 更多