Example usage for javax.imageio ImageWriteParam MODE_EXPLICIT

List of usage examples for javax.imageio ImageWriteParam MODE_EXPLICIT

Introduction

In this page you can find the example usage for javax.imageio ImageWriteParam MODE_EXPLICIT.

Prototype

int MODE_EXPLICIT

To view the source code for javax.imageio ImageWriteParam MODE_EXPLICIT.

Click Source Link

Document

A constant value that may be passed into methods such as setTilingMode or setCompressionMode to enable a feature for future writes.

Usage

From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java

private static ImageWriteParam getOptimizedImageWriteParam(ImageWriter writer, Size scaledSize) {
    ImageWriteParam iwp = writer.getDefaultWriteParam();
    try {//w  ww .  jav  a  2 s .c  om
        if (iwp.canWriteCompressed()) {
            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            int maxSize = Math.max(scaledSize.getWidth(), scaledSize.getHeight());
            if (maxSize <= 50) {
                iwp.setCompressionQuality(0.95f);
            } else if (maxSize <= 100) {
                iwp.setCompressionQuality(0.90f);
            } else if (maxSize <= 200) {
                iwp.setCompressionQuality(0.85f);
            } else if (maxSize <= 500) {
                iwp.setCompressionQuality(0.80f);
            } else {
                iwp.setCompressionQuality(0.75f);
            }
        }
    } catch (Exception e) {
        //bmp can be compressed but don't allow it!!!
        return writer.getDefaultWriteParam();
    }
    return iwp;
}

From source file:org.onehippo.forge.gallerymagick.core.command.ScalrProcessorUtils.java

/**
 * Resize the given image {@code sourceFile} with resizing it to {@code width} and {@code height}
 * and store the resized image to {@code targetFile}, with appending {@code extraOptions} in the command line if provided.
 * @param sourceFile source image file//w w  w  .  j  a  va  2 s. c  o  m
 * @param targetFile target image file
 * @param dimension image dimension
 * @param extraOptions extra command line options
 * @throws IOException if IO exception occurs
 */
public static void resizeImage(File sourceFile, File targetFile, ImageDimension dimension,
        String... extraOptions) throws IOException {
    if (dimension == null) {
        throw new IllegalArgumentException("Invalid dimension: " + dimension);
    }

    ImageReader reader = null;
    ImageWriter writer = null;

    try {
        reader = getImageReader(sourceFile);

        if (reader == null) {
            throw new IllegalArgumentException(
                    "Unsupported image file name extension for reading: " + sourceFile);
        }

        writer = getImageWriter(targetFile);

        if (writer == null) {
            throw new IllegalArgumentException(
                    "Unsupported image file name extension for writing: " + targetFile);
        }

        BufferedImage sourceImage = reader.read(0);
        BufferedImage resizedImage = Scalr.resize(sourceImage, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC,
                dimension.getWidth(), dimension.getHeight());

        final ImageWriteParam writeParam = writer.getDefaultWriteParam();

        if (writeParam.canWriteCompressed()) {
            String[] compressionTypes = writeParam.getCompressionTypes();

            if (compressionTypes != null && compressionTypes.length > 0) {
                writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                writeParam.setCompressionType(compressionTypes[0]);
                writeParam.setCompressionQuality(1.0f);
            }
        }

        final IIOImage iioImage = new IIOImage(resizedImage, null, null);
        writer.write(null, iioImage, writeParam);
    } finally {
        if (reader != null) {
            reader.dispose();
        }
        if (writer != null) {
            writer.dispose();
        }
    }
}

From source file:osmb.program.tiledatawriter.TileImageJpegDataWriter.java

@Override
public void initialize() {
    if (log.isTraceEnabled()) {
        String s = "Available JPEG image writers:";
        Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg");
        while (writers.hasNext()) {
            ImageWriter w = writers.next();
            s += "\n\t" + w.getClass().getName();
        }//  w w w .  j  av a 2s . c  om
        log.trace(s);
    }
    jpegImageWriter = ImageIO.getImageWritersByFormatName("jpeg").next();
    if (jpegImageWriter == null)
        throw new NullPointerException("Unable to create a JPEG image writer");
    jpegImageWriter.addIIOWriteWarningListener(ImageWriterWarningListener.INSTANCE);
    log.debug("Used JPEG image writer: " + jpegImageWriter.getClass().getName());
    iwp = jpegImageWriter.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionQuality(jpegCompressionLevel);
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static void writeJpeg(BufferedImage img, float quality, File file) throws IOException {
    if (img == null) {
        return;//w w w.  j  a  va  2s .c  o m
    }

    float q = quality;
    if (q < 0) {
        q = 0;
    }
    if (q > 1) {
        q = 1;
    }

    Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
    ImageWriter writer = (ImageWriter) iter.next();
    ImageWriteParam iwp = writer.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionQuality(q); // float between 0 and 1
                                  // 1 specifies minimum compression and maximum quality

    FileImageOutputStream output = new FileImageOutputStream(file);
    writer.setOutput(output);
    IIOImage image = new IIOImage(img, null, null);
    writer.write(null, image, iwp);
    writer.dispose();
}

From source file:tvbrowserdataservice.file.ProgramField.java

/**
 * This Function loads an image using imageio,
 * resizes it to the Max-Size of Images in TVBrowser and
 * stores it as an compressed jpg./*from   w  w  w  .j av  a2  s . c  o m*/
 * <p/>
 * If the Image is smaller than the Max-Size, it isn't altered
 *
 * @param data Image-Data
 * @return resized Image-Data
 */
private static byte[] recreateImage(byte[] data) {
    byte[] newdata = null;
    BufferedImage image = null;
    try {
        // Read Image
        image = ImageIO.read(new ByteArrayInputStream(data));

        int curx = image.getWidth(null);
        int cury = image.getHeight(null);

        // If the Size is < than max, use the original Data to reduce compression
        // artefacts
        if ((curx <= MAX_IMAGE_SIZE_X) && (cury <= MAX_IMAGE_SIZE_Y)) {
            return data;
        }

        int newx = MAX_IMAGE_SIZE_X;
        int newy = (int) ((MAX_IMAGE_SIZE_X / (float) curx) * cury);

        if (newy > MAX_IMAGE_SIZE_Y) {
            newy = MAX_IMAGE_SIZE_Y;
            newx = (int) ((MAX_IMAGE_SIZE_Y / (float) cury) * curx);
        }

        BufferedImage tempPic = new BufferedImage(image.getWidth(null), image.getHeight(null),
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = tempPic.createGraphics();
        g2d.drawImage(image, null, null);
        g2d.dispose();

        BufferedImage newImage = UiUtilities.scaleIconToBufferedImage(tempPic, newx, newy);

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        // Find a jpeg writer
        ImageWriter writer = null;
        Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg");
        if (iter.hasNext()) {
            writer = iter.next();
        }

        if (writer != null) {
            // Prepare output file
            ImageOutputStream ios = ImageIO.createImageOutputStream(out);
            writer.setOutput(ios);

            JPEGImageWriteParam param = new JPEGImageWriteParam(null);

            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            param.setCompressionQuality(0.85f);

            // Write the image
            writer.write(null, new IIOImage(newImage, null, null), param);

            // Cleanup
            ios.flush();
            writer.dispose();
            ios.close();

            newdata = out.toByteArray();
        } else {
            mLog.severe("No JPEG-Exporter found. Image is not stored in Data");
        }

    } catch (IOException e) {
        e.printStackTrace();
        newdata = null;
    }

    return newdata;
}