0

是否有任何库或 API 可用于将 MHT 文件转换为图像?我们可以使用通用文档转换器软件来做到这一点吗?欣赏任何想法。

4

1 回答 1

1

如果您真的想以编程方式执行此操作,

MHT

存档网页。当您在 Internet Explorer 中将网页另存为 Web 存档时,网页会将此信息保存为多用途 Internet 邮件扩展 HTML (MHTML) 格式,文件扩展名为 .MHT。重新映射网页中的所有相关链接,并且嵌入的内容包含在 .MHT 文件中。

您可以使用JEditorPane将其转换为图像

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public class Test {
    private static volatile boolean loaded;

    public static void main(String[] args) throws IOException {
        loaded = false;
        URL url = new URL("http://www.google.com");
        JEditorPane editorPane = new JEditorPane();
        editorPane.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("page")) {
                    loaded = true;
                }
            }
        });
        editorPane.setPage(url);
        while (!loaded) {
            Thread.yield();
        }

        File file = new File("out.png");

        componentToImage(editorPane, file);
    }

    public static void componentToImage(Component comp, File file) throws IOException {
        Dimension prefSize = comp.getPreferredSize();
        System.out.println("prefSize = " + prefSize);
        BufferedImage img = new BufferedImage(prefSize.width, comp.getPreferredSize().height,
                                              BufferedImage.TYPE_INT_ARGB);
        Graphics graphics = img.getGraphics();
        comp.setSize(prefSize);
        comp.paint(graphics);
        ImageIO.write(img, "png", file);
    }

}
于 2009-08-24T13:58:32.123 回答