我正在尝试在我的应用程序中使用TwelveMonkeys来处理缩略图生成。另外,我使用maven-dependency-plugin
来解压所有依赖项并创建一个FAT jar。虽然,插件在正常情况下工作得很好,但在与maven-dependency-plugin一起使用时根本不起作用。这是我的POM XML:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.prakhar</groupId>
<artifactId>demo</artifactId>
<name>demo</name>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<organization>
<!-- Used as the 'Vendor' for JNLP generation -->
<name>Your Organisation</name>
</organization>
<properties>
<slf4j.version>1.6.1</slf4j.version>
</properties>
<build>
<plugins>
<plugin>
<!-- Get the dependencies jar files to a common place, for jar signing -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<!-- <includeArtifactIds>commons-net,imageio-bmp,imageio-jpeg,imageio-tiff,imageio-pnm,imageio-psd,imageio-iff,imageio-pcx,imageio-pict,imageio-sgi,imageio-tga,imageio-icns,imageio-thumbsdb,imageio-core,imageio-metadata,common-lang,common-io,common-image</includeArtifactIds> -->
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- MigLayout -->
<!-- Apache Commons -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- Twelve Monkeys dependencies -->
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-bmp</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tiff</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-pnm</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-psd</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-iff</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-pcx</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-pict</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-sgi</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tga</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-icns</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-thumbsdb</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>
</project>
而且,这是我的功能:
private static void convertToJpg(File file) {
try {
BufferedImage image = ImageIO.read(file);
int width = 300, height = 200; // new width/height
BufferedImageOp resampler = new ResampleOp(width, height, ResampleOp.FILTER_LANCZOS); // A good default filter, see class documentation for more info
BufferedImage thumbnailImg = resampler.filter(image, null);
FileImageOutputStream fios = new FileImageOutputStream(new File(file.getAbsolutePath() + ".png"));
if (!ImageIO.write(thumbnailImg, "PNG", fios)) {
System.out.println("ERROR: Unable to convert image " + file.getAbsolutePath());
} else {
System.out.println("SUCCESS: Image " + file.getAbsolutePath() + ".png converted successfully");
}
} catch (Exception e) {
e.printStackTrace();
}
}
以下是我得到的例外情况:
java.lang.NullPointerException: Input == null
at com.twelvemonkeys.image.ResampleOp.filter(Unknown Source)
如果有人知道如何解决,请说明。
答案 0 :(得分:3)
总结一下,对于未来的读者来说,问题是每个ImageIO插件JAR都包含META-INF/services
下的多个资源。自动发现ImageIO插件需要这些资源。他们是:
META-INF/services/javax.imageio.spi.ImageReaderSpi
META-INF/services/javax.imageio.spi.ImageWriterSpi
META-INF/services/javax.imageio.spi.ImageInpuStreamSpi
META-INF/services/javax.imageio.spi.ImageOutputStreamSpi
可能还有更多,但TwelveMonkeys不使用这些。
感兴趣的是,您可以详细了解Service Provider Interface机制和ServiceRegistry
类。
将所有JAR文件解压缩到一个文件夹中时,每个JAR都会覆盖这些文件。相反,具有相同名称的资源需要合并到一个新的单一资源中。
Maven Shade plugin为此提供了解决方案,请参阅资源转换器,尤其是ServicesResourceTransformer。