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

时间:2022-09-26 11:05:09

I have a byte array of several images in the png format. I have to convert this to a tiff file and then into the corresponding byte array. This tiff file will hold multiple images.

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

I have searced a lot, but I haven't been successful. The catch is. i have to do this in java only!! :) Can anyone provide some insight as regards my issue?

我已经接受了很多,但我没有成功。问题是。我只能在java中这样做!! :)任何人都可以就我的问题提供一些见解吗?

I wont benefit from ImageMagick etc, because i have a server component that handles this conversion, and then saves it in the backend as a tiff. The client gies me a byte array which would translate into a png image.

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

5 个解决方案

#1


Below is the solution.

以下是解决方案。

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

}

#2


The JMagick library is a Java interface to ImageMagick, and is likely to be what you want.

JMagick库是ImageMagick的Java接口,很可能是你想要的。

#3


Are you trying to do this programatically? You might want to look at ImageMagick (http://www.imagemagick.org/script/index.php)

您是否正在尝试以编程方式执行此操作?你可能想看看ImageMagick(http://www.imagemagick.org/script/index.php)

If you are writing a shell script of some sort (bash, batch) you can invoke the convert command (which is part of that package.) Otherwise, all of the functionality of ImageMagick is available through libraries (C, perl, etc) which you can try linking in with your program.

如果你正在编写某种shell脚本(bash,batch),你可以调用convert命令(它是该包的一部分。)否则,ImageMagick的所有功能都可以通过库(C,perl等)获得。你可以尝试链接你的程序。

#4


I've used ghostscript to create multi-page PDF files, you might be able to do something similar. My particular application had a number of JPG files that I passed to a command line call to ghostscript on windows to create a multi-page PDF.

我使用ghostscript创建了多页PDF文件,你可以做类似的事情。我的特定应用程序有一些JPG文件,我传递给命令行调用windows上的ghostscript来创建一个多页PDF。

If I were doing this, I would start by writing the png files to disk. Next use ghostscript to create a multi-page ps file by taking all the PNG files as input, and outputing to a sinlge PostScript document. Finally, use something like ps2tif to do the final conversion. You could either automate the process by programmatically writing out a batch script or just having your app call the command line apps directly.

如果我这样做,我会先将png文件写入磁盘。接下来使用ghostscript创建一个多页ps文件,将所有PNG文件作为输入,并输出到sinlge PostScript文档。最后,使用ps2tif之类的东西进行最终转换。您可以通过以编程方式编写批处理脚本或让应用程序直接调用命令行应用程序来自动执行该过程。

This is probably easiest to do in a Unix/Linux environment, but it's doable on Win32 with the same tools.

这在Unix / Linux环境中可能是最容易做到的,但是在Win32上使用相同的工具是可行的。

#5


You should use the JAI (Java Imaging API). It is capable of reading/writing PNG's and at least capable of reading TIFF (writing should be supported too).

您应该使用JAI(Java Imaging API)。它能够读/写PNG并且至少能够读取TIFF(也应该支持写入)。

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

If writing is not supported use Lizard's Tiff Library for Java (http://www.lizardworks.com/libs.html)

如果不支持写作,请使用Lizard的Tiff Library for Java(http://www.lizardworks.com/libs.html)

In this thread you can find how to merge multiple files into one TIFF. The poster has Problems changing the TIFF metadata but this should not affect your Problem. http://forums.java.net/jive/thread.jspa?messageID=244190&tstart=0

在此主题中,您可以找到如何将多个文件合并为一个TIFF。海报有问题更改TIFF元数据但这不应该影响您的问题。 http://forums.java.net/jive/thread.jspa?messageID=244190&tstart=0

#1


Below is the solution.

以下是解决方案。

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

}

#2


The JMagick library is a Java interface to ImageMagick, and is likely to be what you want.

JMagick库是ImageMagick的Java接口,很可能是你想要的。

#3


Are you trying to do this programatically? You might want to look at ImageMagick (http://www.imagemagick.org/script/index.php)

您是否正在尝试以编程方式执行此操作?你可能想看看ImageMagick(http://www.imagemagick.org/script/index.php)

If you are writing a shell script of some sort (bash, batch) you can invoke the convert command (which is part of that package.) Otherwise, all of the functionality of ImageMagick is available through libraries (C, perl, etc) which you can try linking in with your program.

如果你正在编写某种shell脚本(bash,batch),你可以调用convert命令(它是该包的一部分。)否则,ImageMagick的所有功能都可以通过库(C,perl等)获得。你可以尝试链接你的程序。

#4


I've used ghostscript to create multi-page PDF files, you might be able to do something similar. My particular application had a number of JPG files that I passed to a command line call to ghostscript on windows to create a multi-page PDF.

我使用ghostscript创建了多页PDF文件,你可以做类似的事情。我的特定应用程序有一些JPG文件,我传递给命令行调用windows上的ghostscript来创建一个多页PDF。

If I were doing this, I would start by writing the png files to disk. Next use ghostscript to create a multi-page ps file by taking all the PNG files as input, and outputing to a sinlge PostScript document. Finally, use something like ps2tif to do the final conversion. You could either automate the process by programmatically writing out a batch script or just having your app call the command line apps directly.

如果我这样做,我会先将png文件写入磁盘。接下来使用ghostscript创建一个多页ps文件,将所有PNG文件作为输入,并输出到sinlge PostScript文档。最后,使用ps2tif之类的东西进行最终转换。您可以通过以编程方式编写批处理脚本或让应用程序直接调用命令行应用程序来自动执行该过程。

This is probably easiest to do in a Unix/Linux environment, but it's doable on Win32 with the same tools.

这在Unix / Linux环境中可能是最容易做到的,但是在Win32上使用相同的工具是可行的。

#5


You should use the JAI (Java Imaging API). It is capable of reading/writing PNG's and at least capable of reading TIFF (writing should be supported too).

您应该使用JAI(Java Imaging API)。它能够读/写PNG并且至少能够读取TIFF(也应该支持写入)。

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

If writing is not supported use Lizard's Tiff Library for Java (http://www.lizardworks.com/libs.html)

如果不支持写作,请使用Lizard的Tiff Library for Java(http://www.lizardworks.com/libs.html)

In this thread you can find how to merge multiple files into one TIFF. The poster has Problems changing the TIFF metadata but this should not affect your Problem. http://forums.java.net/jive/thread.jspa?messageID=244190&tstart=0

在此主题中,您可以找到如何将多个文件合并为一个TIFF。海报有问题更改TIFF元数据但这不应该影响您的问题。 http://forums.java.net/jive/thread.jspa?messageID=244190&tstart=0