2017-03-09 107 views
0

如何從視頻裁剪畫布並保存爲圖像? 即時通訊使用此如何從視頻裁剪畫布並保存爲圖像?

function capture(){ 
var canvas = document.getElementById('canvas'); 
var video = document.getElementById('video'); 
canvas.getContext('2d').drawImage(video, 0, 0, video.videoWidth, video.videoHeight); } 

和HTML是

<v ideo id="video" src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4" controls></video><br/> 

<button onclick="capture()">Capture</button> <br/><br/> 
<canvas class="input" id="canvas"></canvas> <br/><br/> 

它得到一個形象的畫布。但現在如何保存爲圖像?我試着用Filesaver.js ,但我不知道如何保存爲圖像從畫布與此代碼。

var canvas = document.getElementById("my-canvas"), ctx == canvas.getContext("2d"); 
// draw to canvas... 
canvas.toBlob(function(blob) { 
saveAs(blob, "pretty image.png"); 
}); 

回答

0

寫在下載的單擊事件下面的代碼:

this.href = canvas.toDataURL('image/jpeg'); 
this.download = 'pretty_image.jpeg'; 
+0

要阿希什...謝謝你「偷」我的(批准)的第2次修訂建議編輯...你進行回滾它們(在修訂版本3中),然後在修訂版本4中,您編輯了您的答案...通過重新使用我建議編輯的95%...這是***不是很好,這也是我爲什麼標記你的答案...我可以讓我的「+2」代表點恢復嗎? PS to @Bhargav:重新回滾的merci,太糟糕了,它不會讓我再次回到+2 ...哦,好吧,就這樣吧... –