I am trying to save a BufferedImage as a PNM file. I already installed the JAI (Java Advanced Imaging), and have the PNMWriter plug-in imported. However, I don't know how to add it to my ImageWriter so it can write in .pnm. When I run ImageIO.getWriterFormatNames() to get the possible format names, only the standard ones (.png, .bmp, .jpg....) come up... What do
Asked
Active
Viewed 654 times
4
-
I just found out that there is a scanForPlugins() class in ImageIO, however it did not make any difference. – Pedro Cimini Jul 01 '11 at 15:40
2 Answers
1
I implemented this myself for my software. It was only 30 lines of source code and I did not want to add Java Advanced Imaging for something that can be solved so easily. Here is my solution:
public static void write(BufferedImage image, OutputStream stream) throws IOException
{
/*
* Write file header.
*/
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
stream.write('P');
stream.write('6');
stream.write('\n');
stream.write(Integer.toString(imageWidth).getBytes());
stream.write(' ');
stream.write(Integer.toString(imageHeight).getBytes());
stream.write('\n');
stream.write(Integer.toString(255).getBytes());
stream.write('\n');
/*
* Write each row of pixels.
*/
for (int y = 0; y < imageHeight; y++)
{
for (int x = 0; x < imageWidth; x++)
{
int pixel = image.getRGB(x, y);
int b = (pixel & 0xff);
int g = ((pixel >> 8) & 0xff);
int r = ((pixel >> 16) & 0xff);
stream.write(r);
stream.write(g);
stream.write(b);
}
}
stream.flush();
}

Simon C
- 1,977
- 11
- 14
0
Use JAI (JAI
class), not ImageIO
(Java standard), use:
JAI.create("ImageWrite", renderedImage, file, "pnm");

David Oliván
- 2,717
- 1
- 19
- 26