Example usage for javax.imageio ImageWriteParam setProgressiveMode

List of usage examples for javax.imageio ImageWriteParam setProgressiveMode

Introduction

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

Prototype

public void setProgressiveMode(int mode) 

Source Link

Document

Specifies that the writer is to write the image out in a progressive mode such that the stream will contain a series of scans of increasing quality.

Usage

From source file:ImageUtil.java

/**
 * store BufferedImage to file/*  ww w.  ja va  2  s . c  om*/
 * @param image BufferedImage
 * @param outputFile output image file
 * @param quality quality of output image
 * @return true success, else fail
 */
public static boolean storeImage(BufferedImage image, File outputFile, float quality) {
    try {
        //reconstruct folder structure for image file output
        if (outputFile.getParentFile() != null && !outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }
        if (outputFile.exists()) {
            outputFile.delete();
        }
        //get image file suffix
        String extName = "png";
        //get registry ImageWriter for specified image suffix
        Iterator writers = ImageIO.getImageWritersByFormatName(extName);
        ImageWriter imageWriter = (ImageWriter) writers.next();
        //set image output params
        ImageWriteParam params = new JPEGImageWriteParam(null);
        params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        params.setCompressionQuality(quality);
        params.setProgressiveMode(javax.imageio.ImageWriteParam.MODE_DISABLED);
        params.setDestinationType(new ImageTypeSpecifier(IndexColorModel.getRGBdefault(),
                IndexColorModel.getRGBdefault().createCompatibleSampleModel(16, 16)));
        //writer image to file
        ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputFile);
        imageWriter.setOutput(imageOutputStream);
        imageWriter.write(null, new IIOImage(image, null, null), params);
        imageOutputStream.close();
        imageWriter.dispose();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:org.gallery.web.controller.ImageController.java

public static boolean compressImg(BufferedImage src, File outfile, double d) {
    FileOutputStream out = null;//www  .j a v  a 2 s  .c om
    ImageWriter imgWrier;
    ImageWriteParam imgWriteParams;

    // ? jpg
    imgWrier = ImageIO.getImageWritersByFormatName("jpg").next();
    imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(null);
    // ??MODE_EXPLICIT
    imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);
    // ?qality?0~1
    imgWriteParams.setCompressionQuality((float) d);
    imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);
    ColorModel colorModel = ColorModel.getRGBdefault();
    // ?
    imgWriteParams.setDestinationType(
            new javax.imageio.ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16)));

    try {
        out = new FileOutputStream(outfile);
        imgWrier.reset();
        //  out?write, ImageOutputStream?
        // OutputStream
        imgWrier.setOutput(ImageIO.createImageOutputStream(out));
        // write???
        imgWrier.write(null, new IIOImage(src, null, null), imgWriteParams);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}