- <!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>
posted @
2017-12-07 16:25
极速代码
阅读(
2677)
评论()
编辑
收藏
举报
点击右上角即可分享
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗