【问题标题】:convert MHT files to images将 MHT 文件转换为图像
【发布时间】:2010-11-22 06:50:27
【问题描述】:

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

【问题讨论】:

  • 我认为这个问题与编程有关!

标签: converter


【解决方案1】:

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

MHT

存档网页。保存 Web 时 页面作为 Internet 中的 Web 存档 Explorer,网页保存了这个 多用途互联网信息 邮件扩展 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);
    }

}

【讨论】:

    猜你喜欢
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2011-03-29
    • 2014-10-08
    • 2013-08-13
    • 2011-11-04
    • 2012-12-21
    相关资源
    最近更新 更多