我可以告诉 BufferedImage 最初的文件类型是什么吗? [英] Can I tell what the file type of a BufferedImage originally was?

查看:72
本文介绍了我可以告诉 BufferedImage 最初的文件类型是什么吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我有一个 BufferedImage 加载了 ImageIO 像这样上课:

In my code, I have a BufferedImage that was loaded with the ImageIO class like so:

BufferedImage image = ImageIO.read(new File (filePath);

稍后,我想将它保存到一个字节数组,但是 ImageIO.write 方法要求我选择 GIF、PNG 或 JPG 格式来将我的图像写入(如描述在教程这里).

Later on, I want to save it to a byte array, but the ImageIO.write method requires me to pick either a GIF, PNG, or JPG format to write my image as (as described in the tutorial here).

我想选择与原始图像相同的文件类型.如果图像最初是 GIF,我不希望将其保存为 PNG 的额外开销.但是,如果图像最初是 PNG,我不想通过将其保存为 JPG 或 GIF 来失去半透明性等.有没有办法可以从 BufferedImage 确定原始文件格式是什么?

I want to pick the same file type as the original image. If the image was originally a GIF, I don't want the extra overhead of saving it as a PNG. But if the image was originally a PNG, I don't want to lose translucency and such by saving it as a JPG or GIF. Is there a way that I can determine from the BufferedImage what the original file format was?

我知道我可以在加载图像时简单地解析文件路径以找到扩展名,然后将其保存以备后用,但我最希望的是直接从 BufferedImage 中执行此操作.

I'm aware that I could simply parse the file path when I load the image to find the extension and just save it for later, but I'd ideally like a way to do it straight from the BufferedImage.

推荐答案

正如@JarrodRoberson 所说,BufferedImage 没有格式"(即没有文件格式,它确实有几种像素格式之一,或像素布局").我不知道 Apache Tika,但我想他的解决方案也适用.

As @JarrodRoberson says, the BufferedImage has no "format" (i.e. no file format, it does have one of several pixel formats, or pixel "layouts"). I don't know Apache Tika, but I guess his solution would also work.

但是,如果您更喜欢仅使用 ImageIO 而不想向您的项目添加新的依赖项,您可以编写如下代码:

However, if you prefer using only ImageIO and not adding new dependencies to your project, you could write something like:

ImageInputStream input = ImageIO.createImageInputStream(new File(filePath));

try {
    Iterator<ImageReader> readers = ImageIO.getImageReaders(input);

    if (readers.hasNext()) {
        ImageReader reader = readers.next();

        try {
            reader.setInput(input);

            BufferedImage image = reader.read(0);  // Read the same image as ImageIO.read

            // Do stuff with image...

            // When done, either (1):
            String format = reader.getFormatName(); // Get the format name for use later
            if (!ImageIO.write(image, format, outputFileOrStream)) {
                // ...handle not written
            }
            // (case 1 done)

            // ...or (2):
            ImageWriter writer = ImageIO.getImageWriter(reader); // Get best suitable writer

            try {
                ImageOutputStream output = ImageIO.createImageOutputStream(outputFileOrStream);

                try {
                    writer.setOutput(output);
                    writer.write(image);
                }
                finally {
                    output.close();
                }
            }
            finally {
                writer.dispose();
            }
            // (case 2 done)
        }
        finally {
            reader.dispose();
        }
    }
}
finally {
    input.close();
}

这篇关于我可以告诉 BufferedImage 最初的文件类型是什么吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆