• 介绍几个java把网页报存为图片的框架


    java在图像这一块非常弱。用java实现java截图倒不难,原理吗就是把当前屏幕存成一个图,然后获取鼠标拉去的想去位置然后把截取的图保存到panel里边,再生成图片即可:示例代码就不展示了,网上很多。下边说几个将网页保存为图片的框架:

    ①html2image

    网上炒这个还不少呢。我说这个就是原声的java代码进行封装的一个jar包。效果非常差,代码就不贴了网上好多。

    ②cobra

    如果你不知道这个的话,你应该听说过lobobrowser,纯java实现的浏览器,测试了下,除了启动慢的要死其他还可以。

    这个代码截取还是不错的,不说了直接上代码:

     1 package htmlToImage;
     2  
     3 import java.awt.image.BufferedImage;
     4 import java.awt.image.RenderedImage;
     5 import java.io.File;
     6  
     7 import javax.imageio.ImageIO;
     8 import javax.swing.JFrame;
     9 import javax.swing.JPanel;
    10 import javax.swing.SwingUtilities;
    11  
    12 import org.lobobrowser.html.gui.HtmlPanel;
    13 import org.lobobrowser.html.test.SimpleHtmlRendererContext;
    14 import org.lobobrowser.html.test.SimpleUserAgentContext;
    15  
    16 public class CobraTest {
    17     public static void main(String[] args) throws Exception {
    18         JFrame window = new JFrame();
    19         HtmlPanel panel = new HtmlPanel();
    20         window.getContentPane().add(panel);
    21         window.setSize(600, 400);
    22         window.setVisible(true);
    23         new SimpleHtmlRendererContext(panel, new SimpleUserAgentContext())
    24                 .navigate("http://jobs.zhaopin.com/377931819252715.htm?ssidkey=y&ss=201&ff=03");
    25         System.out.println("10");
    26         Thread.sleep(10000);
    27         BufferedImage image = new BufferedImage(panel.getWidth(),
    28                 panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
    29  
    30         // paint the editor onto the image
    31         SwingUtilities.paintComponent(image.createGraphics(), panel,
    32                 new JPanel(), 0, 0, image.getWidth(), image.getHeight());
    33         // save the image to file
    34         ImageIO.write((RenderedImage) image, "png", new File("html.png"));
    35         System.out.println("www");
    36     }
    37 }

    但是这个框架应该有个限制,css3应该支持不了。

    ③cssbox

    这个非常不错。如果网站不做故意限制的话,截图非常完美。。。

    package htmlToImage;
     
    import java.io.File;
    import java.io.FileOutputStream;
     
    import org.fit.cssbox.demo.ImageRenderer;
     
    public class CssBox {
        public static void main(String[] args) throws Exception {
            ImageRenderer render = new ImageRenderer();
            System.out.println("kaishi");
            String url = "http://www.xiachufang.com/";
            FileOutputStream out = new FileOutputStream(new File("D:"+File.separator+"html.png"));
            render.renderURL(url, out, ImageRenderer.TYPE_PNG);
            System.out.println("OK");
        }
    }

    ④java原生代码

    package htmlToImage;
     
    import java.awt.image.BufferedImage;
    import java.awt.image.RenderedImage;
    import java.io.File;
    import java.net.URL;
     
    import javax.imageio.ImageIO;
    import javax.swing.JEditorPane;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
     
    /**
     * 原理就是在现在的awt或者swing上显示网页然后将内容保存为一个图片
     * 没办法控制延迟啊。
     * @author zlqiao
     *
     */
    public class JavaCoreApi {
        public static void main(String[] args) throws Exception {
            //load the webpage into the editor
            //JEditorPane ed = new JEditorPane(new URL("http://www.google.com"));
            JEditorPane ed = new JEditorPane(new URL("http://www.baidu.com"));
            System.out.println("10");
            Thread.sleep(10000);
            ed.setSize(1000,1000);
     
            //create a new image
            BufferedImage image = new BufferedImage(ed.getWidth(), ed.getHeight(),
                                                    BufferedImage.TYPE_INT_ARGB);
     
            //paint the editor onto the image
            SwingUtilities.paintComponent(image.createGraphics(), 
                                          ed, 
                                          new JPanel(), 
                                          0, 0, image.getWidth(), image.getHeight());
            //save the image to file
            ImageIO.write((RenderedImage)image, "png", new File("html.png"));
                System.out.println("ok");
         
        }
    }
  • 相关阅读:
    结构~函数~输入输出
    常用缀名
    结构
    枚举
    int argc char*argv[]
    字符串的操作
    字符串函数#include<string.h>
    指针的应用
    2019.1.25~2019.1.30学习总结
    v-for
  • 原文地址:https://www.cnblogs.com/zhangdashao/p/4606704.html
Copyright © 2020-2023  润新知