Capture Image and save into Database.
<%@ Page Language="C#" AutoEventWireup="true" Codefile="cisf_v.aspx.cs" Inherits="vstr_gt_ps_systm.cisf_v" %>
<script src="js/JavaScript1.js"></script>
<script src="js/WebCam.js"></script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body align="center" style="background-color: #FFFFFF; text-align: left;">
<form id="form1" runat="server">
<table border="1" class="auto-style43" >
<tr>
<td class="auto-style41"><img src="images/m1.png" class="auto-style42" /></td>
<th colspan="4" class="auto-style2" style="box-sizing: border-box;" ><span class="auto-style50">Meja Thermal Power Plant<br/>
</span>
<font style="box-sizing: border-box;">
<span class="auto-style36">VISITOR GATE PASS SYSTEM</span></font></th>
</tr>
<tr>
<td class="auto-style41">Emp. Name :</td>
<td class="auto-style53">
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</td>
<td class="auto-style49" colspan="2">
Emp. ID
:</td>
<td class="auto-style44">
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style41">Visitor ID:</td>
<td class="auto-style53">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
<td class="auto-style49" colspan="2">Visitor Name :</td>
<td class="auto-style44"> <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td class="auto-style41">Visitor Aadhar No. :</td>
<td class="auto-style53"> <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label></td>
<td class="auto-style49" colspan="2">Gender :</td>
<td class="auto-style44"><asp:Label ID="Label7" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td class="auto-style41">Father/Husband Name :</td>
<td class="auto-style53"> <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label></td>
<td class="auto-style49" colspan="2">Current Address :</td>
<td class="auto-style44"><asp:Label ID="Label8" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td class="auto-style41">Purpose of visit :</td>
<td class="auto-style7" colspan="4"> <asp:Label ID="Label9" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td class="auto-style41"></td>
<td class="auto-style48" colspan="2"><u>Live Camera</u></td>
<td class="auto-style52"><u>Captured Picture</u></td>
</tr>
<tr>
<td class="auto-style41"></td>
<td class="auto-style4" colspan="2">
<div id="webcam"> </div>
</td>
<td class="auto-style4" colspan="2">
<img id="imgCapture" /></td>
</tr>
<tr>
<td class="auto-style45" colspan="5">
<input id="btnCapture" type="button" value="Capture" />
<input id="btnUpload" disabled="disabled" type="button" value="Upload" />
<asp:Button ID="Button2" runat="server" Text="Back" OnClick="Button2_Click" CssClass="auto-style8" Height="26px" Width="67px" />
</td>
</tr>
</table>
</form>
</body>
</html>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="WebCam.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
Webcam.set({
width: 200,
height: 150,
image_format: 'jpeg',
jpeg_quality: 90
});
Webcam.attach('#webcam');
$("#btnCapture").click(function () {
Webcam.snap(function (data_uri) {
$("#imgCapture")[0].src = data_uri;
$("#btnUpload").removeAttr("disabled");
});
});
$("#btnUpload").click(function () {
$.ajax({
type: "POST",
url: "cisf_v.aspx/SaveCapturedImage",
data: "{data: '" + $("#imgCapture")[0].src + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) { }
});
});
});
</script>
namespace vstr_gt_ps_systm
{
public partial class cisf_v : System.Web.UI.Page
{
[WebMethod()]
public static bool SaveCapturedImage(string data)
{
string fileName = DateTime.Now.ToString("dd-MM-yy hh-mm-ss");
//Convert Base64 Encoded string to Byte Array.
byte[] imageBytes = Convert.FromBase64String(data.Split(',')[1]);
//Save the Byte Array as Image File.
string filePath = HttpContext.Current.Server.MapPath(string.Format("~/Captures/{0}.jpg", fileName));
File.WriteAllBytes(filePath, imageBytes);
return true;
}
int empno = 0;
protected void Page_Load(object sender, EventArgs e)
{
empno = Convert.ToInt32(Request.QueryString["Id"].ToString());
Session["EmpId"] = empno;
if (!IsPostBack)
{
BindTextBoxvalues();
}
}
private void BindTextBoxvalues()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("select * from vstr where Id=" + empno, con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
Label1.Text = dt.Rows[0][0].ToString();
Label3.Text = dt.Rows[0][1].ToString();
Label2.Text = dt.Rows[0][2].ToString();
Label4.Text = dt.Rows[0][3].ToString();
Label5.Text = dt.Rows[0][4].ToString();
Label6.Text = dt.Rows[0][5].ToString();
Label7.Text = dt.Rows[0][6].ToString();
Label8.Text = dt.Rows[0][7].ToString();
Label9.Text = dt.Rows[0][8].ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("cisf.aspx");
}
}
}