问题
my panel looks like:
when I convert to image
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 ?
EDIT:
library which you need to compile this code:
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;
回答1:
To literally answer your question:
how I can change quality of this image to the quality of panel ?
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;
}
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.
回答2:
you can use the Graphics2D transform so the panel's paint immediately goes to the scaled image
BufferedImage resizedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
g.transform(AffineTransform.getScaleInstance((float)panel.getWidth()/w,
(float)panel.getHeight()/h));//this might need to be inverted I'm not sure...
panel.paint(g);
g.dispose();
btw the setRenderingHint won't do anything useful after you disposed the Graphics
来源:https://stackoverflow.com/questions/10366660/how-to-convert-java-swing-panel-to-quality-image