Capture des images en utilisant WebCamera dans ASP.NET 4.5
Code Default.aspx
<%@ Page Title="Page d'accueil" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="AspNetCamera._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="Scripts/jquery.webcam.js" type="text/javascript"></script>
<script>
function CreateCamera() {
$("#Camera").webcam({
width: 500,
height: 300,
mode: "save",
swffile: "Scripts/jscam.swf",
onTick: function () { },
onSave: function () {
},
onCapture: function () {
webcam.save('/Capture.aspx'); ;
},
debug: function () { },
onLoad: function () { }
});
}
</script>
<link href="Styles/Camera.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Bienvenue!
</h2>
<br />
<br />
<div>
<input type="button" value="Afficher Caméra !" onclick="CreateCamera();" />  
<input type="button" value="Capturer!" onclick="webcam.capture();" />
<div id="Camera"></div>
</div>
</asp:Content>
Code pour l'enregistrement de l'image:
Capture.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace AspNetCamera
{
public partial class Capture : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var stream = Request.InputStream;
string dump;
using (var reader = new StreamReader(stream))
dump = reader.ReadToEnd();
string nomphoto = DateTime.Now.Year + "" + DateTime.Now.Month +"" + DateTime.Now.Day + "" + DateTime.Now.Minute;
var path = Server.MapPath("~/Captures/" + nomphoto + ".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;
}
}
}
Télécharger le code source complet
Code Default.aspx
<%@ Page Title="Page d'accueil" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="AspNetCamera._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="Scripts/jquery.webcam.js" type="text/javascript"></script>
<script>
function CreateCamera() {
$("#Camera").webcam({
width: 500,
height: 300,
mode: "save",
swffile: "Scripts/jscam.swf",
onTick: function () { },
onSave: function () {
},
onCapture: function () {
webcam.save('/Capture.aspx'); ;
},
debug: function () { },
onLoad: function () { }
});
}
</script>
<link href="Styles/Camera.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Bienvenue!
</h2>
<br />
<br />
<div>
<input type="button" value="Afficher Caméra !" onclick="CreateCamera();" />  
<input type="button" value="Capturer!" onclick="webcam.capture();" />
<div id="Camera"></div>
</div>
</asp:Content>
Code pour l'enregistrement de l'image:
Capture.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace AspNetCamera
{
public partial class Capture : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var stream = Request.InputStream;
string dump;
using (var reader = new StreamReader(stream))
dump = reader.ReadToEnd();
string nomphoto = DateTime.Now.Year + "" + DateTime.Now.Month +"" + DateTime.Now.Day + "" + DateTime.Now.Minute;
var path = Server.MapPath("~/Captures/" + nomphoto + ".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;
}
}
}
Télécharger le code source complet
Aucun commentaire:
Enregistrer un commentaire