tags:

views:

687

answers:

3

hi

i am new in xna

i want to use my webcam and make webcam image as a background texture for 3D models

is there a function that calls webcam

thanks for suggestions

A: 

I ran into this problem myself a while ago, it's quite a messy solution that I came up with.

First, you need to use the motion_src library, you can find that here:

http://www.codeproject.com/KB/audio-video/Motion%5FDetection.aspx

That tutorial is all about motion detection, but if you download the demo code you can take the bit where it captures the input from the camera.

Now, add that as a reference to your xna project.

Once you have the system set up capturing a feed from the camera (all the details are in that tutorial, I won't repeat them here) you'll need to copy the feed (which is captured into a System.Drawing.Bitmap into an xna texture.

Texture2D image;

b = (System.Drawing.Bitmap)camera.LastFrame.Clone();
for (int j = 0; j < image.Height; j++)
{
    for (int i = 0; i < image.Width; i++)
    {
        c = b.GetPixel(i, j);
        colours[i + j * image.Width] = new Color(c.R, c.G, c.B, byte.MaxValue);
    }
}
image.SetData<Color>(colours);

You can then display the image texture using a normal call to spritebatch :)

Martin
thanks but this is too complex for me i didn't understand it
eomer
Which bit are you getting stuck on?
Martin
i just want to draw the images from the web cam to the screen in that example there are too many classes i dont know which one to examine and the author explained but he started to explain the functions after he get video frame i dont need threshold or erosion in my project
eomer
Well do the bit of the tutorial for capturing the image to a System.Drawing.Bitmap, then ignore the rest of the tutorial, once you have the Bitmap you just need to copy it into the XNA texture as I showed above and that's it.If you still don't get it I can possibly try and find my old project for doing this, although I'm not certain I still have it :/
Martin
i'm sorry but which tutorial are you talkin about there is no any tutorial headline or linki assumed you were talkin about the writings under the algorithm headline and those are started from the threshold
eomer
I've updated my answer to be a bit clearer
Martin
A: 

You could use the third party VideoTexture class. It can use a webcam or an AVI, MPEG, or WMV and gives you access to a Texture2D object with the current frame as the image that can be used with spritebatch or applied to 3D objects.

  1. Copy the VideoTexture.cs file into your own project.
  2. Change the namespace in the VideoTexture.cs file to the name of your project namespace.
  3. Add a reference to DirectShowLib-2005.dll from the DirectShow.NET library.
  4. Compile it. The VideoTexture class should now be available in your project.

I've never used it, but if you download the documentation it should help. It should just be a matter of creating a VideoTexture, and using its VideoTexture2D property to retrieve the Texture2D. Then you could set that as the texture for each effect for some 3D object.

Tim R.
do you have sample code about videoTexture classi found a videoTexture class but i couldn't manage to use it
eomer
Edited. Is that any more helpful?
Tim R.
+1  A: 

If you check out the AForge framework you'll find a sample program that does motion detection on webcam feeds.

http://code.google.com/p/aforge/

This framework has a very easy to get webcam feeds.

Basically ends up

VideoCaptureDevice device = new VideoCaptureDevice(monikor);
device.NewFrame += new AForge.Video.NewFrameEventHandler(webcam_NewFrame);
webcam.Start();

where you can get the webcam's monikor by calling code similar to...

FilterInfoCollection webcamList = new FilterInfoCollection(FilterCategory.VideoInputDevice);

foreach(FilterInfo info in webcamList)
{
    string monikor = info.MonikorString;
    string deviceName = info.Name;
}

This is the best/easiest way i've come across to get webcam feeds.

Then you can convert the Bitmap into a Texture2D and display as you wish!

Kurru