gpt4 book ai didi

java - 在 java 中将 PPM 转换为 JPG 或 BMP

转载 作者:行者123 更新时间:2023-11-30 06:15:23 25 4
gpt4 key购买 nike

我想知道在java中写一个函数来读取一个ppm文件并将其转换为jpgbmp格式的可行性如何。有人有这方面的经验吗?我可以使用 ImageMagick 等工具实现目标,但我想以纯 Java 方式实现。

最佳答案

除了旧的 Sun JAI 实现之外,还有其他 ImageIO 插件 TwelveMonkeys ImageIO它扩展了 JDK/JRE 中的 ImageIO 实现。

下面是一个使用这些插件的小例子。该示例取决于版本 3.1-SNAPSHOT(早期版本不提供 PNM 支持)。所以需要先构建插件工程。

./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>sub.optimal</groupId>
<artifactId>JAI-Demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-pnm</artifactId>
<version>3.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.1-SNAPSHOT</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<killAfter>-1</killAfter>
<mainClass>sub.optimal.jai.Main</mainClass>
<systemProperties>
<systemProperty>
<key>user.dir</key>
<value>${project.build.directory}\classes</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
</project>

./src/main/java/sub/optimal/jai/Main.java

package sub.optimal.jai;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import javax.imageio.ImageIO;

/**
*
* @author SubOptimal
*/
public class Main {

public static void main(String[] args) throws IOException {
String outFormat = "%-17s: %s%n";

String filesDirectory = System.getProperty("user.dir");
System.out.printf(outFormat, "files directory", filesDirectory);

System.out.printf(outFormat, "supported formats", Arrays.toString(ImageIO.getWriterFormatNames()));

Path inputFile = Paths.get(filesDirectory, "pond.ppm");
System.out.printf(outFormat, "input file", inputFile.toAbsolutePath());
InputStream is = Files.newInputStream(inputFile, StandardOpenOption.READ);
BufferedImage image = ImageIO.read(is);

File outputFile = Paths.get(filesDirectory, "output.jpg").toAbsolutePath().toFile();
System.out.printf(outFormat, "output file", outputFile.getAbsolutePath());

boolean writeSuccess = ImageIO.write(image, "JPEG", outputFile);
System.out.printf(outFormat, "write successful", writeSuccess);
}
}

./src/main/resources/pond.ppm
该文件是 jai-1_1_2-unix-sample.tar.gz 中提供的图像之一。

构建并运行示例代码。

mvn clean compile exec:java

关于java - 在 java 中将 PPM 转换为 JPG 或 BMP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28944954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com