开源Java库,用于生成服务器端的网页缩略图 [英] Open source Java library to produce webpage thumbnails server-side

查看:372
本文介绍了开源Java库,用于生成服务器端的网页缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索一个开源Java库来为给定的URL生成缩略图。我需要捆绑此功能,而不是呼叫外部服务,例如亚马逊 websnapr

I am searching for an open source Java library to generate thumbnails for a given URL. I need to bundle this capability, rather than call out to external services, such as Amazon or websnapr.

http://www.webrenderer.com/ 服务器生成的网页截图,但这是一个商业解决方案。

http://www.webrenderer.com/ was mentioned in this post: Server generated web screenshots, but it is a commercial solution.

我希望有一个基于Java的解决方案,但可能需要考虑执行外部流程,如 khtml2png ,或集成诸如 html2ps 之类的内容。

I'm hoping for a Java based solution, but may need to look into executing an external process such as khtml2png, or integrating something like html2ps.

有什么建议吗?

推荐答案

第一件事就是记住是使用AWT捕获屏幕抓取(见下面的代码)。您可以查看捕获 JEditorPane JDIC WebBrowser 控件或 SWT 浏览器(通过 AWT嵌入支持)。后两者嵌入了原生浏览器(IE,Firefox),因此引入依赖关系; JEdi​​torPane HTML支持在HTML 3.2处停止。可能这些都不适用于无头系统。

The first thing that comes to mind is using AWT to capture a screen grab (see code below). You could look at capturing the JEditorPane, the JDIC WebBrowser control or the SWT Browser (via the AWT embedding support). The latter two embed native browsers (IE, Firefox), so introduce dependencies; the JEditorPane HTML support stopped at HTML 3.2. It may be that none of these will work on a headless system.

import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JLabel;

public class Capture {

    private static final int WIDTH = 128;
    private static final int HEIGHT = 128;

    private BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
            BufferedImage.TYPE_INT_RGB);

    public void capture(Component component) {
        component.setSize(image.getWidth(), image.getHeight());

        Graphics2D g = image.createGraphics();
        try {
            component.paint(g);
        } finally {
            g.dispose();
        }
    }

    private BufferedImage getScaledImage(int width, int height) {
        BufferedImage buffer = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = buffer.createGraphics();
        try {
            g.drawImage(image, 0, 0, width, height, null);
        } finally {
            g.dispose();
        }
        return buffer;
    }

    public void save(File png, int width, int height) throws IOException {
        ImageIO.write(getScaledImage(width, height), "png", png);
    }

    public static void main(String[] args) throws IOException {
        JLabel label = new JLabel();
        label.setText("Hello, World!");
        label.setOpaque(true);

        Capture cap = new Capture();
        cap.capture(label);
        cap.save(new File("foo.png"), 64, 64);
    }

}

这篇关于开源Java库,用于生成服务器端的网页缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆