简体   繁体   English

javax.imageio.ImageIO 文件格式常量

[英]javax.imageio.ImageIO file format constants

In javax.imageio.ImageIO there is a method #write(RenderedImage im, String formatName, OutputStream output) that accepts a "stringly-typed" format developer wants image to be written to.javax.imageio.ImageIO有一个方法#write(RenderedImage im, String formatName, OutputStream output)接受开发人员希望写入图像的“字符串类型”格式。

There is an inner class called CanEncodeImageAndFormatFilter deep inside that file that checks if there is an SPI (whatever it is) that supports the aforementioned format.在该文件深处有一个名为CanEncodeImageAndFormatFilter的内部类,用于检查是否存在支持上述格式的 SPI(无论它是什么)。 For example, BMPImageWriterSpi declares例如, BMPImageWriterSpi声明

    private static String[] formatNames = {"bmp", "BMP"};

But in all these SPI-classes these format names are declared either private or package-default, so I cannot access them in my application.但是在所有这些 SPI 类中,这些格式名称被声明为私有或默认包,因此我无法在我的应用程序中访问它们。 But I'd like to expose that format parameter in API I'm working on right now, but I don't want to reinvent the wheel.但是我想在我现在正在处理的 API 中公开该格式参数,但我不想重新发明轮子。

➥ Is there an enum or something I can safely use as a format name parameter for the #write method? ➥ 是否有枚举或我可以安全地用作#write方法的格式名称参数的#write

Yes, this is more like an opinion based question with coding-style tag.是的,这更像是带有编码风格标签的基于意见的问题。 :D :D


UPD: What I have: UPD:我所拥有的:

 ImageIO.write(image, "jpg", outStream);

What I want:我想要的是:

 ImageIO.write(image, ImageTypes.JPEG, outStream);

There is no such Enum you are looking for in the default Java API.在默认 Java API 中没有您要查找的此类 Enum。

The reason is that the Java Image I/O API uses the Service provider interface (SPI) .原因是 Java Image I/O API 使用服务提供者接口 (SPI) Which allows to create extensible applications.这允许创建可扩展的应用程序。 In the scope of your question additional image reader/writer classes (which implements the respective interface) can be added to the classpath and will be discovered automatically during runtime.在您的问题范围内,可以将其他图像读取器/写入器类(实现相应接口)添加到类路径中,并将在运行时自动发现。 The application can use them without even knowing at compile time they exist.应用程序甚至可以在编译时不知道它们存在的情况下使用它们。 Hence such Enum not exist.因此这样的 Enum 不存在。

You can discover all currently known ImageWriter implementations with following snippet.您可以使用以下代码段发现所有当前已知的ImageWriter实现。

import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageWriterSpi;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        IIORegistry registry = IIORegistry.getDefaultInstance();
        Iterator<ImageWriterSpi> serviceProviders = registry.getServiceProviders(ImageWriterSpi.class, false);
        while(serviceProviders.hasNext()) {
            ImageWriterSpi next = serviceProviders.next();
            System.out.printf("description: %-27s   format names: %s%n",
                    next.getDescription(Locale.ENGLISH),
                    Arrays.toString(next.getFormatNames())
            );
        }
    }
}

output for Java 13 Java 13 的输出

description: Standard BMP Image Writer     format names: [bmp, BMP]
description: Standard JPEG Image Writer    format names: [JPEG, jpeg, JPG, jpg]
description: Standard WBMP Image Writer    format names: [wbmp, WBMP]
description: Standard PNG image writer     format names: [png, PNG]
description: Standard GIF image writer     format names: [gif, GIF]
description: Standard TIFF image writer    format names: [tif, TIF, tiff, TIFF]

Below is a simplified example how the SPI could be used.以下是如何使用 SPI 的简化示例。

assume following files in the current directory假设当前目录中有以下文件

Main.class                  - the one from above source
ExampleImageWriterSpi.java  - an example implementation of the ImageWriterSpi

ExampleImageWriterSpi.java ExampleImageWriterSpi.java

package sub.optimal;

import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriter;
import javax.imageio.spi.ImageWriterSpi;
import java.io.IOException;
import java.util.Locale;

public class ExampleImageWriterSpi extends ImageWriterSpi {

    private static final String[] formatNames = { "xyz", "XYZ"};

    public String getDescription(Locale locale) {
        return "Example XYZ image writer";
    }

    @Override
    public String[] getFormatNames() {
        return formatNames;
    }

    // following is without implementation in this example
    public boolean canEncodeImage(ImageTypeSpecifier type) {
        return false;
    }

    public ImageWriter createWriterInstance(Object extension) throws IOException {
        return null;
    }
}

compile the class编译类

javac ExampleImageWriterSpi.java

create the provider configuration file创建提供者配置文件
(the filename specifies the implemented interface, the line specifies the implementing class) (文件名指定实现的接口,行指定实现类)

mkdir -p META-INF/services/
echo sub.optimal.ExampleImageWriterSpi > META-INF/services/javax.imageio.spi.ImageWriterSpi

create an archive for the service provider为服务提供者创建档案

jar cf example-image-writer.jar META-INF/ sub/

run the plain example code运行简单的示例代码

java Main

output (all service providers which are part of the Java runtime)输出(作为 Java 运行时一部分的所有服务提供者)

description: Standard BMP Image Writer     format names: [bmp, BMP]
description: Standard JPEG Image Writer    format names: [JPEG, jpeg, JPG, jpg]
description: Standard WBMP Image Writer    format names: [wbmp, WBMP]
description: Standard PNG image writer     format names: [png, PNG]
description: Standard GIF image writer     format names: [gif, GIF]
description: Standard TIFF image writer    format names: [tif, TIF, tiff, TIFF]

run the example with the additional service provider使用附加服务提供程序运行示例

java -cp example-image-writer.jar:. Main

output输出

description: Example XYZ image writer      format names: [xyz, XYZ]
description: Standard JPEG Image Writer    format names: [JPEG, jpeg, JPG, jpg]
description: Standard WBMP Image Writer    format names: [wbmp, WBMP]
description: Standard PNG image writer     format names: [png, PNG]
description: Standard GIF image writer     format names: [gif, GIF]
description: Standard BMP Image Writer     format names: [bmp, BMP]
description: Standard TIFF image writer    format names: [tif, TIF, tiff, TIFF]

You might create the enum yourself.您可以自己创建枚举。 But you should check at runtime if there is a provider available for this image type.但是您应该在运行时检查是否有可用于此图像类型的提供程序。 eg TIFF was not available in the Java runtime before Java 9.例如 TIFF 在 Java 9 之前的 Java 运行时中不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM