如何Java实现网页截图技术

1、最直接的方式——使用Robot

 

方法详解:该方法利用Robat提供的强大桌面操作能力,硬性调用浏览器打开指定网页,并将网页信息保存到本地。

 

优势:简单易用,不需要任何第三方插件。

 

缺点:不能同时处理大量数据,技术含量过低,属于应急型技巧(不推荐)。

 

实现方法:使用如下代码即可。

  1. package cc.javar.util;
  2. import java.awt.AWTException;
  3. import java.awt.Desktop;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.Rectangle;
  8. import java.awt.Robot;
  9. import java.awt.Toolkit;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.image.BufferedImage;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.net.MalformedURLException;
  15. import java.net.URISyntaxException;
  16. import java.net.URL;
  17. import javax.imageio.ImageIO;
  18. public class CutPicture {
  19.     public  void main() throws MalformedURLException,
  20.             IOException, URISyntaxException, AWTException {
  21.         // 此方法仅适用于JdK1.6及以上版本
  22.         Desktop.getDesktop().browse(
  23.                 new URL("http://www.baidu.com").toURI());
  24.         Robot robot = new Robot();
  25.         robot.delay(10000);
  26.         Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
  27.         int width = (int) d.getWidth();
  28.         int height = (int) d.getHeight();
  29.         // 最大化浏览器
  30.         robot.keyRelease(KeyEvent.VK_F11);
  31.         robot.delay(2000);
  32.         Image image = robot.createScreenCapture(new Rectangle(00, width,
  33.                 height));
  34.         BufferedImage bi = new BufferedImage(width, height,
  35.                 BufferedImage.TYPE_INT_RGB);
  36.         Graphics g = bi.createGraphics();
  37.         g.drawImage(image, 00, width, height, null);
  38.         // 保存图片
  39.         ImageIO.write(bi, "jpg"new File("d:/google"+System.currentTimeMillis()+".jpg"));
  40.     }
  41. }

2.比较好用的组建,正在调试,需要JWebBrowser 组件。

  1. package cc.javar.util;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import javax.imageio.ImageIO;
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11. import javax.swing.SwingUtilities;
  12. import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.IO;
  13. import chrriis.dj.nativeswing.swtimpl.NativeComponent;
  14. import chrriis.dj.nativeswing.swtimpl.NativeInterface;
  15. import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
  16. import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
  17. import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;
  18. public class ScreenUtils extends JPanel {
  19.     public static final long serialVersionUID = 1L;
  20.     // 行分隔符  
  21.     public static final String LS = System.getProperty("line.separator""/n");
  22.     // 文件分割符  
  23.     public static final String FS = System.getProperty("file.separator""//");
  24.     //以javascript脚本获得网页全屏后大小  
  25.     final static StringBuffer jsDimension;
  26.     static {
  27.         jsDimension = new StringBuffer();
  28.         jsDimension.append("var width = 0;").append(LS);
  29.         jsDimension.append("var height = 0;").append(LS);
  30.         jsDimension.append("if(document.documentElement) {").append(LS);
  31.         jsDimension.append("width = Math.max(width, document.documentElement.scrollWidth);").append(LS);
  32.         jsDimension.append("height = Math.max(height, document.documentElement.scrollHeight);").append(LS);
  33.         jsDimension.append("}").append(LS);
  34.         jsDimension.append("if(self.innerWidth) {").append(LS);
  35.         jsDimension.append("width = Math.max(width, self.innerWidth);").append(LS);
  36.         jsDimension.append("height = Math.max(height, self.innerHeight);").append(LS);
  37.         jsDimension.append("}").append(LS);
  38.         jsDimension.append("if(document.body.scrollWidth) {").append(LS);
  39.         jsDimension.append("width = Math.max(width, document.body.scrollWidth);").append(LS);
  40.         jsDimension.append("height = Math.max(height, document.body.scrollHeight);").append(LS);
  41.         jsDimension.append("}").append(LS);
  42.         jsDimension.append("return width + ':' + height;");
  43.     }
  44.     public ScreenUtils(final String fileName, final String url, final int maxWidth, final int maxHeight) {
  45.         super(new BorderLayout());
  46.         JPanel webBrowserPanel = new JPanel(new BorderLayout());
  47.         final JWebBrowser webBrowser = new JWebBrowser(null);
  48.         webBrowser.setBarsVisible(false);
  49.         webBrowser.navigate(url);
  50.         webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
  51.         add(webBrowserPanel, BorderLayout.CENTER);
  52.         JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 44));
  53.         webBrowser.addWebBrowserListener(new WebBrowserAdapter() {
  54.             // 监听加载进度  
  55.             public void loadingProgressChanged(WebBrowserEvent e) {
  56.                 // 当加载完毕时  
  57.                 if (e.getWebBrowser().getLoadingProgress() == 100) {
  58.                     String result = (String) webBrowser.executeJavascriptWithResult(jsDimension.toString());
  59.                     int index = result == null ? -1 : result.indexOf(":");
  60.                     NativeComponent nativeComponent = webBrowser.getNativeComponent();
  61.                     Dimension originalSize = nativeComponent.getSize();
  62.                     Dimension imageSize = new Dimension(Integer.parseInt(result.substring(0, index)), Integer.parseInt(result.substring(index + 1)));
  63.                     imageSize.width = Math.max(originalSize.width,imageSize.width + 50);
  64.                     imageSize.height = Math.max(originalSize.height,imageSize.height + 50);
  65.                     nativeComponent.setSize(imageSize);
  66.                     BufferedImage image = new BufferedImage(imageSize.width,imageSize.height, BufferedImage.TYPE_INT_RGB);
  67.                     nativeComponent.paintComponent(image);
  68.                     nativeComponent.setSize(originalSize);
  69.                     try {
  70.                         // 输出图像  
  71.                         ImageIO.write(image, "jpg"new File(fileName));
  72.                     } catch (IOException ex) {
  73.                         ex.printStackTrace();
  74.                     }
  75.                     // 退出操作  
  76.                     System.exit(0);
  77.                 }
  78.             }
  79.         }
  80.         );
  81.         add(panel, BorderLayout.SOUTH);
  82.     }
  83.     public static void Screen(final String fileName,final String url){
  84.         NativeInterface.open();
  85.         SwingUtilities.invokeLater(new Runnable() {
  86.             public void run() {
  87.                 // SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser  
  88.                 JFrame frame = new JFrame("以DJ组件保存指定网页截图");
  89.                 // 加载指定页面,最大保存为640x480的截图  
  90.                 frame.getContentPane().add(new ScreenUtils(fileName, url, 1024768),BorderLayout.CENTER);
  91.                 frame.setSize(1024768);
  92.                 // 仅初始化,但不显示  
  93.                 frame.invalidate();
  94.                 frame.pack();
  95.                 frame.setVisible(false);
  96.             }
  97.         });
  98.         NativeInterface.runEventPump();
  99.    }
  100. }

jar包

腾讯云服务器安全可靠高性能,多种配置供您选择
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: