在Java下,将TIF文件转换为PNG的最佳方法是什么?
简单是可取的,但如果最简单的方法是使用第三方库,那么我会考虑这个解决方案。
答案 0 :(得分:9)
首先,安装JAI。然后安装JAI/ImageIO。然后做
public static void main(final String[] args) throws Exception
{
final BufferedImage tif = ImageIO.read(new File("test.tif"));
ImageIO.write(tif, "png", new File("test.png"));
}
答案 1 :(得分:7)
使用像imageMagic这样的im4java java库,它们的性能和质量比JAI好得多
例如:
import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;
public static void convertTifToPng(File inputImage, File outputImage){
IMOperation op = new IMOperation();
op.addImage(); //place holder for input file
op.addImage(); //place holder for output file
ConvertCmd convert = new ConvertCmd();
convert.run(op, new Object[]{inputImage.getAbsolutePath(), outputImage.getAbsolutePath()});
}
im4java的maven依赖是
<dependency>
<groupId>im4java</groupId>
<artifactId>im4java</artifactId>
<version>0.98.0</version>
</dependency>
答案 2 :(得分:1)
Java高级成像APi是一个很好的图像处理库
答案 3 :(得分:1)
下载JIMI软件开发工具包jimi1_0.zip并将JimiProClasses.zip设置为您的类路径
JIMI是较旧的java图像库,但它易于使用且没有平台相关代码(没有本机可执行文件,可以像标准jar一样使用它)
import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import com.sun.jimi.core.Jimi;
public class JIMIImageConverter {
public static byte[] convert(byte[] inBytes, String inMimeType, String outMimeType) throws Exception{
Image rawImage = Jimi.getImage(new ByteArrayInputStream(inBytes), inMimeType);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Jimi.putImage(outMimeType, rawImage, outputStream);
return outputStream.toByteArray();
}
}
其中inMimeType和outMimeType是图形格式mimetypes
答案 4 :(得分:0)
也许你可以使用这段代码,对我有用
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.IOException;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;
public class ImageConvert {
public static RenderedImage[] readMultiPageTiff(String fileName)throws IOException{
File file = new File(fileName);
SeekableStream seekableStream = new FileSeekableStream(file);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", seekableStream, null);
int numPages = decoder.getNumPages();
RenderedImage image[]= new RenderedImage[numPages];
int count = 0;
for(int i=0;i<decoder.getNumPages();i++){
image[i] = decoder.decodeAsRenderedImage(i);
count++;
}
String newFolderName;
String s3 = fileName;
String [] temp = null;
temp = s3.split("\\.");
int j;
j = 0;
do{
newFolderName = temp[j];
String spoonFeeding = newFolderName;
File f = new File(spoonFeeding);
f.mkdirs();
j++;
}while (j<1);
for (int i = 0; i < count; i++) {
RenderedImage page = decoder.decodeAsRenderedImage(i);
File fileObj = new File(newFolderName+"/" + (i+1) + ".png");
System.out.println("Saving " + fileObj.getCanonicalPath());
ParameterBlock parBlock = new ParameterBlock();
parBlock.addSource(page);
parBlock.add(fileObj.toString());
parBlock.add("png");
RenderedOp renderedOp = JAI.create("filestore",parBlock);
renderedOp.dispose();
}
return image;
}
}