net.sourceforge.openutils.mgnlmedia.media.utils
Class ImageUtils

java.lang.Object
  extended by net.sourceforge.openutils.mgnlmedia.media.utils.ImageUtils

public final class ImageUtils
extends Object

Main utility class that works with images and media nodes

Version:
$Id: ImageUtils.java 3691 2011-11-06 17:48:11Z fgiust $
Author:
molaschi

Field Summary
static String RESOLUTION_PROPERTY
          Nodedata name where resolution is saved
 
Method Summary
static BufferedImage addRoundedCorners(BufferedImage original, Color backgroundColor, int radius)
          Create rounded corners on image
static boolean checkOrCreateResolution(info.magnolia.cms.core.Content media, String resolutionTarget)
          Check if the resolution for a media is already present.
static boolean checkOrCreateResolution(info.magnolia.cms.core.Content media, String resolutionTarget, String nodeDataName)
          Check if the resolution for a media is already present.
static BufferedImage createBufferedImage(info.magnolia.cms.core.NodeData image)
          Create a buffered image from the binary data stored in nodedata
static BufferedImage cropImage(BufferedImage original, int left, int top, int width, int height)
          Crop an image from left, top for width, height
static String getExtension(info.magnolia.cms.core.Content media, String resolution)
          Get file extension for a resolution stored in a media node
static BufferedImage getImageForResolution(BufferedImage original, String resolution, Map<String,String> params)
          Get image for a resolution
static String getResolutionPath(String resolution)
          Get resolution nodedata name for a given resolution string
protected static info.magnolia.cms.core.Content getResolutionsNode(info.magnolia.cms.core.Content media)
          Returns the "resolutions" node, checking for existence
static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint, boolean higherQuality)
          Convenience method that returns a scaled instance of the provided BufferedImage.
static long getStream(BufferedImage image, String extension, float quality, boolean forceProgressive, OutputStream outputstream)
          Get an inputstream for an image and the target file extension
static Point parseForSize(String res)
          Parse resolution string for required sizesuper.read(b);
static BufferedImage resizeImage(BufferedImage original, int x, int y)
          Resize an image to x,y
static BufferedImage resizeImage(BufferedImage original, int x, int y, int canvasX, int canvasY, Color background)
          Resize an image to x,y
static void saveResolution(BufferedImage image, info.magnolia.cms.core.Content saveTo, String extension, float quality, boolean forceProgressive)
          Save a resolution for an image to a node (in resolutions/res-[width]x[height]/data.jpg)
static void saveResolution(BufferedImage image, info.magnolia.cms.core.Content saveTo, String name, String extension, float quality, boolean forceProgressive)
          Save a resolution for an image to a node (in resolutions/[name]/data.jpg)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RESOLUTION_PROPERTY

public static String RESOLUTION_PROPERTY
Nodedata name where resolution is saved

Method Detail

resizeImage

public static BufferedImage resizeImage(BufferedImage original,
                                        int x,
                                        int y)
Resize an image to x,y

Parameters:
original - original image
x - new width
y - new height
Returns:
resized image

resizeImage

public static BufferedImage resizeImage(BufferedImage original,
                                        int x,
                                        int y,
                                        int canvasX,
                                        int canvasY,
                                        Color background)
Resize an image to x,y

Parameters:
original - original image
x - new width
y - new height
canvasX - canvas width
canvasY - canvas height
background - background color
Returns:
resized image

getScaledInstance

public static BufferedImage getScaledInstance(BufferedImage img,
                                              int targetWidth,
                                              int targetHeight,
                                              Object hint,
                                              boolean higherQuality)
Convenience method that returns a scaled instance of the provided BufferedImage.

Parameters:
img - the original image to be scaled
targetWidth - the desired width of the scaled instance, in pixels
targetHeight - the desired height of the scaled instance, in pixels
hint - one of the rendering hints that corresponds to RenderingHints.KEY_INTERPOLATION (e.g. RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR, RenderingHints.VALUE_INTERPOLATION_BILINEAR, RenderingHints.VALUE_INTERPOLATION_BICUBIC)
higherQuality - if true, this method will use a multi-step scaling technique that provides higher quality than the usual one-step technique (only useful in downscaling cases, where targetWidth or targetHeight is smaller than the original dimensions, and generally only when the BILINEAR hint is specified)
Returns:
a scaled version of the original BufferedImage

cropImage

public static BufferedImage cropImage(BufferedImage original,
                                      int left,
                                      int top,
                                      int width,
                                      int height)
Crop an image from left, top for width, height

Parameters:
original - original image
left - start crop point left
top - start crop point top
width - crop width
height - crop height
Returns:
resized image

addRoundedCorners

public static BufferedImage addRoundedCorners(BufferedImage original,
                                              Color backgroundColor,
                                              int radius)
Create rounded corners on image

Parameters:
original - original image
backgroundColor - optional background color
radius - corners radius
Returns:
image with rounded corners

saveResolution

public static void saveResolution(BufferedImage image,
                                  info.magnolia.cms.core.Content saveTo,
                                  String extension,
                                  float quality,
                                  boolean forceProgressive)
                           throws javax.jcr.RepositoryException,
                                  IOException
Save a resolution for an image to a node (in resolutions/res-[width]x[height]/data.jpg)

Parameters:
image - image to save
saveTo - node to save to
extension - extension
quality - image quality
forceProgressive - true to force progressive mode
Throws:
javax.jcr.RepositoryException - exception in jcr operations
IOException - exception converting image to jpg

saveResolution

public static void saveResolution(BufferedImage image,
                                  info.magnolia.cms.core.Content saveTo,
                                  String name,
                                  String extension,
                                  float quality,
                                  boolean forceProgressive)
                           throws javax.jcr.RepositoryException,
                                  IOException
Save a resolution for an image to a node (in resolutions/[name]/data.jpg)

Parameters:
image - image to save
saveTo - node to save to
name - name for this resolution
extension - extension
quality - image quality
forceProgressive - true to force progressive mode
Throws:
javax.jcr.RepositoryException - exception in jcr operations
IOException - exception converting image to jpg

getResolutionPath

public static String getResolutionPath(String resolution)
Get resolution nodedata name for a given resolution string

Parameters:
resolution - resolution string
Returns:
resolution nodedata name

getStream

public static long getStream(BufferedImage image,
                             String extension,
                             float quality,
                             boolean forceProgressive,
                             OutputStream outputstream)
                      throws IOException
Get an inputstream for an image and the target file extension

Parameters:
image - image to get the inputstream from
extension - target file extension
quality - image quality
forceProgressive - true if image has to be saved as progressive mode
Returns:
byte count
Throws:
IOException

checkOrCreateResolution

public static boolean checkOrCreateResolution(info.magnolia.cms.core.Content media,
                                              String resolutionTarget)
Check if the resolution for a media is already present. Otherwise create it

Parameters:
media - media to check the resolutoin on
resolutionTarget - target resolution
Returns:
false if resolution doesn't exist and there is a problem in generate it; true otherwise

checkOrCreateResolution

public static boolean checkOrCreateResolution(info.magnolia.cms.core.Content media,
                                              String resolutionTarget,
                                              String nodeDataName)
Check if the resolution for a media is already present. Otherwise create it

Parameters:
media - media to check the resolution on
resolutionTarget - target resolution
nodeDataName - nodedata where the image to resize is stored
Returns:
false if resolution doesn't exist and there is a problem in generate it; true otherwise

getResolutionsNode

protected static info.magnolia.cms.core.Content getResolutionsNode(info.magnolia.cms.core.Content media)
Returns the "resolutions" node, checking for existence

Parameters:
media -
Returns:

getImageForResolution

public static BufferedImage getImageForResolution(BufferedImage original,
                                                  String resolution,
                                                  Map<String,String> params)
Get image for a resolution

Parameters:
original - original image
resolution - resolution
params - parameters
Returns:
new image

getExtension

public static String getExtension(info.magnolia.cms.core.Content media,
                                  String resolution)
Get file extension for a resolution stored in a media node

Parameters:
media - media
resolution - resolution
Returns:
file extension for a resolution stored in a media node

createBufferedImage

public static BufferedImage createBufferedImage(info.magnolia.cms.core.NodeData image)
Create a buffered image from the binary data stored in nodedata

Parameters:
image - nodedata
Returns:
buffered image from the binary data stored in nodedata

parseForSize

public static Point parseForSize(String res)
Parse resolution string for required sizesuper.read(b);

Parameters:
res - resolution string
Returns:
required size parsed from given resolution string


Copyright © 2008-2011 Openmind. All Rights Reserved.