gpt4 book ai didi

java - 如何将java swing面板转换为高质量图像

转载 作者:行者123 更新时间:2023-11-30 04:48:28 24 4
gpt4 key购买 nike

我的面板看起来像:

enter image description here

当我转换为图像时

public BufferedImage createImage(JPanel panel) {

int w = (int) PageSize.A4.getWidth();//panel.getWidth();
int h = (int) PageSize.A4.getHeight();//panel.getHeight();

BufferedImage originalImage = new BufferedImage(panel.getHeight(), panel.getWidth(), BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D gg = originalImage.createGraphics();
gg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
gg.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
panel.paint(gg);

BufferedImage resizedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, w, h, null);
g.dispose();
g.setComposite(AlphaComposite.Src);

g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

return resizedImage;
}

它看起来很糟糕 - 更大而且质量更差

enter image description here

如何将此图像的质量更改为面板的质量?

编辑:

编译此代码所需的库:

import com.itextpdf.text.PageSize;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

最佳答案

从字面上回答你的问题:

如何将此图像的质量更改为面板的质量?

很简单,不要改变图像的大小。放弃整个“将图像拉伸(stretch)到 A4 尺寸”,因为这是质量损失的原因。

public BufferedImage createImage(JPanel panel) {
BufferedImage originalImage = new BufferedImage(
panel.getHeight(), panel.getWidth(),
BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D gg = originalImage.createGraphics();
gg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
gg.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
panel.paint(gg);
gg.dispose();
return originalImage;
}

但是不确定这是否是您一直在寻找的。如果您确实希望图像为 A4 尺寸,我建议在将其转换为图像之前尝试以 A4 尺寸清晰渲染面板。将小图像拉伸(stretch)到较大的版本总是会导致质量损失。

关于java - 如何将java swing面板转换为高质量图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10366660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com