在 java 中输出图像

Outputting Image in java

我在 java 中编写了一个程序,它接受彩色图像并将其转换为灰度图像。图像被读取为 BufferedImage,RGB 组件被提取和修改并设置为相同的图像以显示在创建的控制台 window 上。但我希望输出为单独的 jpeg 或 png 文件。谁能告诉我怎么做?

编辑:

    public static void saveToFile(BufferedImage img)throws FileNotFoundException, IOException
          {
          File outputfile = new File("E:\Java\Sample.jpg");
          ImageIO.write(img, "jpg", outputfile);
          }

这是我希望使用的方法。这里的 img 是我正在使用的图像(编辑,即更改像素值)。我要存储输出的路径是 E:\Java。请有人帮助..

这样使用:

{
    File outputfile = new File("E:\Java\Sample.jpg");
    FileOutputStream fos = new FileOutputStream(outputfile);
    ImageIO.write(img, "jpg", outputfile);
    fos.flush();
    fos.close()
 }