简体   繁体   English

是否可以获取JavaScript进行屏幕截图?

[英]Is it possible to get javascript to take a screenshot?

Bit of a long shot, but is there a javascript function/process which allows automatically takes a screenshot of a specified area, and outputs a picture to a folder on my computer. 有点远,但是有一个javascript函数/过程可以自动拍摄指定区域的屏幕快照,并将图片输出到我计算机上的文件夹中。 Ideally, it would be able to put within a loop so that it takes a picture for each loop iteration. 理想情况下,它将能够放入循环中,以便为每次循环迭代拍摄一张照片。

Is this at all remotely possible? 这根本不可能吗?

If you have a web server with PHP installed, you can simulate this using wkhtmltoimage . 如果您具有安装PHP的Web服务器,则可以使用wkhtmltoimage对此进行模拟。 To generate a new file every 5 seconds, your JS would be: 要每5秒生成一个新文件,您的JS将是:

$(document).ready(function() {
    function takeImage() {
        $.post(
            'htmltoimage.php',
            { currentUrl : window.location + "?stopTimer=1" }, // data that your script might need
            function(data) {
                if (data.url !== undefined) {
                    console.log("The URL where you can download your image is " + data.url);
                }
            },
            'json' // use JSON for expected return type
        );
    }
    var startTimer = <?php echo (isset($_POST['stopTimer']) ? "false" : "true"); ?>
    if (startTimer) { setTimeout(takeImage, 5000); }
});

Your PHP file would simply use wkhtmltoimage to go to your URL. 您的PHP文件将仅使用wkhtmltoimage转到您的URL。 In its most simple form: 最简单的形式:

<?php
    function() {
        $outputLocation = "/images/output_" . strtotime(date()) . ".png";
        // assuming wkhtmltoimage is in your PATH (otherwise specify full path to executable below)
        `wkhtmltoimage $_POST['currentUrl'] $outputLocation`;
        return array('url' => $outputLocation);
    }
?>

You can then crop it at the positions you desire using ImageMagick or a similar PHP image processing library. 然后,您可以使用ImageMagick或类似的PHP图像处理库将其裁剪到所需的位置。

This can also be achieved using Adobe AIR, or really any program that uses WebKit. 这也可以使用Adobe AIR或使用WebKit的任何程序来实现。

Yes, you can. 是的你可以。 There is a useful library for that. 有一个有用的库。 You might want to take a look: 您可能想看看:

http://phantomjs.org/screen-capture.html http://phantomjs.org/screen-capture.html

Since PhantomJS is using WebKit, a real layout and rendering engine, it can capture a web page as a screenshot. 由于PhantomJS使用的是WebKit,它是真正的布局和渲染引擎,因此它可以捕获网页作为屏幕截图。 Because PhantomJS can render anything on the web page, it can be used to convert contents not only in HTML and CSS, but also SVG and Canvas. 由于PhantomJS可以呈现网页上的任何内容,因此它不仅可以用于转换HTML和CSS中的内容,还可以用于转换SVG和Canvas中的内容。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM