Class FSImageWriter


  • public class FSImageWriter
    extends java.lang.Object

    Writes out BufferedImages to some outputstream, 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 constructore (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);
     

    The method newJpegWriter(float) creates a writer for JPG images; you just need to specify the output quality. Note that for the JPG format, your image or BufferedImage shouldn't be ARGB.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEFAULT_IMAGE_FORMAT  
    • Constructor Summary

      Constructors 
      Constructor Description
      FSImageWriter()
      New image writer for the PNG image format
      FSImageWriter​(java.lang.String imageFormat)
      New writer for a given image format, using the informal format name.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static FSImageWriter newJpegWriter​(float quality)
      Convenience method for initializing a writer for the JPEG image format.
      void setWriteCompressionMode​(int mode)
      Compression mode for images to be generated from this writer.
      void setWriteCompressionQuality​(float q)
      Compression quality for images to be generated from this writer.
      void setWriteCompressionType​(java.lang.String type)
      Compression type for images to be generated from this writer.
      void write​(java.awt.image.BufferedImage bimg, java.io.OutputStream os)
      Writes the image out to the target file, creating the file if necessary, or overwriting if it already exists.
      void write​(java.awt.image.BufferedImage bimg, java.lang.String filePath)
      Writes the image out to the target file, creating the file if necessary, or overwriting if it already exists.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_IMAGE_FORMAT

        public static final java.lang.String DEFAULT_IMAGE_FORMAT
        See Also:
        Constant Field Values
    • Constructor Detail

      • FSImageWriter

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

        public FSImageWriter​(java.lang.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 Detail

      • newJpegWriter

        public static FSImageWriter newJpegWriter​(float quality)
        Convenience method for initializing a writer for the JPEG image format.
        Parameters:
        quality - level of compression, between 0 and 1; 0 is lowest, 1 is highest quality.
        Returns:
        a writer for JPEG images
      • write

        public void write​(java.awt.image.BufferedImage bimg,
                          java.lang.String filePath)
                   throws java.io.IOException
        Writes the image out to the target file, creating the file if necessary, or overwriting if it already exists.
        Parameters:
        bimg - 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:
        java.io.IOException - If the file could not be written.
      • write

        public void write​(java.awt.image.BufferedImage bimg,
                          java.io.OutputStream os)
                   throws java.io.IOException
        Writes the image out to the target file, creating the file if necessary, or overwriting if it already exists.
        Parameters:
        bimg - 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:
        java.io.IOException - If the file could not be written.
      • setWriteCompressionQuality

        public void setWriteCompressionQuality​(float q)
        Compression quality for images to be generated from this writer. See ImageWriteParam.setCompressionQuality(float) for a description of what this means and valid range of values.
        Parameters:
        q - Compression quality for image output.
      • setWriteCompressionMode

        public void setWriteCompressionMode​(int mode)
        Compression mode for images to be generated from this writer. See ImageWriteParam.setCompressionMode(int) for a description of what this means and valid range of values.
        Parameters:
        mode - Compression mode for image output.
      • setWriteCompressionType

        public void setWriteCompressionType​(java.lang.String type)
        Compression type for images to be generated from this writer. See ImageWriteParam.setCompressionType(String) for a description of what this means and valid range of values.
        Parameters:
        type - Type of compression for image output.