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

查看:1084
本文介绍了我能告诉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。有没有办法THA我可以从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天全站免登陆