0

我正在尝试从动态 html 布局呈现图像。如何根据html内表格的高度和宽度动态设置图像的宽度和高度。我的意思是我怎样才能找到桌子width(x)height(y)html

    JLabel label = new JLabel(html);
    label.setSize(x, y);
    BufferedImage image = new BufferedImage(label.getWidth(),label.getHeight(),BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = (Graphics2D)image.getGraphics();
    g2d.setColor(Color.WHITE);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    label.paint(g2d);
    g2d.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        ImageIO.write(image, "png", baos);
    } catch (IOException e) {
        e.printStackTrace();
    }

下面是html的概要。

<html>
<head>....</head>
<body topmargin="0" leftmargin="0"><table border=0 cellpadding=0 cellspacing=0 width=700 style='border-collapse:collapse;table-layout:fixed;width:684pt'>
......
......
</body></html>
4

1 回答 1

3

以下代码有效。这个想法是,如果您不将渲染JLabel放在带有 的容器中LayoutManager,您将需要设置JLabel自己的大小。使用 可以很容易地找到合适的尺寸getPreferredSize()。这应该只在这种情况下完成。在所有其他情况下,您不应该调用setSize()自己,而应该将所有这些都留给LayoutManager.

结果图像:

在此处输入图像描述

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

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

class TestPaint {

    protected void initUI() {
        StringBuilder sb = new StringBuilder("<html>");
        sb.append("<table>");
        for (int i = 0; i < 5; i++) {
            sb.append("<tr>");
            for (int j = 0; j < 5; j++) {
                sb.append("<td>");
                sb.append("Cell ").append(i + 1).append(' ').append(j + 1);
                sb.append("</td>");
            }
            sb.append("</tr>");
        }
        sb.append("</table>");
        JLabel label = new JLabel(sb.toString());
        label.setSize(label.getPreferredSize());
        BufferedImage image = new BufferedImage(label.getWidth(), label.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setColor(Color.WHITE);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        label.paint(g2d);
        g2d.dispose();
        File file = new File("/tmp/test.png");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        FileOutputStream baos = null;
        try {
            baos = new FileOutputStream(file);
            ImageIO.write(image, "png", baos);


        Desktop.getDesktop().open(file);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestPaint().initUI();
            }

        });
    }
}
于 2013-05-13T17:08:58.227 回答