- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道在java中写一个函数来读取一个ppm文件并将其转换为jpg
或bmp
格式的可行性如何。有人有这方面的经验吗?我可以使用 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/
正如标题中所描述的那样。代码如下所示 #include #include using namespace std; #define WIDTH 90 #define HEIGHT 180 cla
我正在开发一个程序来调整 BMP 文件的大小并将其存储在一个新文件中。我注意到有些 BMP 完全颠倒存储,而其他 BMP 则只是直立存储。所以我做了两个解决方案来处理这两种情况。我的问题是如何事先知道
如何使用 ZPL II 打印位图 (BMP) 图像? 我使用 ~DY 将 BMP 下载到打印机: ~DYR:PRINT,B,B, , , 我正在使用 PHP 将原始数据发送到打印机,因此 <>
我正在尝试将彩色 BMP 文件转换为灰度 BMP。输入 bmp 是 24 位,我在输出端生成相同的 24 位 bmp,只是这次是灰度。 我使用的代码是 for(int x = 0; x < max;
正如在 this example 中所见, BMP 文件中的每个 channel (R, G, B) 接受一个输入。一个 24 位的 BMP 图像有 8 位的-R、8 位的 G 和 8 位的 B。我在
总结: 直接计算表明:一张3289 X 4570 X 32bpp的.BMP图片大约需要53MB。同样大小但24bpp的图片大约需要43MB,16bpp的图片大约需要28MB。在我的情况下,ChemDr
我有 24 位图像,我读取位图并将其转换为灰度并像 8 位一样保存。 RGBTRIPLE temp; unsigned char t; ... t = (temp.rgbtBlue * 0.114 +
我想从另一个更大的 bmp 文件中找到一个小的 bmp 文件(更大的一个是从屏幕上捕获的并称为 Sample.bmp ,小的 bmp 文件称为 Button.bmp 。事情是在比较图像时文件可以随处可
我需要使用 .bmp 类型的图像。其格式为: struct bmp_fileheader { unsigned char fileMarker1; /* 'B' */ unsigne
我正在尝试获取一个 BMP 文件并将其读入,然后对其中的像素执行操作以更改其颜色。我的问题是我无法将文件中的数据读入两个 BMP header 结构。我能够很好地将所有数据读入第一个结构,但在读入第二
这个问题已经有答案了: Why does comparing strings using either '==' or 'is' sometimes produce a different resul
我正在尝试使用以下代码将 bmp 图像转换为 jpeg。 from PIL import Image img = Image.open('/Desktop/xyz.bmp') new_img = im
在 24 位 bmp 中,像素存储为 BGR,每种颜色仅占用 1 个字节。那个可以读 for(i=0;i
我在学校有一个任务,要将水印 bmp 图像添加到其他 bmp 图像中。该任务称为 alpha 混合。我必须在用户将在启动时通过程序参数设置的特定坐标插入水印,以及水印混合的 alpha 值。我几乎成功
这是我的位图对象 Bitmap b = new Bitmap(columns, rows, PixelFormat.Format8bppIndexed); BitmapData bmd = b.Loc
我的编程平台是: Ubuntu 3.19 ImageMagick 6.7.7 编程语言:php 我的代码: $img = new Imagick($image_input_24bit_bmp); $i
这是我的函数,我根据维基百科 BITMAPINFOHEADER 使用标题 BMP。但是,我得到的文件没有任何图像...当放置填充时,该过程停止。 // Structures for header i
我正在发布一个 .NET 程序,并且正处于完善阶段。我需要在项目属性中设置图标。根据我的研究,我想创建以下像素大小的图像以适应所有图标大小:16、32、48、96、256。 我还找到了一个程序,可以将
我正在尝试从 BMP 图像创建 GD 图像资源,但是我没有运气。 有问题的 BMP 图像是用 Photoshop 创建和保存的。我也尝试了几个在网上找到的 BMP,它们给出了相同的结果。 getima
类型 FXPT2DOT30 出现在为 BMP 文件定义 struct CIEXYZ 时,根据微软提供的定义: http://msdn.microsoft.com/en-us/library/windo
我是一名优秀的程序员,十分优秀!