com.atlassian.core.util.thumbnail
Class Thumber

java.lang.Object
  extended by com.atlassian.core.util.thumbnail.Thumber

public class Thumber
extends Object

A class to create and retrieve thumbnail of images.


Nested Class Summary
static class Thumber.WidthHeightHelper
           
 
Field Summary
static List<String> THUMBNAIL_FORMATS
           
static List<String> THUMBNAIL_MIME_TYPES
           
 
Constructor Summary
Thumber()
          Legacy compatible behaviour, all thumnails generated will be of type Thumbnail.MimeType.JPG which does not support transparency.
Thumber(Thumbnail.MimeType mimeType)
          Thumbnails will be generated of the given type and, if the type permits it (PNG), preserve transparency.
 
Method Summary
 boolean checkToolkit()
           
 Thumber.WidthHeightHelper determineScaleSize(int maxWidth, int maxHeight, int imageWidth, int imageHeight)
           
 Image getImage(File file)
          Deprecated. since 4.6.3. Use getImage(java.io.File, com.google.common.base.Predicate) instead so that you can supply a predicate to (for example) check that the image has reasonable dimensions that are unlikely to cause an OutOfMemoryError
 BufferedImage getImage(File file, com.google.common.base.Predicate<ReusableBufferedInputStream> predicate)
          Retrieves an image for the specified file if the supplied predicate is met.
 BufferedImage getImage(InputStream is)
          Deprecated. since 4.6.3. Use getImage(com.atlassian.core.util.ReusableBufferedInputStream, com.google.common.base.Predicate) instead so that you can supply a predicate to (for example) check that the image has reasonable dimensions that are unlikely to cause an OutOfMemoryError
 BufferedImage getImage(ReusableBufferedInputStream is, com.google.common.base.Predicate<ReusableBufferedInputStream> predicate)
          Reads an image from the specified input stream if the supplied predicate is met.
static List<String> getThumbnailFormats()
           
static List<String> getThumbnailMimeTypes()
           
 boolean isFileSupportedImage(File file)
           
 boolean isFileSupportedImage(InputStream inputStream)
           
 Thumbnail retrieveOrCreateThumbNail(File originalFile, File thumbnailFile, int maxWidth, int maxHeight, long thumbnailId)
          Retrieves an existing thumbnail, or creates a new one.
 Thumbnail retrieveOrCreateThumbNail(File originalFile, File thumbnailFile, int maxWidth, int maxHeight, long thumbnailId, com.google.common.base.Predicate<Dimensions> rasterBasedRenderingPredicate)
          Retrieves an existing thumbnail, or creates a new one.
 Thumbnail retrieveOrCreateThumbNail(InputStream originalFileStream, String fileName, File thumbnailFile, int maxWidth, int maxHeight, long thumbnailId)
          Retrieves an existing thumbnail, or creates a new one.
 Thumbnail retrieveOrCreateThumbNail(InputStream originalFileStream, String fileName, File thumbnailFile, int maxWidth, int maxHeight, long thumbnailId, com.google.common.base.Predicate<Dimensions> rasterBasedRenderingThreshold)
          Retrieves an existing thumbnail, or creates a new one.
 BufferedImage scaleImage(Image imageToScale, Thumber.WidthHeightHelper newDimensions)
          This method should take BufferedImage argument, but takes just Image for backward compatibility (so that the client code can stay intact).
 BufferedImage scaleImageOld(Image imageToScale, Thumber.WidthHeightHelper newDimensions)
          Deprecated. 
 void setEncodingQuality(float f)
          Set the default encoding quality used by the thumber to encode jpegs.
 void storeImage(BufferedImage scaledImage, File file)
          Store Image in format defined by constructor
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

THUMBNAIL_MIME_TYPES

public static final List<String> THUMBNAIL_MIME_TYPES

THUMBNAIL_FORMATS

public static final List<String> THUMBNAIL_FORMATS
Constructor Detail

Thumber

public Thumber()
Legacy compatible behaviour, all thumnails generated will be of type Thumbnail.MimeType.JPG which does not support transparency.


Thumber

public Thumber(Thumbnail.MimeType mimeType)
Thumbnails will be generated of the given type and, if the type permits it (PNG), preserve transparency.

Parameters:
mimeType - the type of all thumbnails generated and retrieved.
Method Detail

getThumbnailMimeTypes

public static List<String> getThumbnailMimeTypes()

getThumbnailFormats

public static List<String> getThumbnailFormats()

checkToolkit

public boolean checkToolkit()
Returns:
True if the AWT default toolkit exists, false (with an error logged) if it does not.

retrieveOrCreateThumbNail

public Thumbnail retrieveOrCreateThumbNail(File originalFile,
                                           File thumbnailFile,
                                           int maxWidth,
                                           int maxHeight,
                                           long thumbnailId)
                                    throws MalformedURLException
Retrieves an existing thumbnail, or creates a new one.

Parameters:
originalFile - The file which is being thumbnailed.
thumbnailFile - The location of the existing thumbnail (if it exists), or the location to create a new thumbnail.
maxWidth - The max width of the thumbnail.
maxHeight - The max height of the thumbnail.
Throws:
MalformedURLException

retrieveOrCreateThumbNail

public Thumbnail retrieveOrCreateThumbNail(File originalFile,
                                           File thumbnailFile,
                                           int maxWidth,
                                           int maxHeight,
                                           long thumbnailId,
                                           com.google.common.base.Predicate<Dimensions> rasterBasedRenderingPredicate)
                                    throws MalformedURLException
Retrieves an existing thumbnail, or creates a new one. Creating of Thumbnail image will be processed in memory if specified rasterBasedRenderingPredicate is match

Parameters:
originalFile - The file which is being thumbnailed.
thumbnailFile - The location of the existing thumbnail (if it exists), or the location to create a new thumbnail.
maxWidth - The max width of the thumbnail.
maxHeight - The max height of the thumbnail.
Throws:
MalformedURLException

storeImage

public void storeImage(BufferedImage scaledImage,
                       File file)
                throws FileNotFoundException
Store Image in format defined by constructor

Parameters:
scaledImage -
file -
Throws:
FileNotFoundException

scaleImage

public BufferedImage scaleImage(Image imageToScale,
                                Thumber.WidthHeightHelper newDimensions)
This method should take BufferedImage argument, but takes just Image for backward compatibility (so that the client code can stay intact). Normally anyway a BufferedImage instance will be provided and the image will be directly processed without transforming it to BufferedImage first.

Parameters:
imageToScale - image to scale (BufferedImage is welcome, other image types will be transformed to BufferedImage first)
newDimensions - desired max. dimensions
Returns:
scaled image

scaleImageOld

@Deprecated
public BufferedImage scaleImageOld(Image imageToScale,
                                              Thumber.WidthHeightHelper newDimensions)
Deprecated. 

This method provides extremely slow way to scale your image. There are generally much better alternatives (order of magnitude faster with similar quality). Consider using scaleImage(java.awt.Image, com.atlassian.core.util.thumbnail.Thumber.WidthHeightHelper) instead

Parameters:
imageToScale - input image
newDimensions - desired max. dimensions (the ratio will be kept)
Returns:
scaled image

retrieveOrCreateThumbNail

public Thumbnail retrieveOrCreateThumbNail(InputStream originalFileStream,
                                           String fileName,
                                           File thumbnailFile,
                                           int maxWidth,
                                           int maxHeight,
                                           long thumbnailId)
                                    throws MalformedURLException
Retrieves an existing thumbnail, or creates a new one.

Parameters:
originalFileStream - The image stream
fileName - Filename for specified image
thumbnailFile - The location of the existing thumbnail (if it exists), or the location to create a new thumbnail.
maxWidth - The max width of the thumbnail.
maxHeight - The max height of the thumbnail.
Throws:
MalformedURLException

retrieveOrCreateThumbNail

public Thumbnail retrieveOrCreateThumbNail(InputStream originalFileStream,
                                           String fileName,
                                           File thumbnailFile,
                                           int maxWidth,
                                           int maxHeight,
                                           long thumbnailId,
                                           com.google.common.base.Predicate<Dimensions> rasterBasedRenderingThreshold)
Retrieves an existing thumbnail, or creates a new one. Creating of Thumbnail image will be processed in memory if specified rasterBasedRenderingPredicate is match

Parameters:
originalFileStream - The image stream
fileName - Filename for specified image
thumbnailFile - The location of the existing thumbnail (if it exists), or the location to create a new thumbnail.
maxWidth - The max width of the thumbnail.
maxHeight - The max height of the thumbnail.

getImage

public Image getImage(File file)
               throws IOException
Deprecated. since 4.6.3. Use getImage(java.io.File, com.google.common.base.Predicate) instead so that you can supply a predicate to (for example) check that the image has reasonable dimensions that are unlikely to cause an OutOfMemoryError

Returns:
An Image object or null if there was no suitable ImageReader for the given data
Throws:
IOException

getImage

public BufferedImage getImage(File file,
                              @Nonnull
                              com.google.common.base.Predicate<ReusableBufferedInputStream> predicate)
                       throws IOException
Retrieves an image for the specified file if the supplied predicate is met.

Parameters:
file - File to read an image from
predicate - Predicate that needs to be met before attempting to read an image from the file (e.g. image dimensions that shouldn't be exceeded)
Returns:
An Image object or null if there was no suitable ImageReader for the given data
Throws:
FailedPredicateException - If predicate was supplied but not met
IOException

getImage

public BufferedImage getImage(InputStream is)
                       throws IOException
Deprecated. since 4.6.3. Use getImage(com.atlassian.core.util.ReusableBufferedInputStream, com.google.common.base.Predicate) instead so that you can supply a predicate to (for example) check that the image has reasonable dimensions that are unlikely to cause an OutOfMemoryError

Returns:
An Image object or null if there was no suitable ImageReader for the given data
Throws:
IOException

getImage

public BufferedImage getImage(ReusableBufferedInputStream is,
                              @Nonnull
                              com.google.common.base.Predicate<ReusableBufferedInputStream> predicate)
                       throws IOException
Reads an image from the specified input stream if the supplied predicate is met.

Parameters:
is - Input stream to read an image from
predicate - Predicate that needs to be met before attempting to read an image from the file (e.g. image dimensions that shouldn't be exceeded)
Returns:
An Image object or null if there was no suitable ImageReader for the given data
Throws:
FailedPredicateException - If predicate was supplied but not met
IOException

setEncodingQuality

public void setEncodingQuality(float f)
Set the default encoding quality used by the thumber to encode jpegs.


determineScaleSize

public Thumber.WidthHeightHelper determineScaleSize(int maxWidth,
                                                    int maxHeight,
                                                    int imageWidth,
                                                    int imageHeight)

isFileSupportedImage

public boolean isFileSupportedImage(File file)

isFileSupportedImage

public boolean isFileSupportedImage(InputStream inputStream)


Copyright © 2015 Atlassian. All rights reserved.