问题描述
我的面板看起来像:
当我转换成图像
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;
}
它看起来很糟糕 - 质量越来越高质量
it looks terrible - larger and worse quality
如何将图像的质量改为面板的质量?
how I can change quaity of this image to the quality of panel ?
编辑:
库您需要编译此代码:
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;
推荐答案
从字面上回答你的问题:
To literally answer your question:
我如何才能将此图片的质量改为面板质量?
简单,不要改变你的图像的大小。放下整个将您的图像拉伸到A4尺寸,因为这是质量损失的原因。
Simple, do not alter the size of your image. Drop the whole 'stretching your image to A4 size' as this is the cause of the quality loss.
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尺寸。将小图像拉伸到较大版本将始终导致质量损失。
Not sure however if this is what you have been looking for. If you really want the image to be on A4 size, I suggest trying to get your panel sharply rendered on A4 size before converting it to an image. Stretching a small image to a larger version will always result in quality loss.
这篇关于如何将java swing面板转换为质量图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!