用Java截屏整个网页 [英] Screenshot the whole web page in Java

查看:1129
本文介绍了用Java截屏整个网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何获取整个网页的屏幕截图.我正在使用20多个dc.js图表​​在仪表板上工作,对所有这些图表进行交叉过滤,并且我正在使用JSP.客户端希望有一个用户单击的按钮,它将对整个网页进行屏幕截图.我正在通过Java运行它,因为我们必须使用IE11作为我们的标准,并且所有其他js库(例如HTML2canvas.js)都无法正常工作(它不会显示dc.js图表​​),尽管它在Chrome上可以正常工作,但我们必须使用IE11(任何建议都会有所帮助).

I'm trying to figure out of how to get a screenshot the whole web page. I'm working on a dashboard using 20 plus dc.js charts, cross filters all of them, and i'm using JSP. Client want to have a button where a user clicks and it will screen shot the whole web page. I'm running it through java because we have to use IE11 as our standard and all other js libraries such as HTML2canvas.js didnt work (it doesnt display the dc.js charts) though it sort of works on Chrome but we have to use IE11 (any suggestions would help).

到目前为止,当我单击一个按钮时,它将在JAVA中运行main方法进行屏幕截图.到目前为止,我正在使用java.awt.Robot,但我进行了研究,并说它仅基于主监视器的屏幕截图.我正在尝试对我的网页进行屏幕截图.有没有办法做到这一点?如果是这样怎么办?这是下面的我的Java代码.它基于监视器截屏...

So far when I click on a button, it will run a main method in JAVA to do a screen shot. So far I'm using java.awt.Robot but I researched and it says it only screen shot base on the screen of a primary monitor. I'm trying to have a screen shot of my web page. Is there a way to do that? if so how? Here is my Java code below. It screenshot base on the monitor...

 package com.customer_inquiry;

import java.net.URL;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

    public class screenshotAction {

        public static void main(String[] args) throws Exception {

    String outFileName = args[0];
    if (!outFileName.toLowerCase().endsWith(".png")) {
                System.err.println("Error: output file name must " + "end with \".png\".");
                System.exit(1);
            }
            // determine current screen size
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Dimension screenSize = toolkit.getScreenSize();
            Rectangle screenRect = new Rectangle(screenSize);
            // create screen shot
            Robot robot = new Robot();


            BufferedImage image = robot.createScreenCapture(screenRect);
            // save captured image to PNG file
            ImageIO.write(image, "png", new File(outFileName));
            // give feedback
            System.out.println("Saved screen shot (" + image.getWidth() + " x " + image.getHeight() + " pixels) to file \""
                    + outFileName + "\".");
            // use System.exit if the program hangs after writing the file;
            // that's an old bug which got fixed only recently
            // System.exit(0);
        }
    }

推荐答案

有html2canvas可能适合您的需求: https ://html2canvas.hertzen.com/

There is html2canvas that might suit your needs: https://html2canvas.hertzen.com/

除此之外,如果您想自己做,那么想到的是:

Apart from that, if you want to do it yourself, what comes to mind is to:

  • 首先创建一个svg
  • 一个ForeignObject标记
  • 使用XMLSerializer对整个html进行序列化,然后通过innerHTML将其设置为foreignObject.或者使用cloneNode.
  • 在画布上绘制svg
  • 下载画布

这篇关于用Java截屏整个网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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