在你调用 takeLocalSnapshot / takeRemoteSnapshot截取本地/远端视频画面后,你通常需要对返回的 ImageData
进行处理,将其转换成 Blob、JPEG 等格式供后续使用,你可以参考以下最佳实践来实现。
// 从提取ImageData对象 var imageData = await engine.takeLocalSnapshot(StreamIndex.STREAM_INDEX_MAIN); // 创建Canvas元素并获取上下文 var canvas = document.createElement('canvas'); var context = canvas.getContext('2d'); canvas.width = imageData.width; canvas.height = imageData.height; context.putImageData(imageData, 0, 0);
// canvas 转 Blob canvas.toBlob((blob) => { // 获取 Blob 并进行后续操作 const file = new File([blob], "view.png", { type: "image/png" }); const imgUrl = URL.createObjectURL(file); const imgElement = document.createElement('img'); imgElement.src = imgUrl; document.body.appendChild(imgElement); });
// canvas 转 JPEG const url = canvas.toDataURL('image/jpeg'); const a = document.createElement('a'); a.href = url; a.download = '截图-' + Date.now(); a.click();
// canvas 转 PNG const url = canvas.toDataURL('image/png'); const a = document.createElement('a'); a.href = url; a.download = '截图-' + Date.now(); a.click();