Help on adding plug-ins to Java imagewriter

I tried to save the buffered image as a PNM file I have installed Jai (Java advanced imaging) and imported the pnmwriter plug-in However, I don't know how to add it to my imagewriter, so it can be used Prepared by PNM When I run imageio Getwriterformatnames() to get possible format names, only standard names (. PNG,. BMP,. JPG...)

Solution

I implemented this myself for my software It has only 30 lines of source code, and I don't want to add Java advanced imaging. Com for problems that can be easily solved This 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();
        }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>