我的面板看起来像:
当我转换为图像
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;
}
它看起来很糟糕 - 更大更差的质量
我如何将这个图像的质量更改为面板的质量?
编辑:
您需要编译此代码的库:
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;