关于向 Java ImageWriter 添加插件的帮助

发布于 2024-11-17 20:20:38 字数 218 浏览 4 评论 0原文

我正在尝试将 BufferedImage 保存为 PNM 文件。我已经安装了 JAI(Java Advanced Imaging),并导入了 PNMWriter 插件。但是,我不知道如何将其添加到我的 ImageWriter 中,以便它可以以 .pnm 格式写入。当我运行 ImageIO.getWriterFormatNames() 来获取可能的格式名称时,只出现标准格式名称(.png、.bmp、.jpg...)...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2024-11-24 20:20:38

我自己为我的软件实现了这个。它只有 30 行源代码,我不想添加 Java Advanced Imaging 来解决如此容易解决的问题。这是我的解决方案:

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

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();
        }
浅沫记忆 2024-11-24 20:20:38

使用 JAI(JAI 类),而不是 ImageIO(Java 标准),使用:

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

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

JAI.create("ImageWrite", renderedImage, file, "pnm");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文