我在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);
}
但它不起作用。存储的图像是空白图像,而不是我在屏幕上可以看到的图形。 你能帮我吗
非常感谢
答案 0 :(得分:0)