Package de.chris_soft.utilities

Source Code of de.chris_soft.utilities.TiffUtils

/**
* NanoDoA - File based document archive
*
* Copyright (C) 2011-2012 Christian Packenius, christian.packenius@googlemail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package de.chris_soft.utilities;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStream;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
import com.itextpdf.text.pdf.codec.TiffImage;
import com.itextpdf.text.pdf.parser.PdfImageObject;

/**
* Utilities for tiff images.
* @author Christian Packenius.
*/
public class TiffUtils {
  /**
   * Converts a multiple page TIFF file into many PNG files. I'm not proud of this method, but it should work better than the
   * usage of ImageIO and JAI. Sadly it works VERY slowly...
   * @param tiffFile TIFF file.
   * @param tmpDirectory Directory for the destination files.
   * @return List of temporary PNG files.
   * @throws IOException
   */
  public static List<File> convertTiffFileToPngFiles(File tiffFile, File tmpDirectory) throws IOException {
    tmpDirectory.mkdirs();
    List<File> list = new ArrayList<File>();
    RandomAccessFileOrArray ra = new RandomAccessFileOrArray(tiffFile.getCanonicalPath());
    int pageCount = TiffImage.getNumberOfPages(ra);
    for (int i = 0; i < pageCount; ++i) {
      Image img = TiffImage.getTiffImage(ra, i + 1);
      if (img != null) {
        try {
          // First - create a single PDF page from the tiff page.
          Document document = new Document(PageSize.A4, 0.0f, 0.0f, 0.0f, 0.0f);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          PdfWriter.getInstance(document, baos);
          document.open();
          document.add(img);
          document.close();

          // Second - read the memory based PDF file and read the image. Write
          // this into PNG file.
          PdfReader reader = new PdfReader(baos.toByteArray());
          for (int j = 0; j < reader.getXrefSize(); j++) {
            PdfObject pdfobj = reader.getPdfObject(j);
            if (pdfobj != null) {
              if (pdfobj.isStream()) {
                PdfStream stream = (PdfStream) pdfobj;
                PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE);
                if (pdfsubtype != null) {
                  if (pdfsubtype.toString().equals(PdfName.IMAGE.toString())) {
                    PdfImageObject image = new PdfImageObject((PRStream) stream);
                    BufferedImage bufferedImage = image.getBufferedImage();
                    if (bufferedImage != null) {
                      String pngName = IdUtils.getUniqueID() + ".png";
                      File pngFile = new File(tmpDirectory, pngName);
                      FileOutputStream out = new FileOutputStream(pngFile);
                      ImageIO.write(bufferedImage, "png", out);
                      out.close();
                      list.add(pngFile);
                    }
                  }
                }
              }
            }
          }
          reader.close();
        } catch (Exception exception) {
          // Ignore - the page may be corrupt.
        }
      }
    }
    ra.close();
    return list;
  }
}
TOP

Related Classes of de.chris_soft.utilities.TiffUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.