use*_*072 6 html javascript canvas webrtc
最近我一直试图依靠Webrtc创建一个照相亭,几乎完成了所有的代码,除了我已经找不到一种方法来保存图像.
这是我到目前为止最接近我的目标:
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>fotogoof</title>
<link rel="Stylesheet" href="css/bootstrap.css"/>
<link rel="Stylesheet" href="css/style.css"/>
<script type="text/javascript">
window.onload = function () {
var img = document.getElementById('screenshot');
var button = document.getElementById('saveImage');
img.src = '';
img.onload = function () {
button.removeAttribute('disabled');
};
button.onclick = function () {
window.location.href = img.src.replace('image/png', 'image/octet-stream');
};
};
</script>
</head>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<h2 class="brand">Html5 Photobooth</h1>
</div>
</div>
</div>
<div class="container" id="body-wrap">
<div class="container" id="main">
<div class="span10">
<div id="video-container">
<canvas width="400" height="320" class="mask"></canvas>
<div id="counter">
</div>
</div>
<br><div id="pic-holder" class="pull-left"><img id="screenshot"></div></br>
<input id="saveImage" type="button" value="save image" disabled="disabled"/>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
该代码基本上是将网络摄像头流并将其输入到画布元素中.按空格按钮会触发截屏,然后显示为图像.由于我无法提供正在下载的文件的确切来源,因此该按钮仅在浏览器的另一个选项卡中打开图像,而不是正常运行.我真的很感激我可以如何解决这个问题.提前致谢.
我已经设法用这段代码在图像中捕获画布中的流:
document.addEventListener ('keydown', function (event) {
if(event.keyCode == 32) {
document.querySelector('#screenshot').src = canvas.toDataURL('image/webp')
}
})
Run Code Online (Sandbox Code Playgroud)
我想知道的是如何让用户从那里将图像保存到他们的计算机上.
小智 17
如果我理解正确你想下载(即为用户提供一个保存对话框来选择一个位置)图像到用户的机器?
如果是这样,您可以使用此代码段:
function download(canvas, filename) {
/// create an "off-screen" anchor tag
var lnk = document.createElement('a'),
e;
/// the key here is to set the download attribute of the a tag
lnk.download = filename;
/// convert canvas content to data-uri for link. When download
/// attribute is set the content pointed to by link will be
/// pushed as "download" in HTML5 capable browsers
lnk.href = c.toDataURL();
/// create a "fake" click-event to trigger the download
if (document.createEvent) {
e = document.createEvent("MouseEvents");
e.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false,
false, 0, null);
lnk.dispatchEvent(e);
} else if (lnk.fireEvent) {
lnk.fireEvent("onclick");
}
}
Run Code Online (Sandbox Code Playgroud)
现在您需要做的就是提供一个默认文件名并调用该函数:
download(myCanvas, 'untitled.png');
Run Code Online (Sandbox Code Playgroud)
如果你不是这意味着直接保存到用户的硬盘,那么你不能出于安全原因.
话虽如此,总是可以选择使用本地存储,例如File API或Indexed DB - 但是因为这些是沙箱,用户只能通过浏览器访问"文件",所以可能不那么方便.
Roy*_*M J -1
对于第一个任务,您可以使用画布对象支持的 toDataUrl 方法将画布内容导出到图像。
var canvas = document.getElementById("canvas");
if (canvas.getContext) {
var ctx = canvas.getContext("2d"); // Get the context for the canvas.
var myImage = canvas.toDataURL("image/png"); // Get the data as an image.
}
var image = document.getElementById("screenshot"); // Get the image object.
image.src = myImage;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12707 次 |
| 最近记录: |