Class CachedImage

java.lang.Object
com.iizix.image.CachedImage

public class CachedImage extends Object
The cached image class contains a loaded image in memory and can be used to perform various operations on it.
Author:
Christopher Mindus
  • Constructor Details

    • CachedImage

      public CachedImage(BufferedImage bufferedImage, String fileName)
      Wraps an image already in memory, for an image that never was a file of its own, e.g. the bytes of an App icon read from an application jar (CC-PWA-V5B-MANIFEST). No file data is kept and nothing is read: getData() returns null.
      Parameters:
      bufferedImage - The buffered image, used as is (not copied).
      fileName - The file name to report, with an extension such as "icon.png"; it names the image and the suffixed names of scale(double,String).
      Throws:
      NullPointerException - If bufferedImage or fileName is null.
    • CachedImage

      public CachedImage(File file) throws IOException
      Loads an image from a File without keeping the file data.

      An error is logged in the server in case of an exception not being a direct {@link IOException}.

      Parameters:
      file - The file.
      Throws:
      FileNotFoundException - If the file is not found.
      IOException - For I/O errors (could also wrap e.g. OutOfMemoryError or other exceptions).
    • CachedImage

      public CachedImage(File file, boolean keepFileData) throws IOException
      Loads an image from a File.

      An error is logged in the server in case of an exception not being a direct {@link IOException}.

      Parameters:
      file - The file.
      keepFileData - Flag to keep the original file data (please note that this requires more memory).
      Throws:
      FileNotFoundException - If the file is not found.
      IOException - For I/O errors (could also wrap e.g. OutOfMemoryError or other exceptions).
  • Method Details

    • getBufferedImage

      public BufferedImage getBufferedImage()
      Gets the buffered image.
      Returns:
      The buffered image.
    • getFileName

      public String getFileName()
      Gets the original file name.
      Returns:
      The original file name.
    • getFileExtension

      public String getFileExtension()
      Gets the file extension.
      Returns:
      The file extension without the "dot", null if none is found.
    • getSize

      public Size getSize()
      Gets the image size.
      Returns:
      The size of the image.
    • getData

      public byte[] getData()
      Gets a copy of the original (file) data.
      Returns:
      The byte array, or null in case of an OutOfMemoryException or that the file data is not kept in this instance. An error is logged in the server in case of a problem.
    • writeTo

      public void writeTo(File file) throws IOException
      Writes the image data to a file. The data written is the cached data if present, otherwise the data from the buffered image.
      Parameters:
      file - The new file name, including a potential new file extension.
      Throws:
      IllegalArgumentException - If the file name does is not present.
      IOException - For I/O errors.
    • scale

      public CachedImage scale(double dpr, String suffix)
      Generates a new cached image scaled to the request dpr (100% = 1d = 1 DPR, 200% = 2 DPR, etc.).
      Parameters:
      dpr - The desired DPR.
      suffix - The suffix to append to the file name.
      Returns:
      A new cached image.
      Throws:
      IllegalArgumentException - If dpr is zero or negative.
    • scale

      public CachedImage scale(int maxWidth, int maxHeight, String suffix)
      Generates a new cached image thumbnail that has the maximum width and height. The scaling factor is calculated to fit, i.e. the aspect/ratio of the image is kept.
      Parameters:
      maxWidth - The width.
      maxHeight - The height.
      Returns:
      A new cached image.
      Throws:
      IllegalArgumentException - If the width or height is less than 1.