gpt4 book ai didi

java - 如何将多个 png 图像转换为单个 tiff 文件

转载 作者:行者123 更新时间:2023-12-03 01:53:49 24 4
gpt4 key购买 nike

我有一个由 png 格式的多个图像组成的字节数组。我必须将其转换为 tiff 文件,然后转换为相应的字节数组。此 tiff 文件将保存多个图像。

我已经搜索了很多,但没有成功。问题是。我只能用java来做这个!! :)谁能就我的问题提供一些见解?

我不会从 ImageMagick 等中受益,因为我有一个服务器组件来处理此转换,然后将其作为 tiff 保存在后端。客户端给了我一个字节数组,它将转换为 png 图像。

最佳答案

下面是解决方案。

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.IIOImage;
import javax.imageio.ImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;

/**
*
*
* This class is used to convert the multiple images in to single multi-pages tiff image file.
*
*/
public class MultiPageTiffGenerator {

private static String compressionType="JPEG";

public static boolean generateMultiPageTiff(String dirName,String outputFileName) throws Exception{
boolean generated=false;
ImageOutputStream ios=null;
ImageWriter writer=null;
try {
// verify the directory for the image files
File dir = new File(dirName);
if(null!=dir && !dir.isDirectory()){
throw new FileNotFoundException("No directory exists with the given name"+dirName);
}
// verify the images files exist

File[] files = dir.listFiles();
if(null == files || files.length == 0){
throw new FileNotFoundException("No image files to process");

}else{
// Create the output file on the disk
File file = new File(dirName+outputFileName+".tif");
ios = ImageIO.createImageOutputStream(file);

// Get the appropriate Tiff Image Writer
Iterator <ImageWriter> writers=ImageIO.getImageWritersByFormatName("tiff");
if(null== writers || ! writers.hasNext()){
throw new Exception("Appropriate Tiff writer not found");
}

writer = ImageIO.getImageWritersByFormatName("tiff").next();
writer.setOutput(ios);
// Set the compression parameters for Tiff image
ImageWriteParam param = writer.getDefaultWriteParam();
//param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
//param.setCompressionType(compressionType);
//param.setCompressionQuality(0.9F);

// Loop through all image files and write them to output tiff image
for (int i = 0; i < files.length; i++) {
InputStream fis=null;
int dotIndex= files[i].getName().lastIndexOf('.');
dotIndex=dotIndex>0?dotIndex:files[i].getName().length();
String fileName = files[i].getName().substring(0,dotIndex);
if(!fileName.equalsIgnoreCase(outputFileName)){
try{

fis = new BufferedInputStream(new FileInputStream(files[i]));
BufferedImage image = ImageIO.read(fis);
IIOImage img = new IIOImage(image, null, null);
if (i == 0) {
writer.write(null, img, param);
}
else {
writer.writeInsert(-1, img, param);
}
image.flush();
}finally{
if(null!=fis){
fis.close();
}
}
}

}
ios.flush();
generated=true;

}

}catch(FileNotFoundException ex){
generated=false;
}catch(IOException ex){
generated=false;
}catch(Exception ex){
generated=false;
}finally{
if(null!=ios)
ios.close();
if(null!=writer)
writer.dispose();
}
return generated;
}

}

关于java - 如何将多个 png 图像转换为单个 tiff 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/792255/

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