HOW TO USE WEBCAM OR LAPTOP FRONT CAMERA TO CAPTURE IN ASP.NET AND C#

in programming •  5 years ago 


HOW TO USE WEBCAM OR LAPTOP FRONT CAMERA TO CAPTURE IN ASP.NET AND C#


HERE ARE THE STEP BELOW



STEP 1 :-DOWNLOAD JQUERY WEBCAM PLUGIN

http://www.xarg.org/project/jquery-webcam-plugin/


STEP 2:-

ADD DOWNLOADED FILE IN YOUR PROJECT AND CREATE CAPTURES FOLDER

WERE CAPTURE IMAGE WILL BE STORE


STEP 3 :-CREATE WEBFORM PAGE

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Camera.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script src='<%=ResolveUrl("~/jQuery-webcam-master/jquery.webcam.js") %>' type="text/javascript"></script>

<script type="text/javascript">

var pageUrl = '<%=ResolveUrl("~/WebForm1.aspx") %>';

$(function () {

    jQuery("#webcam").webcam({

        width: 320,

        height: 240,

        mode: "save",

        swffile: '<%=ResolveUrl("~/jQuery-webcam-master/jscam.swf") %>',

        debug: function (type, status) {

            $('#camStatus').append(type + ": " + status + '<br /><br />');

        },

        onSave: function (data) {

            $.ajax({

                type: "POST",

                url: pageUrl + "/GetCapturedImage",

                data: '',

                contentType: "application/json; charset=utf-8",

                dataType: "json",

                success: function (r) {

                    $("[id*=imgCapture]").css("visibility", "visible");

                    $("[id*=imgCapture]").attr("src", r.d);

                },

                failure: function (response) {

                    alert(response.d);

                }

            });

        },

        onCapture: function () {

            webcam.save(pageUrl);

        }

    });

});

function Capture() {

    webcam.capture();

    return false;

}

</script>

<form id="form1" runat="server">

<table border="0" cellpadding="0" cellspacing="0">

    <tr>

        <td align="center">

            <u>Live Camera</u>

        </td>

        <td>

        </td>

        <td align="center">

            <u>Captured Picture</u>

        </td>

    </tr>

    <tr>

        <td>

            <div id="webcam">

            </div>

        </td>

        <td>

            &nbsp;

        </td>

        <td>

            <asp:Image ID="imgCapture" runat="server" Style="visibility: hidden; width: 320px;

                height: 240px" />

        </td>

    </tr>

</table>

<br />

<asp:Button ID="btnCapture" Text="Capture" runat="server" OnClientClick="return Capture();" />

<br />

<span id="camStatus"></span>

</form>

</body>

</html>


STEP 4:- C#

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.IO;

using System.Web.Services;


namespace Camera

{

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!this.IsPostBack)

{

if (Request.InputStream.Length > 0)

{

using (StreamReader reader = new StreamReader(Request.InputStream))

{

string hexString = Server.UrlEncode(reader.ReadToEnd());

string imageName = "test" + System.DateTime.Now.ToString("HHmmss");

string imagePath = string.Format("~/Captures/{0}.jpg", imageName);

File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString));

Session["CapturedImage"] = ResolveUrl(imagePath);

}

}

}

}


private static byte[] ConvertHexToBytes(string hex)

{

byte[] bytes = new byte[hex.Length / 2];

for (int i = 0; i < hex.Length; i += 2)

{

bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);

}

return bytes;

}


[WebMethod(EnableSession = true)]

public static string GetCapturedImage()

{

string url = HttpContext.Current.Session["CapturedImage"].ToString();

HttpContext.Current.Session["CapturedImage"] = null;

return url;

}

}

}


If You Haven't Subscribers My Channel Than Please Subscribers It And Even Share It Link Of My Channnel :- https://www.youtube.com/channel/UCdIDIGUh3rL9A_Yt_tbZmkg Please Go And Subscribers It

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!