如何使用特定的文件名保存画布图像 [英] How to save a canvas image with a specific filename

查看:78
本文介绍了如何使用特定的文件名保存画布图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,可以显示实时流,然后可以对实时流进行快照。

I have a web page that can show a live stream and then be able to snap shot the live stream.

我需要有一个按钮,可以使用我自己的特定文件名(可以是今天的日期)将图像保存在画布中。

I need to have a button that can save the image in the canvas with a specific file name of my own (it can be the date for today).

非常需要它。感谢那些会做出回应的人! :)

Badly need it. Thanks to those who will respond! :)

<!DOCTYPE html>
<html>
<head>
<title>Video Live Stream</title>
<link rel="stylesheet" type="text/css" href="demo'.css"/>
</head>
<body>
<center>
<style>
    <p>
    video { border: 20px solid #ccc; display: block; margin: 0 0 -5px 0; }
    #canvas { margin-top: 20px; border: 1px solid #ccc; display: block; }

    </p>
</style>
</br>
<h3>Live Stream with Snap Shot</h3>
<div align="center">
    <video style="position:relative;top:-75px;" id="video" width="600" height="600" autoplay="">
    </video>
</div>
<div align="center">
<button style="position:relative;top:-60px;" id="snap" class="sexyButton">Snap Photo</button>
<canvas style="position:relative;top:-60px;" class="input" id="canvas" width="640" height="480"></canvas>

</div>
<a href="#" id="downloader" onclick="$filename">Download!</a>
<script>

    // Put event listeners into place
    window.addEventListener("DOMContentLoaded", function() {
        // Grab elements, create settings, etc.
        var canvas = document.getElementById("canvas"),
            context = canvas.getContext("2d"),
            video = document.getElementById("video"),
            videoObj = { "video": true },
            errBack = function(error) {
                console.log("Video capture error: ", error.code); 
            };
        // Put video listeners into place
        if(navigator.getUserMedia) { // Standard
            navigator.getUserMedia(videoObj, function(stream) {
                video.src = stream;
                video.play();
            }, errBack);
        } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
            navigator.webkitGetUserMedia(videoObj, function(stream){
                video.src = window.webkitURL.createObjectURL(stream);
                video.play();
            }, errBack);
        } else if(navigator.mozGetUserMedia) { // WebKit-prefixed
            navigator.mozGetUserMedia(videoObj, function(stream){
                video.src = window.URL.createObjectURL(stream);
                video.play();
            }, errBack);
        }
        // Trigger photo take
        document.getElementById("snap").addEventListener("click", function() {
            context.drawImage(video, 0, 0, 640, 480);
        });

    }, false);
</script>
</center>
</body>
</html>


推荐答案

我刚刚找到了一种保存画布图像的方法您自己的文件名。您可以在以下位置找到它

I just found a way to save the canvas image with the filename of your own. You can find it at

http://eligrey.com/demos/FileSaver.js/

编辑画布部分:

<section id="image-demo">
 <form id="canvas-options">
 <label>Filename: <input type="text" class="filename" id="canvas-filename" placeholder="filename"/>.png</label>
        <input type="submit" value="Save"/>
        <input type="button" id="canvas-clear" value="Clear"/>
</form>

并插入以下脚本:

<script type="application/ecmascript" async="" src="Blob.js"/>
<script type="application/ecmascript" async="" src="canvas-toBlob.js"/>
<script type="application/ecmascript" async="" src="FileSaver.js"/>
<script type="application/ecmascript">

有效!

这篇关于如何使用特定的文件名保存画布图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆