Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在PC中使用网络摄像头作为环境光传感器_C#_Windows_Windows 8_Webcam_.net 4.5 - Fatal编程技术网

C# 在PC中使用网络摄像头作为环境光传感器

C# 在PC中使用网络摄像头作为环境光传感器,c#,windows,windows-8,webcam,.net-4.5,C#,Windows,Windows 8,Webcam,.net 4.5,是否可以将pc的网络摄像头用作环境光传感器? 我正在Windows 8 pro中使用.Net 4.5 framework。Philippe对这个问题的回答是:有代码可以计算bitmat图像的平均RGB值(代码中的bm): 您还可以将RGB值转换为HSL,然后查看“L”,这可能更准确,我不知道。您的意思是只检测网络摄像头可以查看房间中的光线?只需使用WIA或其他库从中拍摄一张照片,然后查看像素以确定它的暗度或亮度。感谢您的回复。我将搜索并了解WIA的使用,我还将查看RGE.net是否有帮助。但是你

是否可以将pc的网络摄像头用作环境光传感器?
我正在Windows 8 pro中使用.Net 4.5 framework。

Philippe对这个问题的回答是:有代码可以计算bitmat图像的平均RGB值(代码中的bm):


您还可以将RGB值转换为HSL,然后查看“L”,这可能更准确,我不知道。

您的意思是只检测网络摄像头可以查看房间中的光线?只需使用WIA或其他库从中拍摄一张照片,然后查看像素以确定它的暗度或亮度。感谢您的回复。我将搜索并了解WIA的使用,我还将查看RGE.net是否有帮助。但是你能帮我理解如何通过像素来确定它吗?谢谢你的回复,这将解决亮度部分的问题。从你之前的评论中,我搜索了WIA以及访问cam的有用性,并找到了这篇文章——这篇文章说WIA在Windows 8中不受支持:(@indranilpal如果你阅读了你的链接,Rob Caplan说“问题和答案是特定于Metro风格的应用程序的。WIA在桌面应用程序中受支持。”。它可以在Windows 8上运行,只是不能在Metro应用程序上运行,普通dekstop应用程序可以使用WIA。此外,还有很多库也可以用于访问网络摄像头。是的,我忽略了这一部分……它确实支持桌面模式。我将尝试实现它,并在遇到任何问题时通知您。
BitmapData srcData = bm.LockBits(
        new Rectangle(0, 0, bm.Width, bm.Height), 
        ImageLockMode.ReadOnly, 
        PixelFormat.Format32bppArgb);

int stride = srcData.Stride;

IntPtr Scan0 = dstData.Scan0;

long[] totals = new long[] {0,0,0};

int width = bm.Width;
int height = bm.Height;

unsafe
{
  byte* p = (byte*) (void*) Scan0;

  for (int y = 0; y < height; y++)
  {
    for (int x = 0; x < width; x++)
    {
      for (int color = 0; color < 3; color++)
      {
        int idx = (y*stride) + x*4 + color;

        totals[color] += p[idx];
      }
    }
  }
}

int avgR = totals[0] / (width*height);
int avgG = totals[1] / (width*height);
int avgB = totals[2] / (width*height);
Color imageColor = Color.FromARGB(avgR, avgG, avgB);
double brightness = imageColor.GetBrightness();