JAVA实现网页快照 存为图片格式

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
截取的google的效果,将就吧,不是特别好。 但是作为普通的应用,我想这个效果我已经很满意了。
注意,里面的
this.setVisible(true);
这句话如果运行在一些不能显示图形界面的机器上,请屏蔽掉它,不过这样的话,网页里的图片就不能被截取了。


效果图:



完整的源代码如下:
  1. import java.awt.Graphics2D;
  2. import java.awt.RenderingHints;
  3. import java.awt.geom.AffineTransform;
  4. import java.awt.image.BufferedImage;
  5. import java.awt.image.ColorModel;
  6. import java.awt.image.WritableRaster;
  7. import java.io.*;
  8. import javax.imageio.*;
  9. import javax.swing.*;

  10. /**
  11.  * HTML2JPG,HTML页面转图片的实现方法。
  12.  * 
  13.  * @author 老紫竹(Java世纪网,java2000.net)
  14.  */
  15. public class Test extends JFrame {

  16.   public Test(String url, File file) throws Exception {
  17.     JEditorPane editorPane = new JEditorPane();
  18.     editorPane.setEditable(false);
  19.     editorPane.setPage(url);
  20.     JScrollPane jsp = new JScrollPane(editorPane);
  21.     getContentPane().add(jsp);
  22.     this.setLocation(00);
  23.     this.setVisible(true); // 如果这里不设置可见,则里面的图片等无法截取
  24.     
  25.     // 如果不延时,则图片等可能没有时间下载显示
  26.     // 具体的秒数需要根据网速等调整
  27.     Thread.sleep(5 * 1000);

  28.     setSize(1000010000);

  29.     pack();
  30.     // BufferedImage image = new BufferedImage(editorPane.getWidth(),
  31.     // editorPane.getHeight(), BufferedImage.TYPE_INT_RGB);
  32.     BufferedImage image = new BufferedImage(editorPane.getWidth(), editorPane.getHeight(),
  33.         BufferedImage.TYPE_INT_RGB);
  34.     Graphics2D graphics2D = image.createGraphics();
  35.     editorPane.paint(graphics2D);
  36.     
  37.     BufferedImage image1 = resize(image, 600400);

  38.     ImageIO.write(image1, "jpg", file);
  39.     dispose();
  40.   }

  41.   public static void main(String[] args) throws Exception {
  42.     new Test("http://www.google.cn"new File("d:/file.jpg"));
  43.   }

  44.   public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
  45.     // targetW,targetH分别表示目标长和宽
  46.     int type = source.getType();
  47.     BufferedImage target = null;
  48.     double sx = (double) targetW / source.getWidth();
  49.     double sy = (double) targetH / source.getHeight();
  50.     // 这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
  51.     // 则将下面的if else语句注释即可
  52.     if (sx > sy) {
  53.       sx = sy;
  54.       targetW = (int) (sx * source.getWidth());
  55.       // } else {
  56.       // sy = sx;
  57.       // targetH = (int) (sy * source.getHeight());
  58.     }
  59.     if (type == BufferedImage.TYPE_CUSTOM) { // handmade
  60.       ColorModel cm = source.getColorModel();
  61.       WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
  62.       boolean alphaPremultiplied = cm.isAlphaPremultiplied();
  63.       target = new BufferedImage(cm, raster, alphaPremultiplied, null);
  64.     } else
  65.       target = new BufferedImage(targetW, targetH, type);
  66.     Graphics2D g = target.createGraphics();
  67.     // smoother than exlax:
  68.     g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  69.     g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
  70.     g.dispose();
  71.     return target;
  72.   }
  73. }



     欢迎访问老紫竹的网站( http://www.java2000.net )和我在CSDN的博客( http://blog.csdn.net/java2000_net );           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/jgfyyfd/article/details/83821450