publicvoidsaveImage(Stringimagename){intw=offScreenImage.getWidth(this);inth=offScreenImage.getHeight(this);//TYPE_INT_RGB这个参数会影响保存的图片的颜色清晰度BufferedImag...
public void saveImage(String imagename) {
int w = offScreenImage.getWidth(this);
int h = offScreenImage.getHeight(this);
// TYPE_INT_RGB 这个参数会影响保存的图片的颜色清晰度
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
try {
g.drawImage(offScreenImage, 0, 0, null);
ImageIO.write(bi, "jpg", new File(imagename));
} catch (IOException e) {
e.printStackTrace();
}
}
offScreenImage 是绘图时的缓存。Image offScreenImage
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(PaintFrame.CANVAS_WIDTH,
PaintFrame.CANVAS_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(backColor);
gOffScreen.fillRect(0, 0, PaintFrame.CANVAS_WIDTH, PaintFrame.CANVAS_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
前面是直接显示的,后面是保存后的图像,感觉差太多了。请问需要如何处理,才能保存清晰的图像。
展开