DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • All Things ASP.NET Core and MVC: Tutorials and Articles
  • Add Watermark Text to Images in ASP.NET MVC
  • OAuth Implicit flow Using Angular 6 and ASP.NET MVC Core
  • Identify ASP.NET MVC Assembly Version

Trending

  • The Agile Architect: Mastering Architectural Observability To Slay Technical Debt
  • How To Learn Secure Software Development Lifecycle (SDLC)
  • Parallel Sort
  • Unleashing the Power of Microservices With Spring Cloud
  1. DZone
  2. Coding
  3. Frameworks
  4. Using jQuery Webcam Plugin With ASP.NET MVC

Using jQuery Webcam Plugin With ASP.NET MVC

Gunnar Peipman user avatar by
Gunnar Peipman
·
Feb. 10, 13 · Interview
Like (1)
Save
Tweet
Share
30.68K Views

Join the DZone community and get the full member experience.

Join For Free


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.

jQuery webcam plugin: Flash needs permissions to use webcam

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.

jQuery webcam plugin: Me in webcam

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.




ASP.NET MVC ASP.NET JQuery

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • All Things ASP.NET Core and MVC: Tutorials and Articles
  • Add Watermark Text to Images in ASP.NET MVC
  • OAuth Implicit flow Using Angular 6 and ASP.NET MVC Core
  • Identify ASP.NET MVC Assembly Version

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: