對 HTML5 視頻進行截圖

jopen 11年前發布 | 65K 次閱讀 HTML5 前端技術

一個簡單的例子展示了如何從視頻中捕獲靜止圖像。

假設你有下面的HTML代碼:

<video id="video" controls="controls">
    <source src=".mp4" />
</video>

<button id="capture">Capture</button>

<div id="output"></div>

然后,當用戶單擊“捕獲”按鈕,我們將當前的視頻內容寫入到一個畫布,并使用在畫布上顯示圖像。

(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);           

}());

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!