navigator.getUserMedia(constraints, successCallback, errorCallback);
var canvas, context, video; // jQuery document ready event $(function () { canvas = document.getElementById("canvas"); context = canvas.getContext("2d"); video = document.getElementById("video"); // different browsers provide getUserMedia() in differnt ways, so we need to consolidate navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; if (navigator.getUserMedia) { navigator.getUserMedia( { video : true }, // which media function (stream) { // success function video.src = window.URL.createObjectURL(stream); video.onloadedmetadata = function (e) { video.play(); }; }, function (err) { // error function console.log("The following error occured: " + err.name); } ); } else { console.log("getUserMedia not supported"); } });
function takePhoto() { context.drawImage(video, 0, 0, 640, 480); // takes content from video control at this point and draws on Canvas }
function savePhoto() { var data = canvas.toDataURL(); // convert canvas contents to base64 format using png format var title = $("#title").val(); $.ajax({ type: "POST", url: "savephoto.aspx", data: { photo: data, // image title : title // title } }).done(function (o) { // success function alert("Photo Saved Successfully!"); }); }
<!DOCTYPE html> <html> <head> <script src="jquery.js"></script> <title>Sanp Photo</title> <script> var canvas, context, video, videoObj; $(function () { canvas = document.getElementById("canvas"); context = canvas.getContext("2d"); video = document.getElementById("video"); // different browsers provide getUserMedia() in differnt ways, so we need to consolidate navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; if (navigator.getUserMedia) { navigator.getUserMedia( { video : true }, // which media function (stream) { // success function video.src = window.URL.createObjectURL(stream); video.onloadedmetadata = function (e) { video.play(); }; }, function (err) { // error function console.log("The following error occured: " + err.name); } ); } else { console.log("getUserMedia not supported"); } }); function takePhoto() { context.drawImage(video, 0, 0, 640, 480); } function savePhoto() { var data = canvas.toDataURL(); var title = $("#title").val(); $.ajax({ type: "POST", url: "savephoto.aspx", data: { photo: data, title: title } }).done(function (o) { alert("Photo Saved Successfully!"); }); } </script> </head> <body> Enter Title : <input type="text" name="title" id="title" /> <button id="btnSnap" onclick="takePhoto()">Snap Photo</button> <button id="btnSave" onclick="savePhoto()">Save Photo</button> <a href="ListPhotos.aspx">ListPhotos.aspx</a> <p /> <video id="video" width="640" height="480" autoplay></video> <canvas style="float:right" id="canvas" width="640" height="480"></canvas> </body> </html>
data:image/png;base64,contents
<%@ Page Language="C#" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <!DOCTYPE html> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { try { SqlConnection con = new SqlConnection (@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"); string qry = "insert into Photos (title,photo) values(@title, @photo)"; SqlCommand cmd = new SqlCommand(qry, constr); // create Parameters cmd.Parameters.AddWithValue("@title", Request.Form["title"]); SqlParameter photoParam = new SqlParameter("@photo", SqlDbType.Image); String source = Request.Form["photo"]; // remove extra chars at the beginning source = source.Substring(source.IndexOf(",") + 1); byte[] data = Convert.FromBase64String(source); photoParam.Value = data; cmd.Parameters.Add(photoParam); //Open connection and execute insert query. con.Open(); cmd.ExecuteNonQuery(); con.Close(); } catch (Exception ex) { Trace.Write(ex.Message); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> </body> </html>
<%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <!DOCTYPE html> <script runat="server"> DataSet ds; protected void Page_Load(object sender, EventArgs e) { try { SqlConnection con = new SqlConnection (@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"); SqlDataAdapter da = new SqlDataAdapter("select * from photos",con); ds = new DataSet(); da.Fill(ds,"photos"); DataList1.DataSource = ds.Tables[0]; DataList1.DataBind(); } catch (Exception ex) { Trace.Write(ex.Message); } } protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { Image img = e.Item.FindControl("photo") as Image; DataRow row = ds.Tables[0].Rows[e.Item.ItemIndex]; byte [] bytes = (byte []) row["photo"]; string base64String = Convert.ToBase64String(bytes, 0, bytes.Length); img.ImageUrl = "data:image/png;base64," + base64String; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>List Of Photos</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound"> <ItemTemplate> <h2> <%# Eval("title") %></h2> <asp:Image ID="photo" runat="server" /> </ItemTemplate> </asp:DataList> </div> </form> </body> </html>