Search in sources :

Example 31 with IIOImage

use of javax.imageio.IIOImage in project alliance by codice.

the class NitfPreStoragePlugin method renderToJpeg2k.

private byte[] renderToJpeg2k(final BufferedImage bufferedImage) throws IOException {
    BufferedImage imageToCompress = bufferedImage;
    if (bufferedImage.getColorModel().getNumComponents() == ARGB_COMPONENT_COUNT) {
        imageToCompress = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = imageToCompress.createGraphics();
        g.drawImage(bufferedImage, 0, 0, null);
    }
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    J2KImageWriter writer = new J2KImageWriter(new J2KImageWriterSpi());
    J2KImageWriteParam writeParams = (J2KImageWriteParam) writer.getDefaultWriteParam();
    writeParams.setLossless(false);
    writeParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    writeParams.setCompressionType("JPEG2000");
    writeParams.setCompressionQuality(0.0f);
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    writer.setOutput(ios);
    writer.write(null, new IIOImage(imageToCompress, null, null), writeParams);
    writer.dispose();
    ios.close();
    return os.toByteArray();
}
Also used : J2KImageWriterSpi(com.github.jaiimageio.jpeg2000.impl.J2KImageWriterSpi) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) J2KImageWriteParam(com.github.jaiimageio.jpeg2000.J2KImageWriteParam) BufferedImage(java.awt.image.BufferedImage) J2KImageWriter(com.github.jaiimageio.jpeg2000.impl.J2KImageWriter) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) Graphics2D(java.awt.Graphics2D) IIOImage(javax.imageio.IIOImage)

Example 32 with IIOImage

use of javax.imageio.IIOImage in project acs-aem-commons by Adobe-Consulting-Services.

the class ProgressiveJpeg method write.

/**
 * For JPEG images, this method behaves similar to {@link Layer#write(String, double, OutputStream)}. The major
 * difference is that it uses progressive encoding.
 *
 * @param layer the layer with the image to write to the output stream
 * @param quality JPEG compression quality between 0 and 1
 * @param out target output stream
 * @throws IOException if anything goes wrong
 */
public static void write(Layer layer, double quality, OutputStream out) throws IOException {
    ImageWriter writer = null;
    ImageOutputStream imageOut = null;
    try {
        ImageWriteParam iwp = new JPEGImageWriteParam(null);
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);
        iwp.setCompressionQuality((float) quality);
        writer = ImageIO.getImageWritersBySuffix("jpeg").next();
        imageOut = ImageIO.createImageOutputStream(out);
        writer.setOutput(imageOut);
        BufferedImage image = getRgbImage(layer);
        writer.write(null, new IIOImage(image, null, null), iwp);
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (imageOut != null) {
            try {
                imageOut.close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 33 with IIOImage

use of javax.imageio.IIOImage in project tess4j by nguyenq.

the class ImageIOHelper method getIIOImageList.

/**
 * Gets a list of <code>IIOImage</code> objects for an image file.
 *
 * @param imageFile input image file. It can be any of the supported
 * formats, including TIFF, JPEG, GIF, PNG, BMP, JPEG, and PDF if GPL
 * Ghostscript is installed
 * @return a list of <code>IIOImage</code> objects
 * @throws IOException
 */
public static List<IIOImage> getIIOImageList(File imageFile) throws IOException {
    File workingTiffFile = null;
    ImageReader reader = null;
    ImageInputStream iis = null;
    try {
        // convert PDF to TIFF
        if (imageFile.getName().toLowerCase().endsWith(".pdf")) {
            workingTiffFile = PdfUtilities.convertPdf2Tiff(imageFile);
            imageFile = workingTiffFile;
        }
        List<IIOImage> iioImageList = new ArrayList<IIOImage>();
        String imageFileName = imageFile.getName();
        String imageFormat = imageFileName.substring(imageFileName.lastIndexOf('.') + 1);
        if (imageFormat.matches("(pbm|pgm|ppm)")) {
            imageFormat = "pnm";
        } else if (imageFormat.matches("(jp2|j2k|jpf|jpx|jpm)")) {
            imageFormat = "jpeg2000";
        }
        Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFormat);
        if (!readers.hasNext()) {
            throw new RuntimeException(JAI_IMAGE_READER_MESSAGE);
        }
        reader = readers.next();
        iis = ImageIO.createImageInputStream(imageFile);
        reader.setInput(iis);
        int imageTotal = reader.getNumImages(true);
        for (int i = 0; i < imageTotal; i++) {
            // IIOImage oimage = new IIOImage(reader.read(i), null, reader.getImageMetadata(i));
            IIOImage oimage = reader.readAll(i, reader.getDefaultReadParam());
            iioImageList.add(oimage);
        }
        return iioImageList;
    } finally {
        try {
            if (iis != null) {
                iis.close();
            }
            if (reader != null) {
                reader.dispose();
            }
        } catch (Exception e) {
        // ignore
        }
        if (workingTiffFile != null && workingTiffFile.exists()) {
            workingTiffFile.delete();
        }
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ArrayList(java.util.ArrayList) ImageReader(javax.imageio.ImageReader) File(java.io.File) IIOInvalidTreeException(javax.imageio.metadata.IIOInvalidTreeException) IOException(java.io.IOException) IIOImage(javax.imageio.IIOImage)

Example 34 with IIOImage

use of javax.imageio.IIOImage 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 35 with IIOImage

use of javax.imageio.IIOImage 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)

Aggregations

IIOImage (javax.imageio.IIOImage)123 ImageWriter (javax.imageio.ImageWriter)92 ImageWriteParam (javax.imageio.ImageWriteParam)79 ImageOutputStream (javax.imageio.stream.ImageOutputStream)79 BufferedImage (java.awt.image.BufferedImage)66 File (java.io.File)45 IOException (java.io.IOException)41 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 IIOMetadata (javax.imageio.metadata.IIOMetadata)32 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)25 ByteArrayInputStream (java.io.ByteArrayInputStream)22 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)17 ImageReader (javax.imageio.ImageReader)16 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)16 Graphics2D (java.awt.Graphics2D)14 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)14 Test (org.junit.Test)14 FileOutputStream (java.io.FileOutputStream)13 Iterator (java.util.Iterator)11 RenderedImage (java.awt.image.RenderedImage)10