开发者

save graphics to buffered image

I have few classes and functions in java code. one of the function is drawing a graph using graphics object. I want to save this graph in jpg format so i did:

public void paintComponent(Graphics graphics) {
Graphics g1=null;
double scale = ((double) ySize) / histogram.getMaxFrequency(band);
if (histColor != null)
  graphics.setColor(histColor);
for (int x = 0; x < 256; ++x) {
  int y = (int) Math.round(scale*histogram.getFrequency(band, x));
  i开发者_StackOverflow中文版f (y > 0)
    graphics.drawLine(x+xOrigin, yOrigin, x+xOrigin, yOrigin-y);
}
FileOutputStream fileopstream;
try {
    fileopstream = new FileOutputStream("C:\\Users\\spider\\Desktop\\Study\\Computer Vision\\programs\\histogram1.jpg");
JPEGImageEncoder output=JPEGCodec.createJPEGEncoder(fileopstream);  
BufferedImage image= new BufferedImage(100,100,BufferedImage.TYPE_BYTE_BINARY);
//WritableRaster raster=image.getRaster();
g1=image.createGraphics();
g1=graphics.create();
//g1.dispose();
//graphics.drawImage(image, 0, 0, 100, 100, null, null);
//output.encode(image);
// Save as JPEG
File file = new File("newimage.jpg");
ImageIO.write(image, "jpg", file);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (ImageFormatException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
drawAxis(graphics, xOrigin, yOrigin+1);

}

but it is not working. The stored image is a blank image and not the graph which i can see on screen. can you pls help me

thanks a lot


got the answer :) checked: http://www.exampledepot.com/egs/javax.imageio/Graphic2File.html#comment-83262

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜