ProgramixGenericLib v5.0.1

com.programix.gui.image
Class ImageTools

java.lang.Object
  extended by com.programix.gui.image.ImageTools

public class ImageTools
extends Object

Collection of functions useful for working with Image and BufferedImage.

Author:
Paul Hyde

Nested Class Summary
static class ImageTools.ImageException
          Thrown to indicate that there was a problem reading, writing, converting, or other problems working with an Image or BufferedImage.
static class ImageTools.ImageNotFoundException
           
 
Field Summary
static String JPEG_FORMAT
           
static String PNG_FORMAT
           
 
Method Summary
static void closeQuietly(ImageInputStream iis)
           
static void closeQuietly(ImageOutputStream ios)
           
static BufferedImage convertToBuffered(Image image)
          Converts the passed Image into a BufferedImage if necessary.
static byte[] createBytes(RenderedImage image, String format)
           
static BufferedImage createImage(byte[] formattedImageBytes)
          Creates a BufferedImage by reading from the specified byte[].
static BufferedImage createImage(File source)
          Creates a BufferedImage by reading from the specified file.
static BufferedImage createImage(InputStream source)
          Creates a BufferedImage by reading from the specified source which should be presenting the data in a PNG, JPEG, GIF, etc.
static BufferedImage createImage(URL imageUrl)
          Creates a BufferedImage by reading from the specified URL.
static BufferedImage createImageFromFile(String filename)
          Creates a BufferedImage by reading from the specified file.
static BufferedImage createImageFromResource(String resourceLocation)
          Creates a BufferedImage by reading from the specified resource.
static BufferedImage createImageFromResource(String resourceLocation, Class<?> searchStartPoint)
          Creates a BufferedImage by reading from the specified resource.
static byte[] createJPEGBytes(RenderedImage image)
           
static byte[] createPNGBytes(RenderedImage image)
           
static void write(RenderedImage image, OutputStream destination, String format)
          Writes out the specified image to the specified stream in the specified format.
static void writeJPEG(RenderedImage image, OutputStream destination)
           
static void writePNG(RenderedImage image, OutputStream destination)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PNG_FORMAT

public static final String PNG_FORMAT
See Also:
Constant Field Values

JPEG_FORMAT

public static final String JPEG_FORMAT
See Also:
Constant Field Values
Method Detail

createImage

public static BufferedImage createImage(InputStream source)
                                 throws ImageTools.ImageException
Creates a BufferedImage by reading from the specified source which should be presenting the data in a PNG, JPEG, GIF, etc. format. The InputStream passed in is closed by this method.

Parameters:
source - origin of the image; stream is always closed by this method
Returns:
the decoded image
Throws:
ImageTools.ImageException - if there is no support for the image format or if there is any problem reading from the stream.

createImage

public static BufferedImage createImage(File source)
                                 throws ImageTools.ImageNotFoundException,
                                        ImageTools.ImageException
Creates a BufferedImage by reading from the specified file. The image's data must be in a PNG, JPEG, GIF, or other format that can be read by ImageIO.

Parameters:
source - origin of the image
Returns:
the decoded image
Throws:
ImageTools.ImageNotFoundException - if the specified file can not be found.
ImageTools.ImageException - if there is no support for the image format or if there is any other problem reading from the stream.

createImageFromFile

public static BufferedImage createImageFromFile(String filename)
                                         throws ImageTools.ImageNotFoundException,
                                                ImageTools.ImageException
Creates a BufferedImage by reading from the specified file. The image's data must be in a PNG, JPEG, GIF, or other format that can be read by ImageIO.

Parameters:
filename - origin of the image
Returns:
the decoded image
Throws:
ImageTools.ImageNotFoundException - if the specified file can not be found.
ImageTools.ImageException - if there is no support for the image format or if there is any other problem reading from the stream.

createImage

public static BufferedImage createImage(URL imageUrl)
                                 throws ImageTools.ImageException
Creates a BufferedImage by reading from the specified URL. The image's data must be in a PNG, JPEG, GIF, or other format that can be read by ImageIO.

Parameters:
imageUrl - origin of the image
Returns:
the decoded image
Throws:
ImageTools.ImageException - if there is no support for the image format or if there is any other problem reading from the stream.

createImageFromResource

public static BufferedImage createImageFromResource(String resourceLocation,
                                                    Class<?> searchStartPoint)
                                             throws ImageTools.ImageNotFoundException,
                                                    ImageTools.ImageException
Creates a BufferedImage by reading from the specified resource. The image's data must be in a PNG, JPEG, GIF, or other format that can be read by ImageIO. The underlying InputStream comes from the specified resourceLocation. This method is suitable for retrieving resources from JAR files, WAR files, and CLASSPATH's. The getResource(String path) method on the Class object passed in is used to locate the requested resource. See IOTools.getUrlForResource(String, Class) for the full explanation of how the search for the URL is performed.

Works like this (with IOExceptions converted to ImageExceptions):

 return createImage(IOTools.getInputStreamForResource(
     resourceLocation, searchStartPoint));
 

Parameters:
resourceLocation - the path to the resource
searchStartPoint - the Class to invoke getResource on. If null, IOTools.class is used.
Returns:
the decoded image
Throws:
ImageTools.ImageNotFoundException - if the specified resource can not be found.
ImageTools.ImageException - if there is no support for the image format or if there is any other problem reading from the stream.

createImageFromResource

public static BufferedImage createImageFromResource(String resourceLocation)
                                             throws ImageTools.ImageNotFoundException,
                                                    ImageTools.ImageException
Creates a BufferedImage by reading from the specified resource. The image's data must be in a PNG, JPEG, GIF, or other format that can be read by ImageIO. The underlying InputStream comes from the specified resourceLocation. This method is suitable for retrieving resources from JAR files, WAR files, and CLASSPATH's. See IOTools.getUrlForResource(String, Class) for the full explanation of how the search for the URL is performed.

Works like this (with IOExceptions converted to ImageExceptions):

 return createImage(
 IOTools.getInputStreamForResource(resourceLocation));
 

Parameters:
resourceLocation - the path to the resource
Returns:
the decoded image
Throws:
ImageTools.ImageNotFoundException - if the specified resource can not be found.
ImageTools.ImageException - if there is no support for the image format or if there is any other problem reading from the stream.

createImage

public static BufferedImage createImage(byte[] formattedImageBytes)
                                 throws ImageTools.ImageException
Creates a BufferedImage by reading from the specified byte[]. The image's data must be in a PNG, JPEG, GIF, or other format that can be read by ImageIO.

Parameters:
formattedImageBytes - origin of the image
Returns:
the decoded image
Throws:
ImageTools.ImageException - if there is no support for the image format or if there is any other problem reading from the stream.

write

public static void write(RenderedImage image,
                         OutputStream destination,
                         String format)
                  throws ImageTools.ImageException
Writes out the specified image to the specified stream in the specified format. The OutputStream pass to this method is not closed.

Parameters:
image - image to convert to bytes
destination - stream for formatted bytes to be sent; this is flushed, but not closed by this method.
format - image format for output, like PNG_FORMAT or JPEG_FORMAT or another other format supported for writing.
Throws:
ImageTools.ImageException - if there are any problems writing out the formatted bytes.

writePNG

public static void writePNG(RenderedImage image,
                            OutputStream destination)
                     throws ImageTools.ImageException
Throws:
ImageTools.ImageException

writeJPEG

public static void writeJPEG(RenderedImage image,
                             OutputStream destination)
                      throws ImageTools.ImageException
Throws:
ImageTools.ImageException

createBytes

public static byte[] createBytes(RenderedImage image,
                                 String format)
                          throws ImageTools.ImageException
Throws:
ImageTools.ImageException

createPNGBytes

public static byte[] createPNGBytes(RenderedImage image)
                             throws ImageTools.ImageException
Throws:
ImageTools.ImageException

createJPEGBytes

public static byte[] createJPEGBytes(RenderedImage image)
                              throws ImageTools.ImageException
Throws:
ImageTools.ImageException

convertToBuffered

public static BufferedImage convertToBuffered(Image image)
                                       throws IllegalArgumentException
Converts the passed Image into a BufferedImage if necessary. That is, is a BufferedImage is passed in, then that same object is simple returned. If null is passed in, then an IllegalArgumentException is thrown. If the passed image is not a BufferedImage, a new BufferedImage is created and the original image is drawn onto it.

Throws:
IllegalArgumentException

closeQuietly

public static void closeQuietly(ImageInputStream iis)

closeQuietly

public static void closeQuietly(ImageOutputStream ios)

ProgramixGenericLib v5.0.1

Copyright © 2001-2009 Programix Incorporated. All rights reserved. ProgramixGenericLib is free and is OSI Certified Open Source Software under the BSD license.