Using jQuery webcam plugin with ASP.NET MVC

I have to use webcam images in one of applications I build and when playing with different components I found free and easy to use Flash and jQuery based webcam component called jQuery webcam plugin. In this posting I will show you how to use jQuery webcam plugin with ASP.NET MVC application to save captured image to server hard disc.

Preparation

Here are some steps to take before writing any ASP.NET code:

  1. Create new ASP.NET MVC application.
  2. Download jQuery webcam plugin and extract it.
  3. Put jquery.webcam.js, jscam.swf and jscam_canvas_only.swf files to Scripts folder of web application.

Now we are ready to go.

Create webcam page

We start with creating default page of web application. I’m using Index view of Home controller.

@{
    ViewBag.Title =
"Index"
;
}
@section scripts
{

   
<script src="@Url.Content("~/Scripts/jquery.webcam.js")">
    </script>
    <script>
        $("#Camera"
).webcam({
             width: 320,
             height: 240,
             mode:
"save"
,
             swffile:
"@Url.Content("~/Scripts/jscam.swf")"
,
             onTick:
function
() { },
             onSave:
function
() {
             },
             onCapture:
function
() {
                 webcam.save(
"@Url.Content("~/Home/Capture")/"
);
             },
             debug:
function
() { },
             onLoad:
function
() { }
         });
    
</script>
}
<h2>Index</h2
>
<
input type="button" value="Shoot!" onclick="webcam.capture();" 
/>
<
div id="Camera"></div
>

We initialize webcam plugin in additional scripts block offered by layout view. To send webcam capture to server we have to use webcam plugin in save mode. onCapture event is the one where we actually give command to send captured image to server. Button with value “Shoot!” is the one we click at right moment.

Saving image to server hard disk

Now let’s save captured image to server hard disk. We add new action called Capture to Home controller. This action reads image from input stream, converts it from hex dump to byte array and then saves the result to disk.

Credits for String_To_Bytes2() method that I quickly borrowed go to Kenneth Scott and his blog posting Convert Hex String to Byte Array and Vice-Versa.

public class HomeController : Controller
{
   
public ActionResult
Index()
    {
       
return
View();
    }

   
public void
Capture()
    {
       
var
stream = Request.InputStream;
       
string
dump;

       
using (var reader = new StreamReader
(stream))
            dump = reader.ReadToEnd();

       
var path = Server.MapPath("~/test.jpg"
);
        System.IO.
File
.WriteAllBytes(path, String_To_Bytes2(dump));
    }

   
private byte[] String_To_Bytes2(string
strInput)
    {
       
int
numBytes = (strInput.Length) / 2;
       
byte[] bytes = new byte
[numBytes];

       
for (int
x = 0; x < numBytes; ++x)
        {
            bytes[x] =
Convert
.ToByte(strInput.Substring(x * 2, 2), 16);
        }

       
return bytes;
    }
}

Before running the code make sure you can write files to disk. Otherwise nasty access denied errors will come.

Testing application

Now let’s run the application and see what happens.

You need Flash to make webcam work

Whoops… we have to give permission to use webcam and microphone to Flash before we can use webcam. Okay, it is for our security.

After clicking Allow I was able to see picture that was forgot to protect with security message.

And webcam works ...

This tired hacker in dark room is actually me, so it seems like JQuery webcam plugin works okay :)

Conclusion

jQuery webcam plugin is simple and easy to use plugin that brings basic webcam functionalities to your web application. It was pretty easy to get it working and to get image from webcam to server hard disk. On ASP.NET side we needed simple hex dump conversion to make hex dump sent by webcam plugin to byte array before saving it as JPG-file.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    8 thoughts on “Using jQuery webcam plugin with ASP.NET MVC

    • May 16, 2014 at 4:50 am
      Permalink

      Dear admin,

      its working fine for all types of camera but not in smi grabber type , kindly help on this ..

      thanks in advance,
      Jayakumar

    • March 22, 2016 at 2:44 pm
      Permalink

      Dear Admin

      How to set the rear camera setting to take the picture.

      Thanks in advance
      Vishal

    • March 27, 2016 at 1:19 am
      Permalink

      You can set what Flash allows you to set. You can’t switch between front and rear camera through web page AFAIK.

    • November 4, 2016 at 9:23 am
      Permalink

      My self Tapas, from India.I did it. It is working. But in my lenovo Tablet It is not working. is there any solution please tell me as soon as possible.

    • November 6, 2016 at 9:48 pm
      Permalink

      This plugin uses Flash and your browser must have Flash installed and allowed. If this condition is met but it still doesn’t work then please see if browser’s developers tools show any console output or exceptions.

    • May 31, 2017 at 8:31 am
      Permalink

      Hi Gunnar have you tried to do it in .Net Core?

      It’s not possible to use Request.InputStream. Can you help me with it?

    • May 31, 2017 at 9:02 am
      Permalink

      ASP.NET Core has Request.Body property that is stream you can read.

    • February 17, 2024 at 1:48 pm
      Permalink

      this will work in 2024?

    Leave a Reply

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