Java Code Examples for javax.imageio.plugins.jpeg.JPEGImageWriteParam#setProgressiveMode()

The following examples show how to use javax.imageio.plugins.jpeg.JPEGImageWriteParam#setProgressiveMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScalablePicture.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * This static method writes the indicated renderedImage (BufferedImage)
 * to the indicated file.
 *
 * @param writeFile The File that shall receive the jpg data
 * @param renderedImage The RenderedImage (BufferedImage) to be written
 * @param jpgQuality The quality with which to compress to jpg
 */
public static void writeJpg(File writeFile, RenderedImage renderedImage, float jpgQuality) {
  Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
  ImageWriter writer = (ImageWriter) writers.next();
  JPEGImageWriteParam params = new JPEGImageWriteParam(null);
  params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  params.setCompressionQuality(jpgQuality);
  params.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
  params.setDestinationType(new ImageTypeSpecifier(java.awt.image.IndexColorModel.getRGBdefault(),
      IndexColorModel.getRGBdefault().createCompatibleSampleModel(16, 16)));

  try (ImageOutputStream ios = ImageIO.createImageOutputStream(new FileOutputStream(writeFile))) {
    writer.setOutput(ios);
    writer.write(null, new IIOImage(renderedImage, null, null), params);
    ios.close();
  } catch (IOException e) {
    // Tools.log("ScalablePicture.writeJpg caught IOException: " + e.getMessage() + "\nwhile writing " +
    // writeFile.toString());
    e.printStackTrace();
  }
  // writer = null;
  writer.dispose(); // 1.4.1 documentation says to do this.
}
 
Example 2
Source File: ImageIOJPEGImageWriter.java    From victor with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected ImageWriteParam getDefaultWriteParam(
        ImageWriter iiowriter, RenderedImage image,
        ImageWriterParams params) {
    JPEGImageWriteParam param = new JPEGImageWriteParam(iiowriter.getLocale());
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionQuality(params.getJPEGQuality());
    if (params.getCompressionMethod() != null
            && !"JPEG".equals(params.getCompressionMethod())) {
        throw new IllegalArgumentException(
                "No compression method other than JPEG is supported for JPEG output!");
    }
    if (params.getJPEGForceBaseline()) {
        param.setProgressiveMode(JPEGImageWriteParam.MODE_DISABLED);
    }
    return param;
}
 
Example 3
Source File: JPEGFilter.java    From GIFKR with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public BufferedImage apply(BufferedImage img) {

	if(compressionLevel == 0f)
		return img;
	
	try {
		ImageWriter iw = ImageIO.getImageWritersByFormatName("jpeg").next();
		JPEGImageWriteParam iwp = (JPEGImageWriteParam) iw.getDefaultWriteParam();
		iwp.setOptimizeHuffmanTables(false);
		iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
		iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
		iwp.setCompressionQuality(1f-compressionLevel);


		ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
		iw.setOutput(new MemoryCacheImageOutputStream(baos)); 

		IIOImage outputImage = new IIOImage(img, null, null);
		iw.write(null, outputImage, iwp);
		iw.dispose();
		
		baos.flush(); 
		byte[] returnImage = baos.toByteArray(); 
		baos.close();
		
		
		BufferedImage img2 = ImageIO.read(new ByteArrayInputStream(returnImage));
		
		if(img2 == null)
			throw new Exception();
		else
			img = img2;

	} catch (Exception e) {
		e.printStackTrace();
	}

	return img;
}