java 开源Java库在服务器端生成网页缩略图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/169573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:16:33  来源:igfitidea点击:

Open source Java library to produce webpage thumbnails server-side

javaopen-sourcethumbnails

提问by Mads Hansen

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 Amazonor websnapr.

我正在寻找一个开源 Java 库来为给定的 URL 生成缩略图。我需要捆绑此功能,而不是调用外部服务,例如Amazonwebsnapr

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

http://www.webrenderer.com/在这篇文章中提到:服务器生成的网页截图,但它是一个商业解决方案。

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.

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

Any suggestions?

有什么建议?

采纳答案by McDowell

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 JDICWebBrowsercontrol or the SWTBrowser(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.

首先想到的是使用 AWT 来捕获屏幕抓取(见下面的代码)。您可以查看捕获JEdi​​torPaneJDIC WebBrowser控件或SWT浏览器(通过AWT 嵌入支持)。后两个嵌入原生浏览器(IE、Firefox),所以引入依赖;JEdi​​torPane HTML 支持在 HTML 3.2 停止。可能这些都不适用于无头系统。

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);
    }

}

回答by Frank Krueger

You're essentially asking for a complete rendering engine accessible by Java. Personally, I would save myself the hassle and call out to a child process.

您实际上是在要求一个可由 Java 访问的完整渲染引擎。就个人而言,我会省去麻烦并调用子进程。

Otherwise, I ran into this pure Java browser: Lobo

否则,我遇到了这个纯 Java 浏览器:Lobo

回答by anjanb

wasn't there a QA/test website/service which would let you specify a web page that you wanted to be rendered in a certain browser(IE, FIREFOX, SAFARI version x,y,z) and they would mail the snapshot back to you. '

没有 QA/测试网站/服务可以让您指定要在某个浏览器(IE、FIREFOX、SAFARI 版本 x、y、z)中呈现的网页,然后他们会将快照邮寄回你。'

I can't remember the service -- maybe other developers who frequent ajaxian might remember it ?

我不记得这个服务——也许其他经常使用 ajaxian 的开发人员可能记得它?

回答by Sietse

Try calling ImageMagick. I know it's not a Java solution, but you can call it from Java, and there's even a Java front-end, although I've had less success with that.

尝试调用ImageMagick。我知道这不是 Java 解决方案,但是您可以从 Java 中调用它,甚至还有一个Java 前端,尽管我在这方面的成功率较低。