Detect Webcam using AForge

Hi,

I’m working on a game that needs to take picture with a webcam and then do some image analysis to determine the position of tokens.

For that I made a c# dll that use AForge library to detect webcam and do the analysis. It works when I try it on a c# application, but when I integrate the DLL in my game in Unity, it won’t detect any webcam.

I built my dll and each dll of AForge in framework 3.5 x86.

Here the code that should find the webcam:

 // Code update
            // enumerate video devices
            FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            // create video source
            LogFile.Instance.Log("Count" + videoDevices.Count);
            _findCamera = false;
            for (int i = 0; i < videoDevices.Count; i++)
            {
                LogFile.Instance.Log(videoDevices*.Name.Trim());*

if (videoDevices.Name.Trim() == CAM_NAME/true/) // Set “true” for tests with other camera.
{
videoSource = new VideoCaptureDevice(videoDevices*.MonikerString);
findCamera = true;*
}

}
if (_findCamera == false)
{
return;
}
While running in Unity, the videoDevices.count is 0.
*I found a post that says that it won’t work, but it was said in 2011. Directshow seemed not to be supported.I hope there is a way now. http://forum.unity3d.com/threads/76860-C-script*_
By the way, I have no error message. Just that the program can’t find the webcam.
Thank you for your help._

Hi @Byard ,

Did you ever this this question answered, as I’m having the same problem?

thanks,

  • Michael

Hello, I have the same problem as well, normally the following code snippet should return the available webcams.

FilterInfoCollection VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

However, “VideoCaptureDevices” is always 0; and I don’t have any error message.
Is there any solution for this problem ??