Example usage for org.apache.commons.imaging ImageInfo getFormat

List of usage examples for org.apache.commons.imaging ImageInfo getFormat

Introduction

In this page you can find the example usage for org.apache.commons.imaging ImageInfo getFormat.

Prototype

public ImageFormat getFormat() 

Source Link

Document

Returns the image file format, ie.

Usage

From source file:com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpRenderListener.java

private byte[] processImage(byte[] imageBytes, List<Rectangle> areasToBeCleaned) {
    if (areasToBeCleaned.isEmpty()) {
        return imageBytes;
    }/*from w  w w .  ja  v  a2 s .  c o  m*/

    try {
        BufferedImage image = Imaging.getBufferedImage(imageBytes);
        ImageInfo imageInfo = Imaging.getImageInfo(imageBytes);
        cleanImage(image, areasToBeCleaned);

        // Apache can only read JPEG, so we should use awt for writing in this format
        if (imageInfo.getFormat() == ImageFormats.JPEG) {
            return getJPGBytes(image);
        } else {
            Map<String, Object> params = new HashMap<String, Object>();

            if (imageInfo.getFormat() == ImageFormats.TIFF) {
                params.put(ImagingConstants.PARAM_KEY_COMPRESSION, TiffConstants.TIFF_COMPRESSION_LZW);
            }

            return Imaging.writeImageToBytes(image, imageInfo.getFormat(), params);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}