如何将多个png图像转换为单个tiff文件 [英] How do I convert multiple png images to a single tiff file

查看:272
本文介绍了如何将多个png图像转换为单个tiff文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

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

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.

推荐答案

以下是解决方案。

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

}

这篇关于如何将多个png图像转换为单个tiff文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆