How do I convert a TIF to PNG in Java?

北慕城南 提交于 2019-12-17 16:13:40

问题


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

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


回答1:


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

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



回答2:


Use imageMagic java libraries like im4java, their performance and quality is much better then 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

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



回答3:


Java advanced imaging APi is a good library for image manipulations

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




回答4:


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

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)

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




回答5:


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

}


来源:https://stackoverflow.com/questions/2291358/how-do-i-convert-a-tif-to-png-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!