Search in sources :

Example 1 with TIFFImageWriteParam

use of com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam in project tess4j by nguyenq.

the class ImageIOHelper method createTiffFiles.

public static List<File> createTiffFiles(List<IIOImage> imageList, int index, int dpiX, int dpiY) throws IOException {
    List<File> tiffFiles = new ArrayList<File>();
    // Set up the writeParam
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
    tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
    // Get tif writer and set output to file
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
    if (!writers.hasNext()) {
        throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE);
    }
    ImageWriter writer = writers.next();
    // Get the stream metadata
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
    // all if index == -1; otherwise, only index-th
    for (IIOImage oimage : (index == -1 ? imageList : imageList.subList(index, index + 1))) {
        if (dpiX != 0 && dpiY != 0) {
            // Get the default image metadata.
            ImageTypeSpecifier imageType = ImageTypeSpecifier.createFromRenderedImage(oimage.getRenderedImage());
            IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, null);
            imageMetadata = setDPIViaAPI(imageMetadata, dpiX, dpiY);
            oimage.setMetadata(imageMetadata);
        }
        File tiffFile = File.createTempFile(OUTPUT_FILE_NAME, TIFF_EXT);
        ImageOutputStream ios = ImageIO.createImageOutputStream(tiffFile);
        writer.setOutput(ios);
        writer.write(streamMetadata, oimage, tiffWriteParam);
        ios.close();
        tiffFiles.add(tiffFile);
    }
    writer.dispose();
    return tiffFiles;
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ArrayList(java.util.ArrayList) TIFFImageWriteParam(com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam) ImageWriter(javax.imageio.ImageWriter) File(java.io.File) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 2 with TIFFImageWriteParam

use of com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam in project tess4j by nguyenq.

the class ImageIOHelper method mergeTiff.

/**
 * Merges multiple images into one multi-page TIFF image.
 *
 * @param inputImages an array of image files
 * @param outputTiff the output multi-page TIFF file
 * @throws IOException
 */
public static void mergeTiff(File[] inputImages, File outputTiff) throws IOException {
    if (inputImages.length == 0) {
        // if no image
        return;
    }
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
    if (!writers.hasNext()) {
        throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE);
    }
    ImageWriter writer = writers.next();
    // Set up the writeParam
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
    // tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED); // commented out to preserve original sizes
    // Get the stream metadata
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
    ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff);
    writer.setOutput(ios);
    boolean firstPage = true;
    int index = 1;
    for (File inputImage : inputImages) {
        List<IIOImage> iioImages = getIIOImageList(inputImage);
        for (IIOImage iioImage : iioImages) {
            if (firstPage) {
                writer.write(streamMetadata, iioImage, tiffWriteParam);
                firstPage = false;
            } else {
                writer.writeInsert(index++, iioImage, tiffWriteParam);
            }
        }
    }
    ios.close();
    writer.dispose();
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) TIFFImageWriteParam(com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 3 with TIFFImageWriteParam

use of com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam in project tess4j by nguyenq.

the class ImageIOHelper method createTiffFiles.

/**
 * Creates a list of TIFF image files from an image file. It basically
 * converts images of other formats to TIFF format, or a multi-page TIFF
 * image to multiple TIFF image files.
 *
 * @param imageFile input image file
 * @param index an index of the page; -1 means all pages, as in a multi-page
 * TIFF image
 * @param preserve preserve compression mode
 * @return a list of TIFF image files
 * @throws IOException
 */
public static List<File> createTiffFiles(File imageFile, int index, boolean preserve) throws IOException {
    List<File> tiffFiles = new ArrayList<File>();
    String imageFileName = imageFile.getName();
    String imageFormat = imageFileName.substring(imageFileName.lastIndexOf('.') + 1);
    Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFormat);
    if (!readers.hasNext()) {
        throw new RuntimeException(JAI_IMAGE_READER_MESSAGE);
    }
    ImageReader reader = readers.next();
    ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
    reader.setInput(iis);
    // Read the stream metadata
    // IIOMetadata streamMetadata = reader.getStreamMetadata();
    // Set up the writeParam
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
    if (!preserve) {
        // not preserve original sizes; decompress
        tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
    }
    // Get tif writer and set output to file
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
    if (!writers.hasNext()) {
        throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE);
    }
    ImageWriter writer = writers.next();
    // Read the stream metadata
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
    int imageTotal = reader.getNumImages(true);
    for (int i = 0; i < imageTotal; i++) {
        // all if index == -1; otherwise, only index-th
        if (index == -1 || i == index) {
            // BufferedImage bi = reader.read(i);
            // IIOImage oimage = new IIOImage(bi, null, reader.getImageMetadata(i));
            IIOImage oimage = reader.readAll(i, reader.getDefaultReadParam());
            File tiffFile = File.createTempFile(OUTPUT_FILE_NAME, TIFF_EXT);
            ImageOutputStream ios = ImageIO.createImageOutputStream(tiffFile);
            writer.setOutput(ios);
            writer.write(streamMetadata, oimage, tiffWriteParam);
            ios.close();
            tiffFiles.add(tiffFile);
        }
    }
    writer.dispose();
    reader.dispose();
    return tiffFiles;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ArrayList(java.util.ArrayList) TIFFImageWriteParam(com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam) ImageWriter(javax.imageio.ImageWriter) IIOImage(javax.imageio.IIOImage) IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageReader(javax.imageio.ImageReader) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 4 with TIFFImageWriteParam

use of com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam in project tess4j by nguyenq.

the class ImageIOHelper method getImageByteBuffer.

/**
 * Gets pixel data of an <code>RenderedImage</code> object.
 *
 * @param image an <code>RenderedImage</code> object
 * @return a byte buffer of pixel data
 * @throws IOException
 */
public static ByteBuffer getImageByteBuffer(RenderedImage image) throws IOException {
    // Set up the writeParam
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
    tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
    // Get tif writer and set output to file
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
    if (!writers.hasNext()) {
        throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE);
    }
    ImageWriter writer = writers.next();
    // Get the stream metadata
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream);
    writer.setOutput(ios);
    writer.write(streamMetadata, new IIOImage(image, null, null), tiffWriteParam);
    // writer.write(image);
    writer.dispose();
    // ImageIO.write(image, "tiff", ios); // this can be used in lieu of writer
    ios.seek(0);
    BufferedImage bi = ImageIO.read(ios);
    return convertImageData(bi);
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) TIFFImageWriteParam(com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 5 with TIFFImageWriteParam

use of com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam in project tess4j by nguyenq.

the class ImageIOHelper method mergeTiff.

/**
 * Merges multiple images into one multi-page TIFF image.
 *
 * @param imageList a list of <code>IIOImage</code> objects
 * @param outputTiff the output TIFF file
 * @param compressionType valid values: LZW, CCITT T.6, PackBits
 * @throws IOException
 */
public static void mergeTiff(List<IIOImage> imageList, File outputTiff, String compressionType) throws IOException {
    if (imageList == null || imageList.isEmpty()) {
        // if no image
        return;
    }
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
    if (!writers.hasNext()) {
        throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE);
    }
    ImageWriter writer = writers.next();
    // Set up the writeParam
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
    // tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED); // comment out to preserve original sizes
    if (compressionType != null) {
        tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        tiffWriteParam.setCompressionType(compressionType);
    }
    // Get the stream metadata
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
    ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff);
    writer.setOutput(ios);
    int dpiX = 300;
    int dpiY = 300;
    for (IIOImage iioImage : imageList) {
        // Get the default image metadata.
        ImageTypeSpecifier imageType = ImageTypeSpecifier.createFromRenderedImage(iioImage.getRenderedImage());
        IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, null);
        imageMetadata = setDPIViaAPI(imageMetadata, dpiX, dpiY);
        iioImage.setMetadata(imageMetadata);
    }
    IIOImage firstIioImage = imageList.remove(0);
    writer.write(streamMetadata, firstIioImage, tiffWriteParam);
    int i = 1;
    for (IIOImage iioImage : imageList) {
        writer.writeInsert(i++, iioImage, tiffWriteParam);
    }
    ios.close();
    writer.dispose();
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) TIFFImageWriteParam(com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage)

Aggregations

TIFFImageWriteParam (com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam)5 IIOImage (javax.imageio.IIOImage)5 ImageWriter (javax.imageio.ImageWriter)5 IIOMetadata (javax.imageio.metadata.IIOMetadata)5 ImageOutputStream (javax.imageio.stream.ImageOutputStream)5 File (java.io.File)3 ArrayList (java.util.ArrayList)2 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)2 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ImageReader (javax.imageio.ImageReader)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1