日期:2014-05-17 浏览次数:20784 次
import java.awt.Desktop; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.net.URL; import javax.imageio.ImageIO; public class Test { public static void main(String[] args) throws Exception { Desktop.getDesktop().browse(new URL("http://www.baidu.com").toURI());//打开百度首页 Robot robot = new Robot(); robot.delay(10000); Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize()); int width = (int) d.getWidth(); int height = (int) d.getHeight(); robot.keyRelease(KeyEvent.VK_F11); robot.delay(2000); Image image = robot.createScreenCapture(new Rectangle(0, 0, width,height)); BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); Graphics g = bi.createGraphics(); g.drawImage(image, 0, 0, width, height, null); ImageIO.write(bi, "jpg", new File("d://baidu.jpg"));//保存图片 } }
------解决方案--------------------
页面中引用jquery.js和html2canvas.js
执行javascript脚本: $('div').html2canvas();