Jes Sie

Jes Sie

  • 742
  • 1.2k
  • 280.1k

Taking Photo From a Web Cam in Asp.Net C#

Nov 20 2019 12:54 AM
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:
 
  1. if (Request.InputStream.Length > 0)  
  2.             {  
  3.                 using (StreamReader reader = new StreamReader(Request.InputStream))  
  4.                 {  
  5.                     string hexString = Server.UrlEncode(reader.ReadToEnd());  
  6.                     string imageName = DateTime.Now.ToString("dd-MM-yy hh-mm-ss");  
  7.                     string imagePath = string.Format("~/Captures/{0}.png", imageName);  
  8.                     File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString));  
  9.                     Session["CapturedImage"] = ResolveUrl(imagePath);  
  10.                 }  
  11.             }  
 Below are the full code of this article:
  1. private static byte[] ConvertHexToBytes(string hex)  
  2.     {  
  3.         byte[] bytes = new byte[hex.Length / 2];  
  4.         for (int i = 0; i < hex.Length; i += 2)  
  5.         {  
  6.             bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);  
  7.         }  
  8.         return bytes;  
  9.     }  
  10.   
  11.     [WebMethod(EnableSession = true)]  
  12.     public static string GetCapturedImage()  
  13.     {  
  14.         string url = HttpContext.Current.Session["CapturedImage"].ToString();  
  15.         HttpContext.Current.Session["CapturedImage"] = null;  
  16.         return url;  
  17.     }  
 and this is the mark-up:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head id="Head1" runat="server">  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         body  
  9.         {  
  10.             font-family: Arial;  
  11.             font-size: 10pt;  
  12.         }  
  13.     </style>  
  14. </head>  
  15. <body>  
  16. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  
  17. <script src='<%=ResolveUrl("~/Webcam_Plugin/jquery.webcam.js") %>' type="text/javascript"></script>  
  18. <script type="text/javascript">  
  19. var pageUrl = '<%=ResolveUrl("~/CS.aspx") %>';  
  20. $(function () {  
  21.     jQuery("#webcam").webcam({  
  22.         width: 320,  
  23.         height: 240,  
  24.         mode: "save",  
  25.         swffile: '<%=ResolveUrl("~/Webcam_Plugin/jscam.swf") %>',  
  26.         debug: function (type, status) {  
  27.             $('#camStatus').append(type + ": " + status + '<br /><br />');  
  28.         },  
  29.         onSave: function (data) {  
  30.             $.ajax({  
  31.                 type: "POST",  
  32.                 url: pageUrl + "/GetCapturedImage",  
  33.                 data: '',  
  34.                 contentType: "application/json; charset=utf-8",  
  35.                 dataType: "json",  
  36.                 success: function (r) {  
  37.                     $("[id*=imgCapture]").css("visibility", "visible");  
  38.                     $("[id*=imgCapture]").attr("src", r.d);  
  39.                 },  
  40.                 failure: function (response) {  
  41.                     alert(response.d);  
  42.                 }  
  43.             });  
  44.         },  
  45.         onCapture: function () {  
  46.             webcam.save(pageUrl);  
  47.         }  
  48.     });  
  49. });  
  50. function Capture() {  
  51.     webcam.capture();  
  52.     return false;  
  53. }  
  54. </script>  
  55. <form id="form1" runat="server">  
  56. <table border="0" cellpadding="0" cellspacing="0">  
  57.     <tr>  
  58.         <td align="center">  
  59.             <u>Live Camera</u>  
  60.         </td>  
  61.         <td>  
  62.         </td>  
  63.         <td align="center">  
  64.             <u>Captured Picture</u>  
  65.         </td>  
  66.     </tr>  
  67.     <tr>  
  68.         <td>  
  69.             <div id="webcam">  
  70.             </div>  
  71.         </td>  
  72.         <td>  
  73.                
  74.         </td>  
  75.         <td>  
  76.             <asp:Image ID="imgCapture" runat="server" Style="visibility: hidden; width: 320px;  
  77.                 height: 240px" />  
  78.         </td>  
  79.     </tr>  
  80. </table>  
  81. <br />  
  82. <asp:Button ID="btnCapture" Text="Capture" runat="server" OnClientClick="return Capture();" />  
  83. <br />  
  84. <span id="camStatus"></span>  
  85. </form>  
  86. </body>  
  87. </html>  
 Thanks for any help.

Answers (1)