Good afternoon everyone,
I found an article from the internet where you can take a photo from the webcam. I followed all the steps but did not succeed. Below is the snippet code where I got a false response:
- if (Request.InputStream.Length > 0)
- {
- using (StreamReader reader = new StreamReader(Request.InputStream))
- {
- string hexString = Server.UrlEncode(reader.ReadToEnd());
- string imageName = DateTime.Now.ToString("dd-MM-yy hh-mm-ss");
- string imagePath = string.Format("~/Captures/{0}.png", imageName);
- File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString));
- Session["CapturedImage"] = ResolveUrl(imagePath);
- }
- }
Below are the full code of this article:
- 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;
- }
and this is the mark-up:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="_Default" %>
-
- <!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 id="Head1" runat="server">
- <title></title>
- <style type="text/css">
- body
- {
- font-family: Arial;
- font-size: 10pt;
- }
- </style>
- </head>
- <body>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
- <script src='<%=ResolveUrl("~/Webcam_Plugin/jquery.webcam.js") %>' type="text/javascript"></script>
- <script type="text/javascript">
- var pageUrl = '<%=ResolveUrl("~/CS.aspx") %>';
- $(function () {
- jQuery("#webcam").webcam({
- width: 320,
- height: 240,
- mode: "save",
- swffile: '<%=ResolveUrl("~/Webcam_Plugin/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>
-
- </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>
Thanks for any help.