USB webcam support

Yes it should work fine. Actually this does just that:
http://code.tinyclr.com/project/355/ghi-usb-webcam-290/

Mike:

The code below gives me a problem Could you run it on your camera?

Note the variable for changing from continuous to start/stop.

using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Media;

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.USBHost;
using GHIElectronics.NETMF.System;

namespace USB_VideoTester
{
    public class Program
    {
        private static USBH_Webcam cam = null;
        private static USBH_Webcam.ImageFormat usedFormat = null;
        private static Thread captureThread = null;
        private static int frameCount = 0;

        private static Bitmap LCD = new Bitmap(SystemMetrics.ScreenWidth, SystemMetrics.ScreenHeight);

        // set the following variable to false 
        // to start and stop streaming for each frame
        static private bool constantStreaming = true;

        public static void Main()
        {
            USBHostController.DeviceConnectedEvent += OnDeviceConnected;
            Debug.Print("Waiting for camera to be recognized");
            while (true)
            {
                Thread.Sleep(10000);
                int save = frameCount;
                frameCount = 0;
                int frameRate = save / 10;
                Debug.Print("Frame Rate: " + frameRate.ToString());
            }

        }

        static void OnDeviceConnected(USBH_Device device)
        {
            if ((device.TYPE == USBH_DeviceType.Webcamera) && (cam == null))
            {
                cam = new USBH_Webcam(device);
                cam.Disconnected += OnCamDisconnected;

                // get formats
                USBH_Webcam.ImageFormat[] formats = cam.GetSupportedFormats();

                // select a format
                usedFormat = formats[1];

                Debug.Print("Camera inserted");
                captureThread = new Thread(CaptureThread);
                captureThread.Start();
            }
        }

        static void OnCamDisconnected(USBH_Webcam sender, USBH_WebcamEventArgs args)
        {

            // release camera
            cam.StopStreaming();
            captureThread.Abort();
            Thread.Sleep(250);
            cam = null;
        }

        

        static void CaptureThread()
        {
            try
            {
                if (constantStreaming)
                {
                    Debug.Print(" Constant Streaming started");
                    cam.StartStreaming(usedFormat);
                }

                while (true)
                {
                    if (!constantStreaming)
                    {
                        Debug.Print("Start Streaming");
                        cam.StartStreaming(usedFormat);
                        Debug.Print("Streaming started");
                    }
                    while (!cam.IsNewImageReady()) Thread.Sleep(10);
                    
                    cam.DrawImage(LCD, (LCD.Width - cam.CurrentFormat.Width) / 2, (LCD.Height - cam.CurrentFormat.Height) / 2, cam.CurrentFormat.Width, cam.CurrentFormat.Height);
                    if (!constantStreaming)
                    {
                        Debug.Print("Stopping streaming");
                        cam.StopStreaming();
                        Debug.Print("Streaming stopped");
                    }
                    LCD.Flush();
                    frameCount++;
                    if (!constantStreaming)
                    {
                        // see if a delay helps
                        Thread.Sleep(500);
                     }
                }
            }
            catch (Exception ex)
            {
                Debug.Print("*** Exception: " + ex.Message);
            }
        }
    }
}

It is working fine here. Maybe your camera doesn’t like stopping and re-starting (this is not usually done a PC). Try adding delays before and after every start/stop streaming…

Mike:

Great! It either the camera or a power issue.

I will try tonight and then see what happens.

BTW: I did notice that if the camera is streaming and I reset the Cobra I get an exception when I try issue a start streaming call.

Mike:

I have tried an external power supply and loooong delays. Neither helped.

I did notice something interesting. After stopping the streaming, I issued a GetSupportedFormat() call. I expected to get back the two formats that I had obtained before I started streaming. What was returned was zero formats. It looks like stopping the steaming did more than just stopping.

Unless there is something you want me to try, I am going to put the camera away.

I just ordered a GHI USB Camera. When I get it, I will go back to my project.

Thanks for the help.

Did you order the camera I ordered too, Mike?
I am still wondering why fullscreen results in a shattered image. :frowning:

I am/was using a Logitech Pro 9000 camera. I order the one that GHI is selling.

Once or twice I did notice a split image on the screen right after startup, but it went away within a few seconds.

I meant the other Mike :stuck_out_tongue:
As you see ahead in this thread my image was very shattered and would stay that way ???

Yes we ordered the camera and will try it here.

Great, let me know how it worked out ;D

Did you get it?

I’m also seeing the color problem. Using a Cobra and the camera from GHI, a blue one if that matters… :slight_smile:

The color starts out ok, but goes all 8 color after a while. Sometimes… :slight_smile:

Example of problem colors

Same snap but with OK colors

I have my Cobra with LCD mounted on a flexible pegboard. If I flex the board the colors on the screen often go bad. I think it has to do with the flexible cable. The cable of the Gadgeteer is a must better approach.

Mine is mounted inside the OEM case. I have a picture where top 1/4 of the image is corrupt, middle 1/2 is ok, bottom 1/4 is corrupt. This isn’t an LCD problem. Not in my case anyway…

Wondering how testing with “my” webcam proceeds?
I would like to use it :slight_smile:

Sorry for delay. af6767 and Robert Jacobs please try this firmware:
http://www.ghielectronics.com/downloads/NETMF/EMX_4_1_6_2.zip
It is top secret test firmware, don’t tell anybody about it.

Top secret! Lol :slight_smile:

I have full screen video now, perfect :slight_smile:
One problem though. Sometimes it just lags for about 1-2 seconds and then proceeds with streaming.

I kind of missed your reply, sorry about that. Been too busy lately. :-[