首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java swing高质量PNG

Java swing高质量PNG
EN

Stack Overflow用户
提问于 2020-06-23 01:43:21
回答 1查看 103关注 0票数 0

我一直在尝试将我的swing 2d图表导出为png文件。我尝试了以下代码:

代码语言:javascript
复制
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics2D referenceGraphics = bufferedImage.createGraphics();
referenceGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
paintComponent(referenceGraphics);

File imageFile = new File(fileName);
if (imageFile.exists() || imageFile.createNewFile()) {
    ImageIO.write(bufferedImage, "png", imageFile);
}

然而,png文件创建成功,但质量太低。我需要创建至少300ppi的图像文件。如何做到这一点呢?

EN

回答 1

Stack Overflow用户

发布于 2020-06-23 20:24:10

简单地增加组件的大小并不会使图像的分辨率更好。它只会更大。您需要做的是创建比组件更大的BufferedImage (即3倍于当前分辨率的3倍),并缩放BufferedImageGraphics对象。生成的代码将如下所示:

代码语言:javascript
复制
public static void main(String[] args) throws IOException {
    Component comp = ...
    BufferedImage img = scaledImageFromComponent(comp, 3);
    File imageFile = new File(fileName);
    if (imageFile.exists() || imageFile.createNewFile()) {
        ImageIO.write(img, "png", imageFile);
    }
}

public static BufferedImage scaledImageFromComponent(final Component c, final double scale) {
    c.setSize(c.getPreferredSize());
    c.doLayout();
    Rectangle r = new Rectangle(0, 0, c.getWidth(), c.getHeight());
    return scaledImageFromComponent(c, r, scale, scale, false);
}

public static BufferedImage scaledImageFromComponent(final Component c, final Rectangle bounds,
                                                     final double scalex, final double scaley,
                                                     final boolean print) {
    BufferedImage image = createCompatibleTransparentImage((int) (scalex * bounds.width),
                                                           (int) (scaley * bounds.height));
    final Graphics2D g2d = (Graphics2D) image.getGraphics();
    g2d.scale(scalex, scaley);
    g2d.translate(-bounds.x, -bounds.y);
    if (print) {
        c.print(g2d);
    } else {
        c.paint(g2d);
    }
    g2d.dispose();
    return image;
}

public static BufferedImage createCompatibleTransparentImage(final int width,
                                                             final int height) {
    return isHeadless() ? new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
                        : getGraphicsConfiguration().createCompatibleImage(width, height,
                                                                           Transparency.BITMASK);
}

private static boolean isHeadless() {
    return GraphicsEnvironment.isHeadless();
}

private static GraphicsConfiguration getGraphicsConfiguration() {
    return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
}

如果您的图表包含文本,您可能希望将Transparency.BITMASKTransparency.OPAQUE进行切换,以获得更好的抗锯齿支持(在Windows上)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62520417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档