如何在 Java 中将 TIF 转换为 PNG?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2291358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:44:35  来源:igfitidea点击:

How do I convert a TIF to PNG in Java?

javaimagepngtiffimage-conversion

提问by James McMahon

Under Java what is the best way to go about converting an TIF file to a PNG?

在 Java 下,将 TIF 文件转换为 PNG 的最佳方法是什么?

Simplicity is preferable, but if the simplest way is to use a third party library then I would consider that solution.

简单是可取的,但如果最简单的方法是使用第三方库,那么我会考虑该解决方案。

采纳答案by Jonathan Feinberg

First, install JAI. Then install JAI/ImageIO. Then do

首先,安装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"));
}

回答by MLefrancois

Java advanced imaging APi is a good library for image manipulations

Java高级成像API是一个很好的图像处理库

http://java.sun.com/products/java-media/jai/iio.html

http://java.sun.com/products/java-media/jai/iio.html

回答by giladbu

Use imageMagicjava libraries like im4java, their performance and quality is much better then JAI

使用像im4java这样的imageMagicjava 库,它们的性能和质量比 JAI 好得多

for example:

例如:

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()});
}

maven dependency for im4java is

im4java 的 maven 依赖项是

<dependency>
  <groupId>im4java</groupId>
  <artifactId>im4java</artifactId>
  <version>0.98.0</version>
</dependency>

回答by wsx22

Download JIMI Software Development Kit jimi1_0.zip and set JimiProClasses.zip to your classpath

下载 JIMI Software Development Kit jimi1_0.zip 并将 JimiProClasses.zip 设置为您的类路径

http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-client-419417.html#7259-jimi_sdk-1.0-oth-JPR

http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-client-419417.html#7259-jimi_sdk-1.0-oth-JPR

JIMI is older java image library, but it is easy to use and there is no platform dependent code (no native executables, can use it like standard jar)

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();

}

}

where inMimeType and outMimeType are graphics formats mimetypes

其中 inMimeType 和 outMimeType 是图形格式 mimetypes

回答by hardcess

maybe you can use this code, works for me

也许你可以使用这个代码,对我有用

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;
        }

}