Use a webcam plugged in a Raspberry pi with Mono in C# using RaspberryCam

RaspberryCam is a Mono/.NET assembly providing access to cameras plugged in your Raspberry Pi.

This program is based on v4l API and some fswebcam source headers.

With RapberryCam, you can take pictures and videos from a webcam pluged in your Raspberry Pi.

For the moment, the video stream is not a MPEG stream, but just a succession of compressed frames.

Source code is here : https://bitbucket.org/rflechner/raspberrycam/wiki/Home

Or you can just install the NuGet RaspberryCam in your project. (cf. https://nuget.org/packages/RaspberryCam)

In a future version, a real MPEG format should be implemented.

Before your first usage

Just copy RaspberryCam.so into /Lib

Or :

Copy C sources files on your Rapsberry Pi and type:

$sudo make
$sudo make install

Try it

Run ServerExample on your Rapsberry Pi

mono RaspberryCam.ServerExample.exe

And run RaspberryCam.VideoViewer.exe on your PC

Take a simple picture from C# code:

var cameras = Cameras.DeclareDevice()  
  .Named("Camera 1")
  .WithDevicePath("/dev/video0")  
  .Memorize();

cameras.Get("Camera 1").SavePicture(new PictureSize(640, 480), "Test1.jpg", 20);

// Or

cameras.Default.SavePicture(new PictureSize(640, 480), "Test2.jpg", 20);

// Best practice: cameras should be a single instance in your program  

Host a video and pictures server in your application

var videoServer = new TcpVideoServer(8080, cameras);  
Console.WriteLine("Server strating ...");  
videoServer.Start();  
Console.WriteLine("Server strated.");  

Download pictures from your Raspberry Pi

var videoClient = new TcpVideoClient(serverHostIp, serverPort);  
videoClient.StartVideoStreaming(new PictureSize(imageWidth/2, imageHeight/2)); //Open hardware  
var data = videoClient.GetVideoFrame(compressionRate); //data contains a simple jpeg frame  
videoClient.StopVideoStreaming(); //Close Hardware

12 thoughts on “Use a webcam plugged in a Raspberry pi with Mono in C# using RaspberryCam

  1. Hi,
    first of all you did a great job on that, anyway if I try your example with my usb cam I get the following error:
    gd-jpeg: JPEG library reports unrecoverable error: Not a JPEG file: starts with 0x93 0x7b

    This occurs always, also when I want to save a picture etc. I tried a different Cam and it works so the environment is working properly. Any chance to get my cam working?
    Thx
    Mathias

  2. Hi,
    I installed the package via NuGet into my project, which basically consists of:

    Cameras cameras = Cameras.DeclareDevice()
    .Named(“Camera 1”).WithDevicePath(“/dev/video0”)
    .Memorize();

    cameras.Get(“Camera 1”).SavePicture(new PictureSize(640, 480), “/var/www/test.jpg”, 20);

    Then I downloaded the built files (my.exe, RaspberryCam.dll & RaspberryCam.so) to my Raspberry PI. I also copied the RaspberryCam.so file to /lib and even to /Lib.
    But whenever I try to execute my program with mono I get a DllNotFoundException for RaspberryCam.so.
    I also added my project path to /etc/ld.so.conf and ran ldconfig.
    Can anyone help me with this matter?

    Thanks
    Dominik

  3. Hello! thanks for this, I was looking for something easy to use. I’ll try yours soon. How can I test this in Windows first?

  4. Pingback: mono on raspberry pi throws DLLNotFoundException at DLLImport of shared object (.so) file | Solutions for enthusiast and professional programmers

  5. Hi there,

    i have an issue it seems the library is restricted to 640×480.

    PictureSiize.cs contains a min() statementa that ensures this restriction is kept. Is there anything you can do on the C++ side to make it available for full resolution still images?

    Thank you so much for your effort in this project

    Tim

Leave a Reply

Your email address will not be published. Required fields are marked *