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


     截取的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. }
  • 相关阅读:
    初识Mysql 连接器的收获(包含JDBC API最新文档)以及一些c++的有用技巧
    重拾 ”多项式“ 给我的启示
    vs2015下配置MySQL,使之能使用c++连接完美运行
    在CentOS上使用yum安装java
    CentOS 用yum安装中文输入法
    Redhat Linux RHEL5配置CentOS YUM更新源
    转:Linux下which、whereis、locate、find 命令的区别
    Centos 挂载NTFS格式的USB硬盘
    scp采用无密码在两台linux服务器之间传输数据
    转:Andriod studio技巧合集
  • 原文地址:https://www.cnblogs.com/encounter/p/2189048.html
Copyright © 2020-2023  润新知