HTML5 拍照与上传图片

 
 
<% // 前端: Default . aspx %>
<%@ Page Language = "C#" AutoEventWireup = "true" CodeFile = "Default.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 runat= "server" >
<title></title>
<script src= "js/jquery.min.js" type= "text/javascript" ></script>
<script type= "text/javascript" >
// 本地视频流
var localMediaStream = null ;
$ ( function () {
// 视频显示元素
var video = document . getElementById ( 'video');
// 画布
var canvas = document . getElementById ( 'canvas');
// 获取一个 CanvasRenderingContext2D 对象,类型为 2d
var ctx = canvas . getContext ( '2d');
// 开启摄像头
var myGetUserMedia = navigator . getUserMedia ||
navigator . webkitGetUserMedia ||
navigator . mozGetUserMedia ||
navigator . msGetUserMedia ;
$ ( "#txtMsg" ). val ( '开启摄像头\r\n\r\n' + $("#txtMsg").val());
navigator . webkitGetUserMedia (
{ video : true },
function ( stream ) {
$ ( "#txtMsg" ). val ( '摄像头开启完毕\r\n\r\n' + $("#txtMsg").val());
video . src = window . URL . createObjectURL ( stream ) ;
localMediaStream = stream ;
// 开始播放
setTimeout ( function () {
canvas . width = video . videoWidth ;
canvas . height = video . videoHeight ;
// $ ( "#img" ). css ( "width" , video . videoWidth ) ;
// $ ( "#img" ). css ( "height" , video . videoHeight ) ;
}, 100 ) ;
},
function () {
$ ( "#txtMsg" ). val ( '无法开启视频采集设备\r\n\r\n' + $("#txtMsg").val());
}
) ;
// 拍照
$ ( "#btnCapture" ). click ( function () {
// 若视频流畅通,则执行以下代码,将视频上的图像绘制到画布并通过 img 显示
if ( localMediaStream ) {
// video 作为图像源源,在 canvas 上绘制图像
ctx . drawImage ( video , 0 , 0 ) ;
// 将绘制的图像显示到 img 标签上
$ ( "#img" ). attr ( "src" , canvas . toDataURL ( 'image/png'));
}
}) ;
// 上传
$ ( "#btnUpload" ). click ( function () {
$ ( "#txtMsg" ). val ( '开始上传\r\n\r\n' + $("#txtMsg").val());
$ . post ( "Default.aspx?action=UploadImg"
, { imgDataUrl : $ ( "#img" ). attr ( "src" ) }
, function ( data ) {
if ( data == "success" ) {
$ ( "#txtMsg" ). val ( '上传完毕\r\n\r\n' + $("#txtMsg").val());
}
}
) ;
}) ;
}) ;
</script>
</head>
<body>
<form id= "form1" runat= "server" >
<table>
<tr>
<td colspan= "2" >
<input id= "btnCapture" type= "button" value= "拍照" />
<input id= "btnUpload" type= "button" value= "上传" />
</td>
</tr>
<tr>
<td><video id= "video" width= "350px" height= "300px" autoplay ></video></td>
<td><img id= "img" src= "" style= "border:1px solid red;width:400px;height:300px" /></td>
</tr>
<tr>
<td colspan= "2" >
<textarea id= "txtMsg" style= "width:100%;height:400px" >
getUserMedia
webkitGetUserMedia
mozGetUserMedia
msGetUserMedia
</textarea>
</td>
</tr>
</table>
<canvas id= "canvas" style= "display:none" ></canvas>
</form>
</body>
</html>
<% // 后台: Default . 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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["action"] == "UploadImg")
{
string imgDataUrl = Request["imgDataUrl"];
string imgData = imgDataUrl.Substring(22);
File.WriteAllBytes(MapPath("Upload/" + Guid.NewGuid().ToString() + ".png"), Convert.FromBase64String(imgData));
Response.Write("success");
Response.End();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值