安卓手机利用html5 ,video+canvas从视频流里面截图拍照
2017-05-16 10:19
1066 查看
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>HTML5 GetUserMedia Demo</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> </head> <body> <input type="button" title="开启摄像头" value="开启摄像头" onclick="getMedia();" /><br /> <video height="120px" autoplay="autoplay"></video><hr /> <input type="button" title="拍照" value="拍照" onclick="getPhoto();" /><br /> <canvas id="canvas1" height="120px" ></canvas><hr /> <input type="button" title="视频" value="视频" onclick="getVedio();" /><br /> <canvas id="canvas2" height="120px"></canvas> <script type="text/javascript"> var video = document.querySelector('video'); var audio, audioType; var canvas1 = document.getElementById('canvas1'); var context1 = canvas1.getContext('2d'); var canvas2 = document.getElementById('canvas2'); var context2 = canvas2.getContext('2d'); navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL; var exArray = []; //存储设备源ID MediaStreamTrack.getSources(function (sourceInfos) { for (var i = 0; i != sourceInfos.length; ++i) { var sourceInfo = sourceInfos[i]; //这里会遍历audio,video,所以要加以区分 if (sourceInfo.kind === 'video') { exArray.push(sourceInfo.id); } } }); function getMedia() { if (navigator.getUserMedia) { navigator.getUserMedia({ 'video': { 'optional': [{ 'sourceId': exArray[1] //0为前置摄像头,1为后置 }] }, 'audio':true }, successFunc, errorFunc); //success是获取成功的回调函数 } else { alert('Native device media streaming (getUserMedia) not supported in this browser.'); } } function successFunc(stream) { //alert('Succeed to get media!'); if (video.mozSrcObject !== undefined) { //Firefox中,video.mozSrcObject最初为null,而不是未定义的,我们可以靠这个来检测Firefox的支持 video.mozSrcObject = stream; } else { video.src = window.URL && window.URL.createObjectURL(stream) || stream; } //video.play(); // 音频 audio = new Audio(); audioType = getAudioType(audio); if (audioType) { audio.src = 'polaroid.' + audioType; audio.play(); } } function errorFunc(e) { alert('Error!'+e); } // 将视频帧绘制到Canvas对象上,Canvas每60ms切换帧,形成肉眼视频效果 function drawVideoAtCanvas(video,context) { window.setInterval(function () { context.drawImage(video, 0, 0,90,120); }, 60); } //获取音频格式 function getAudioType(element) { if (element.canPlayType) { if (element.canPlayType('audio/mp4; codecs="mp4a.40.5"') !== '') { return ('aac'); } else if (element.canPlayType('audio/ogg; codecs="vorbis"') !== '') { return ("ogg"); } } return false; } // vedio播放时触发,绘制vedio帧图像到canvas // video.addEventListener('play', function () { // drawVideoAtCanvas(video, context2); // }, false); //拍照 function getPhoto() { context1.drawImage(video, 0, 0,90,120); //将video对象内指定的区域捕捉绘制到画布上指定的区域,实现拍照。 } //视频 function getVedio() { drawVideoAtCanvas(video, context2); } </script> </body> </html>
经测试,支持pc+安卓手机,ios不支持这个方式。
相关文章推荐
- HTML5 Video/Canvas拍照初级
- HTML5调用手机前置摄像头或后置摄像头拍照,canvas显示,经过Android测试
- 利用HTML5的Video进行视频截图并保存到本地
- PHP 使用 微信JSSDK 拍照选择图片接口 利用localid预览时 安卓手机正常显示 iOS手机不显示
- node.js 和 HTML5-Canvas 结合实现截图上传交互
- 利用html5 canvas破解简单验证码及getImageData接口应用
- 安卓实现拍照、在手机中选择图片通过webservice上传图片到服务器
- HTML5 2D Canvas手机游戏开发经验谈
- HTML5 利用Canvas API 组合图形 推荐
- HTML5 Canvas实现平移/放缩/旋转deom示例(附截图)
- html5 video 手机上播放和下载的问题
- 一段很棒的利用html5-canvas及javascript产生三维星空效果的代码!
- javascript和HTML5利用canvas构建猜牌游戏实现算法
- 利用HTML5的一个重要特性—DeviceOrientation来实现手机网站上的摇一摇功能(转)
- 怎样将html5中利用canvas绘制的图像在服务端保存为图片?
- 问题:Flex4的手机项目里面不支持VideoDisplay组件,视频怎样播放?
- android 手机拍照上传项目的开发-----JAVA利用socket传图片给linux服务器
- HTML5 2D Canvas手机游戏开发经验谈
- 如何使用HTML5实现利用摄像头拍照上传功能
- 利用HTML5的一个重要特性 —— DeviceOrientation来实现手机网站上的摇一摇功能