Class FSImageWriter

java.lang.Object
org.xhtmlrenderer.util.FSImageWriter

public class FSImageWriter extends Object

Writes out BufferedImages to some output stream, like a file. Allows image writer parameters to be specified and thus controlled. Uses the java ImageIO libraries--see ImageIO and related classes, especially ImageWriter.

By default, FSImageWriter writes BufferedImages out in PNG format. The simplest possible usage is

 FSImageWriter writer = new FSImageWriter();
 writer.write(img, new File("image.png"));
 

You can set the image format in the constructor (FSImageWriter(String), and can set compression settings using various setters; this lets you create writer to reuse across a number of images, all output at the same compression level. Note that not all image formats support compression. For those that do, you may need to set more than one compression setting, in combination, for it to work. For JPG, it might look like this

      writer = new FSImageWriter("jpg");
                writer.setWriteCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                writer.setWriteCompressionType("JPEG");
                writer.setWriteCompressionQuality(.75f);
 
  • Field Details

    • imageFormat

      private final String imageFormat
    • writeCompressionQuality

      private final float writeCompressionQuality
    • writeCompressionMode

      private final int writeCompressionMode
    • writeCompressionType

      private final String writeCompressionType
  • Constructor Details

    • FSImageWriter

      public FSImageWriter()
      New image writer for the PNG image format
    • FSImageWriter

      public FSImageWriter(String imageFormat)
      New writer for a given image format, using the informal format name.
      Parameters:
      imageFormat - Informal image format name, e.g. "jpg", "png", "bmp"; usually the part that appears as the file extension.
  • Method Details

    • write

      public void write(BufferedImage image, String filePath) throws IOException
      Writes the image out to the target file, creating the file if necessary, or overwriting if it already exists.
      Parameters:
      image - Image to write.
      filePath - Path for file to write. The extension for the file name is not changed; it is up to the caller to make sure this corresponds to the image format.
      Throws:
      IOException - If the file could not be written.
    • write

      public void write(BufferedImage image, OutputStream os) throws IOException
      Writes the image out to the target file, creating the file if necessary, or overwriting if it already exists.
      Parameters:
      image - Image to write.
      os - output stream to write to
      Throws:
      IOException - If the file could not be written.
    • getImageWriteParameters

      protected ImageWriteParam getImageWriteParameters(ImageWriter writer)
      Returns the image output parameters to control the output image quality, compression, etc. By default, this uses the compression values set in this class. Override this method to get full control over the ImageWriteParam used in image output.
      Parameters:
      writer - The ImageWriter we are going to use for image output.
      Returns:
      ImageWriteParam configured for image output.
    • lookupImageWriterForFormat

      @Nonnull private ImageWriter lookupImageWriterForFormat(String imageFormat)
      Utility method to find an image writer.
      Parameters:
      imageFormat - String informal format name, "jpg"
      Returns:
      ImageWriter corresponding to that format