开发者

Help on adding plug-in to Java ImageWriter

I am trying to save a Buffe开发者_如何学JAVAredImage 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


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();
        }


Use JAI (JAI class), not ImageIO (Java standard), use:

JAI.create("ImageWrite", renderedImage, file, "pnm");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜