gpt4 book ai didi

javascript - 如何拍摄 视频流的照片? HTML5 JavaScript

转载 作者:太空宇宙 更新时间:2023-11-04 16:01:32 25 4
gpt4 key购买 nike

当使用<video id="videoID"> </video>时(HTML),

您可以使用canvas = document.getElementById('canvas')在脚本中(Javascript)

videoID.play()videoID.pause()

如果我的视频是从 URL 流式传输(就像网络摄像头那样) <img src="http://ip_address/stream">标签,我无法使用 .play().pause()功能。我还能用吗:

    canvas = document.getElementById('canvas');

var context = acontainer.getContext('2d');
context.drawImage(acontainer, 0, 0, width, height);
var data = acontainer.toDataURL('image/jpeg');
photo.setAttribute('src', data);

目标是在单击时获取视频流的图片 - 因此我有一个鼠标单击事件来执行上述操作。

我该怎么做?

更多信息:此页面的 URL http://ip_address视频流的 URL http://ip_address/stream 。视频流为<img> -大量jpeg图像正在流式传输- <div>内容器,例如代码看起来像 -

    <!DOCTYPE html>
<html>
<head>
...
<style>
...
</style>
</head>
<body>
<div id="container">
<img src=...>
</div>
<script>
container.addEventListener("click", function(element){}, false);
...
</script>
</body>
</html>

编辑:这个问题不是重复的,因为我不是问如何流式传输到 html Canvas 。相反,我已经在容器中显示了一个流,我想为其拍一张照片并将其显示在 Canvas 中 - 这样流和照片就会出现在同一页面上。

最佳答案

Set .getContext("2d") 应该在 canvas 元素上调用,而不是在 acontainer 上调用;将 img 引用作为第一个参数传递给 .drawImage() 而不是 acontainer.toDataURL() 应该在 canvas 元素上调用,而不是在 acontainer 上调用。

var blue = "data:image/png;charset=binary;base64,iVBORw0KGgoAAAANSUhEUgAAASoAAADCCAYAAAD+Wo90AAAABHNCSVQICAgIfAhkiAAAApVJREFUeJzt1DEBACAMwLCBf88ggZMeiYJeXTPnDEDY/h0A8GJUQJ5RAXlGBeQZFZBnVECeUQF5RgXkGRWQZ1RAnlEBeUYF5BkVkGdUQJ5RAXlGBeQZFZBnVECeUQF5RgXkGRWQZ1RAnlEBeUYF5BkVkGdUQJ5RAXlGBeQZFZBnVECeUQF5RgXkGRWQZ1RAnlE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gCRzApLMCUgyJyDJnIAkcwKSzAlIMicgyZyAJHMCkswJSDInIMmcgCRzApLMCUgyJyDJnIAkcwKSzAlIMicgyZyAJHMCkswJSDInIMmcgCRzApLMCUgyJyDJnIAkcwKSzAlIMicgyZyAJHMCkswJSDInIMmcgCRzApLMCUgyJyDJnIAkcwKSzAlIMicgyZyAJHMCkswJSDInIMmcgCRzApLMCUgyJyDJnIAkcwKSzAlIMicgyZyAJHMCkswJSDInIMmcgCRzApLMCUgyJyDJnIAkcwKSzAlIMicgyZyAJHMCkswJSDInIMmcgCRzApLMCUgyJyDJnIAkcwKSzAlIMicgyZyAJHMCkswJSDInIMmcgCRzApLMCUgyJyDJnIAkcwKSzAlIMicgyZyAJHMCkswJSDInIMmcgCRzApLMCUgyJyDJnIAkcwKSzAlIMicgyZyAJHMCkswJSDInIMmcgCRzApLMCUgyJyDJnIAkcwKSzAlIMicgyZyAJHMCkswJSDInIMmcgCRzApLMCUgyJyDJnIAkcwKSzAlIMicgyZyAJHMCkswJSDInIMmcgCRzApLMCUi6PDoDAbyCsn8AAAAASUVORK5CYII=";

var arr = [red, blue, green];

var img = document.querySelector("img");

var button = document.querySelector("button");

var containerImg = document.querySelector("div > img");

var canvas = document.createElement("canvas");

canvas.width = canvas.height = 100;

var ctx = canvas.getContext("2d");

var i = 0;

button.addEventListener("click", function() {
ctx.drawImage(img, 0, 0)
containerImg.src = canvas.toDataURL();
});

setInterval(function() {
img.src = arr[i];
i = ++i % arr.length;
}, 100);
<button>capture image</button><br><br>
<img width="100" height="100" alt="stream"/>
<br><br>
<div>
<img width="100" height="100" alt="capture"/>
</div>

关于javascript - 如何拍摄 <img> 视频流的照片? HTML5 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42234835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com