将图形保存到缓冲图像
我的java代码中的类和函数很少。 其中一项功能是使用图形对象绘制图形。 我想将此图保存为 jpg 格式,所以我这样做了:
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));
if (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);
}
但它不起作用。存储的图像是空白图像,而不是我在屏幕上看到的图表。 你能帮我吗,
非常感谢
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));
if (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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
得到答案了:)
检查:
http://www.exampledepot.com/egs/javax.imageio /Graphic2File.html#comment-83262
got the answer :)
checked:
http://www.exampledepot.com/egs/javax.imageio/Graphic2File.html#comment-83262