(C# code snippet) How to create USB web camera viewer and stream to remote locations
Join the DZone community and get the full member experience.
Join For Freein this brief tutorial you will learn how to develop a camera viewer application in c# that allows you to display the image of your usb webcam and to stream the camera image to remote pcs and smartphones. instead of presenting a long article, i would rather show how to implement such application with a few lines of c# code by using the prewritten components of a c# camera library.
prerequisites
-
a
visual c# wpf application created in visual studio
-
the
voipsdk.dll added to the references. (it can be found on the official website
of this
c# camera library
.)
-
a
media player supporting rtsp streaming (e.g. vlc) installed on a remote pc
first of all let’s build the gui. if you follow the content of the mainwindow.xaml file line-by-line, you will see how to create user all the necessary gui elements that allows the user to be able to connect to a usb camera and display its image, and to set the listen address (including 2 textboxes for the ip address and the port number) that makes rtsp streaming possible. (the following figure illustrates the gui that can be created by using this code snippet.) in the mainwindow.xaml.cs file you will see how to implement the camera viewer functionality and how to turn your application as a video server.
to test your application run the program, click the connect button, then when the camera image is displayed, enter the ipv4 address of your pc as listening address, and specify ’554’ as a port number. thereafter open the vlc media player on an other pc or smartphone, and open the network media stream by entering the following network url: rtsp://192.168.115.1:554 (that is: rtsp://youripv4address/portnumber). the result can be seen below:
i hope my code snippet was useful! happy programming!
// mainwindow.xaml
// mainwindow.xaml.cs
using system;
using system.collections.generic;
using system.linq;
using system.runtime.interopservices;
using system.text;
using system.threading.tasks;
using system.windows;
using system.windows.controls;
using system.windows.data;
using system.windows.documents;
using system.windows.input;
using system.windows.media;
using system.windows.media.imaging;
using system.windows.navigation;
using system.windows.shapes;
using ozeki.media.ipcamera;
using ozeki.media.mediahandlers;
using ozeki.media.mediahandlers.video;
using ozeki.media.mjpegstreaming;
using ozeki.media.video.controls;
namespace basic_cameraviewer
{
///
/// interaction logic for mainwindow.xaml
///
public partial class mainwindow : window
{
private videoviewerwpf _videoviewerwpf;
private bitmapsourceprovider _provider;
private iipcamera _ipcamera;
private webcamera _webcamera;
private mediaconnector _connector;
private myserver _server;
private ivideosender _videosender;
public mainwindow()
{
initializecomponent();
_connector = new mediaconnector();
_provider = new bitmapsourceprovider();
_server = new myserver();
setvideoviewer();
}
private void setvideoviewer()
{
_videoviewerwpf = new videoviewerwpf
{
horizontalalignment = horizontalalignment.stretch,
verticalalignment = verticalalignment.stretch,
background = brushes.black
};
camerabox.children.add(_videoviewerwpf);
_videoviewerwpf.setimageprovider(_provider);
}
#region usb camera connect/disconnect
private void connectusbcamera_click(object sender, routedeventargs e)
{
_webcamera = webcamera.getdefaultdevice();
if (_webcamera == null) return;
_connector.connect(_webcamera, _provider);
_videosender = _webcamera;
_webcamera.start();
_videoviewerwpf.start();
}
private void disconnectusbcamera_click(object sender, routedeventargs e)
{
if (_webcamera == null) return;
_videoviewerwpf.stop();
_webcamera.stop();
_webcamera.dispose();
_connector.disconnect(_webcamera, _provider);
}
#endregion
private void guithread(action action)
{
dispatcher.begininvoke(action);
}
private void startserver_click(object sender, routedeventargs e)
{
var ipadress = ipaddresstext.text;
var port = int.parse(porttext.text);
_server.videosender = _videosender;
_server.onclientcountchanged += server_onclientcountchanged;
_server.start();
_server.setlistenaddress(ipadress, port);
}
void server_onclientcountchanged(object sender, eventargs e)
{
guithread(() =>
{
connectedclientlist.items.clear();
foreach (var client in _server.connectedclients)
connectedclientlist.items.add("end point: " +
client.transportinfo.remoteendpoint);
});
}
private void stopserver_click(object sender, routedeventargs e)
{
_server.onclientcountchanged -= server_onclientcountchanged;
_server.stop();
}
}
}
Opinions expressed by DZone contributors are their own.
Comments