Java BufferedImage Encode encodeAndWriteJPEGFile(File file, BufferedImage image, float quality)

Here you can find the source of encodeAndWriteJPEGFile(File file, BufferedImage image, float quality)

Description

encode And Write JPEG File

License

Open Source License

Declaration

private static void encodeAndWriteJPEGFile(File file, BufferedImage image, float quality) throws IOException 

Method Source Code


//package com.java2s;
import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;

public class Main {
    private static void encodeAndWriteJPEGFile(File file, BufferedImage image, float quality) throws IOException {
        Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(quality);
        FileImageOutputStream output = new FileImageOutputStream(file);
        writer.setOutput(output);//from  w w w .ja  v  a 2  s  . co  m
        IIOImage iioi = new IIOImage(image, null, null);
        writer.write(null, iioi, iwp);
        writer.dispose();
    }
}

Related

  1. encode(RenderedImage image, String formatName)
  2. encodeBufferedImageAsJPEG(BufferedImage bi)
  3. encodeImage(BufferedImage image)
  4. encodeImage(BufferedImage image)
  5. encodeImageToBase64(BufferedImage image)