jc8*_*c88 11 java tiff image jai
最近我在尝试显示图像文件时遇到问题.不幸的是,图像格式是TIFF格式,主要的网络浏览器不支持(因为我知道只有Safari支持这种格式).由于某些限制,我必须将此格式转换为主浏览器支持的其他格式.但是,当我尝试转换格式时,它给我带来了很多问题.
我在网上搜索过,虽然在这个链接中发布了类似的问题如何在Java中将TIF转换为PNG?"但我不能得到它提出的结果..
因此,我再次提出这个问题,希望能得到更好的解释和指导.
在完成提议的解决方案时,我遇到的问题很少:
1)根据Jonathan Feinberg提出的答案,需要安装JAI和JAI/ImageIO.但是,在我安装了它们之后,我仍然无法在Netbean 7.2中导入该文件.NetBean 7.2仍然建议导入默认的imageIO库.
2)当我使用默认的ImageIO库Read方法时,它将返回NULL值,我无法继续进行.
3)我还尝试了其他方法,例如使用BufferedOutputStream方法将TIFF文件转换为BIN文件,但结果文件大于11 MB,这太大而无法加载并最终加载失败.
if (this.selectedDO != null) {
String tempDO = this.selectedDO.DONo;
String inPath = "J:\\" + tempDO + ".TIF";
String otPath = "J:\\" + tempDO + ".bin";
File opFile = new File(otPath);
File inFile = new File(inPath);
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(inPath), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(new FileOutputStream(otPath), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
try {
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
因此,希望能得到所有人的帮助和建议,以便我可以将TIFF格式转换为其他格式,如JPEG/PNG.
jc8*_*c88 16
经过一些研究和测试,发现了一种将TIFF转换为JPEG的方法,很抱歉等待很长时间只上传了这个答案.
SeekableStream s = new FileSeekableStream(inFile);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
RenderedImage op = dec.decodeAsRenderedImage(0);
FileOutputStream fos = new FileOutputStream(otPath);
JPEGEncodeParam jpgparam = new JPEGEncodeParam();
jpgparam.setQuality(67);
ImageEncoder en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);
en.encode(op);
fos.flush();
fos.close();
Run Code Online (Sandbox Code Playgroud)
p/s:otPath是指您想要存储JPEG图像的路径.例如:"C:/image/abc.JPG"; inFile是输入文件,它是TIFF文件
至少这种方法对我来说是可行的.如果还有其他更好的方法,请与我们分享.
感谢致敬,
小智 9
添加依赖项
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.3.1</version> </dependency>
Run Code Online (Sandbox Code Playgroud)https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core
https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core/1.3.1
编码
final BufferedImage tif = ImageIO.read(new File("test.tif"));
ImageIO.write(tif, "png", new File("test.png"));
Run Code Online (Sandbox Code Playgroud)如果页面很多,请执行以下操作:
添加依赖项:
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.4.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)使用以下Java8代码
public void convertTiffToPng(File file) {
try {
try (InputStream is = new FileInputStream(file)) {
try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(is)) {
Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInputStream);
if (iterator == null || !iterator.hasNext()) {
throw new RuntimeException("Image file format not supported by ImageIO: " + file.getAbsolutePath());
}
// We are just looking for the first reader compatible:
ImageReader reader = iterator.next();
reader.setInput(imageInputStream);
int numPage = reader.getNumImages(true);
// it uses to put new png files, close to original example n0_.tiff will be in /png/n0_0.png
String name = FilenameUtils.getBaseName(file.getAbsolutePath());
String parentFolder = file.getParentFile().getAbsolutePath();
IntStream.range(0, numPage).forEach(v -> {
try {
final BufferedImage tiff = reader.read(v);
ImageIO.write(tiff, "png", new File(parentFolder + "/png/" + name + v + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
38645 次 |
最近记录: |