一個(gè)簡(jiǎn)單的例子展示了如何從視頻中捕獲靜止圖像。下面來看一下
假設(shè)你有下面的HTML代碼:
<video id="video" controls="controls"> <source src=".mp4" /> </video> <button id="capture">Capture</button> <p id="output"></p>
然后,當(dāng)用戶單擊“捕獲”按鈕,我們將當(dāng)前的視頻內(nèi)容寫入到一個(gè)畫布,并使用在畫布上顯示圖像。
(function() { "using strict"; var $video, $output; var scale = 0.25; var initialize = function() { $output = $("#output"); $video = $("#video").get(0); $("#capture").click(captureImage); }; var captureImage = function() { var canvas = document.createElement("canvas"); canvas.width = $video.videoWidth * scale; canvas.height = $video.videoHeight * scale; canvas.getContext('2d') .drawImage($video, 0, 0, canvas.width, canvas.height); var img = document.createElement("img"); img.src = canvas.toDataURL(); $output.prepend(img); }; $(initialize); }());
【相關(guān)推薦】
1. 免費(fèi)h5在線視頻教程
2. HTML5 完整版手冊(cè)
3. php.cn原創(chuàng)html5視頻教程