从画布拍摄间隔照片
Capture photos in interval from canvas
我有一个脚本,允许我在画布中显示网络摄像头并在一定间隔内"下载"特定帧。当时间参数很大(每 30 秒捕获 2 分钟)时,我遇到了问题。它平稳运行了大约 15 分钟,然后崩溃(Firefox 关闭并出现内存不足错误)。此外,重新启动 Firefox 后,有时会在 3-4 分钟内拍摄许多 0 字节照片,然后再次开始工作。我正在实验室中放置的旧 2 GB RAM 机器上运行它,有没有办法减少内存使用量?
这是一段带有参数和函数realizarCapturas
的代码。我可以添加其余代码,但我认为要优化的部分应该是这个。
var frecuenciaComienzoCaptura = 1; // how long till next capture
var frecuenciaCaptura = 3; //seconds between photos
var duracion = 5; // amount of photos to capture
function realizarCapturas(){
var i = 1;
var interval = setInterval(function(){
if(i <= duracion){
context.drawImage(video, 0, 0, 640, 480);
var imagen = document.getElementById("imagen");
imagen.href = canvas.toDataURL("image/png");
var now = new Date();
var filename = formatNumber(now.getHours()) + "-" + formatNumber(now.getMinutes()) + "-" + formatNumber(now.getSeconds());
imagen.download = filename + ".png"; // Make sure the browser downloads the image
imagen.click(); // Trigger the click
i = i+1;
}else{
clearInterval(interval);
}
}, frecuenciaCaptura * 1000);
}
setInterval(function(){
realizarCapturas();
}, frecuenciaComienzoCaptura * 1000 * 60 * 60);
realizarCapturas();
}, false);
通常永远不要使用 setInterval
,因为它可能是调用堆栈溢出的来源,这在代码中很难检测到。
您的问题是您没有清除正在生成的所有间隔,因此每 3 秒创建一个新的间隔事件。最终,运行少量代码所需的时间将比您创建的所有间隔事件所能管理的时间长,因此每个间隔将继续将其事件推送到调用堆栈,但在堆栈上放置更多间隔最终导致崩溃之前,不会有机会运行。setInterval 也不保证事件之间的时间是准确的。
请改用setTimeout
。这样,您只会根据需要生成事件,而不必保留句柄来关闭事件。
下面是您编写的代码,以便您永远不会有调用堆栈溢出。
var frecuenciaComienzoCaptura = 1 * 1000* 60 * 60; // how long till next capture
var frecuenciaCaptura = 3 * 1000; //seconds between photos
var duracion = 5; // amount of photos to capture
var counter = 0;
// the capture function
var captura = function () {
counter = counter + 1;
if(counter < duracion){ // do we need more images?
// only create timer events as needed.
setTimeout(captura, frecuenciaCaptura); //set time till next image
}
context.drawImage(video, 0, 0, 640, 480);
var imagen = document.getElementById("imagen");
imagen.href = canvas.toDataURL("image/png");
var now = new Date();
var filename = formatNumber(now.getHours()) + "-" + formatNumber(now.getMinutes()) + "-" + formatNumber(now.getSeconds());
imagen.download = filename + ".png"; // Make sure the browser downloads the image
imagen.click(); // Trigger the click
}
function realizarCapturas() {
// request next batch of captures by only creating one timer event as we need
setTimeout(realizarCapturas,frecuenciaComienzoCaptura);
counter = 0; // reset counter
captura(); // capture timages
}
// start captures.
realizarCapturas();
相关文章:
- 漂亮照片图片库中的Facebook赞按钮
- 图片照片库
- 将照片从javascript发送到php
- 如何设置SVG路径的样式以拍摄图像
- 是否可以创建一个带有进度条的跨浏览器AJAX照片上传器
- 照片滑块的条件语句
- 为什么缩放按钮不会显示在照片擦除中
- 如何使用默认的网络摄像头拍摄照片并将其保存在我的c#.net web应用程序中
- 如何使用VLC Web插件拍摄视频快照
- 脸书's的inApp浏览器在尝试上传表单中的照片时崩溃
- 使用不同的缩略图来拍摄漂亮的照片
- 使用网络摄像头或设备摄像头拍摄照片
- 如何通过HP或平板电脑拍摄照片,并使用PHP自动上传
- 从网络应用程序中的网络摄像头或移动相机拍摄照片
- 从画布拍摄间隔照片
- Phonegap+JQM-相机API,无法查看拍摄的照片
- 从图片中拍摄的照片在android的cordova相机插件中不显示裁剪
- 使用html5的网络摄像头拍摄一张照片
- 无法在第一页加载时使用HTML5和Google Chrome中的getUserMedia()从网络摄像头拍摄照片
- Phonegap拍摄照片并将图像(BLOB)插入数据库SQLite