动态渲染图像

时间:2013-05-13 16:33:48

标签: java html swing jlabel bufferedimage

我正在尝试从动态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>

1 个答案:

答案 0 :(得分:3)

以下代码有效。我们的想法是,如果您不将渲染JLabel放在带有LayoutManager的容器中,则需要自己设置JLabel的大小。使用getPreferredSize()可以轻松找到合适的大小。这应该仅在这种情况下完成。在所有其他情况下,您不应自己致电setSize(),而应将所有内容留给LayoutManager

结果图片:

enter image description here

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

        });
    }
}