How to Connect to a Usb Webcam in .Net

How to find camera attached by USB cable, using C#, so I can then copy stored images

Using this script and the nuget package "MediaDevices" from "Ralf Beckers" you can copy all files from your camera like this in the script below. Just make sure to change stuff like "DEVICE NAME", @"\Internal Storage\" and the location to my desktop :)":

using System.IO;
using MediaDevices;

private void button3_Click(object sender, EventArgs e)
{
var devices = MediaDevice.GetDevices();
foreach(var device in devices)
{
MessageBox.Show(device.FriendlyName);
if (device.FriendlyName == "DEVICE NAME")
{
device.Connect();

var drs = device.GetFiles(@"\Internal Storage\");
foreach (string str in drs)
{
MessageBox.Show(str);
MemoryStream memoryStream = new System.IO.MemoryStream();
device.DownloadFile(str, memoryStream);
memoryStream.Position = 0;
WriteStreamToDisk(@"C:\Users\nikla\Desktop\temp\" + str.Substring(str.LastIndexOf("\\") + 1), memoryStream);
}

}
}

}

static void WriteStreamToDisk(string filePath, MemoryStream memoryStream)
{
using (FileStream file = new FileStream(filePath, FileMode.Create, System.IO.FileAccess.Write))
{
byte[] bytes = new byte[memoryStream.Length];
memoryStream.Read(bytes, 0, (int)memoryStream.Length);
file.Write(bytes, 0, bytes.Length);
memoryStream.Close();
}
}

You can index more files with device.GetDirectories(@"\Internal Storage\") and run through them with a foreach loop.

Credit to "@ZackOfAllTrades" for bringing this nuget packet to my attention

Record a video via a usb WebCam in csharp

Video capture APIs in Windows are:

  1. Video for Windows (simple, limited, deprecated, really outdated)
  2. DirectShow
  3. Media Foundation

All three are native APIs, so you need wrappers to interface from managed code. #3 is supposed to be a successor to #2 but reality makes #2 still the best - in general terms - API for video capture out there. In particular, Expression Encoder uses DirectShow.

DirectShow.NET lets you do DirectShow from C#, and it has CapWMV Sample for you:

Capture from video capture devices to WMV files.

MFCaptureToFile Sample shows you ho to achieve video capture via Media Foundation:

Shows how to capture video from a video camera to a file.

Web cam interfaces in VB.net

There are a few questions on this subject already, and the consensus seems to be DirectShow is the way to go. Try these links:

Webcam usage in C#

Access webcam and microphone input in VB.net

Access web cam using vb.net

Capturing image from WebCam in .Net Core 2.0

For capturing images and other image processing activities, probably you can try OpenCvSharp.

It has support for .NET Core.

https://github.com/shimat/opencvsharp

NuGet Packages are available (look for more details in the above link on the correct one(s) to choose).

Here is an example for capturing video:

https://github.com/shimat/opencvsharp_samples/tree/master/CameraOpenCV

if you pass 0 (zero) as input to VideoCapture, it should open the default camera.

Something like this (not tried):

VideoCapture capture = new VideoCapture(0); //assumption based on how actual openCV works.

Webcam Web Server in Dot Net

If you wish you can install the NuGet package NequeoFFmpeg, this is a C++/CLI wrapper of some FFmpeg tools. One thing you can do is use this wrapper to get WebCam data through the FFmpeg binaries. You can get my pre-built FFmpeg binaries from FFmpeg please use version 2016_01_15.

Code sample:

    private void Capture()
{
Nequeo.Media.FFmpeg.MediaDemux demux = new Nequeo.Media.FFmpeg.MediaDemux();
demux.OpenDevice("video=Integrated Webcam", true, false);

// create instance of video writer
Nequeo.Media.FFmpeg.VideoFileWriter writer = new Nequeo.Media.FFmpeg.VideoFileWriter();
writer.Open(@"C:\Temp\Misc\ffmpeg_screen_capture_video.avi", demux.Width, demux.Height, demux.FrameRate, Nequeo.Media.FFmpeg.VideoCodec.MPEG4);

byte[] sound = null;
Bitmap[] image = null;

List<Bitmap> video = new List<Bitmap>();
long audioPos = 0;
long videoPos = 0;

int captureCount = 0;
int captureNumber = 500;

while ((demux.ReadFrame(out sound, out image, out audioPos, out videoPos) > 0) && captureCount < captureNumber)
{
if (image != null && image.Length > 0)
{
captureCount++;

for (int i = 0; i < image.Length; i++)
{
writer.WriteVideoFrame(image[i]);
image[i].Dispose();
}
}
}

writer.Close();
demux.Close();
}

Set the video capture device name, In the sample above I write to a file, but you can just write the Bitmap to a stream. You could compress the bitmap and then write to a stream. You could change the Bitmap to Jpeg and then send to a stream.

FFmpeg can stream live WebCam video see: StreamingGuide



Related Topics



Leave a reply



Submit