magoffin.matt.ma2.image
Class ImageMediaHelper

java.lang.Object
  extended by magoffin.matt.ma2.image.ImageMediaHelper

public class ImageMediaHelper
extends java.lang.Object

Helper class for dealing with ImageIO-based images.

This class exists so the functionality can be shared between other MediaHandler implementations outside the .image package, which may need to render images from their media. Video handlers, for example, can use this class to render image previews for their video files.

Version:
$Revision: 43 $ $Date: 2007-10-21 11:10:01 +1300 (Sun, 21 Oct 2007) $
Author:
Matt Magoffin (spamsqr@msqr.us)

Constructor Summary
ImageMediaHelper()
           
 
Method Summary
 java.awt.image.BufferedImage getBufferedImage(java.awt.Image image)
          Get a BufferedImage from an Image.
 javax.imageio.ImageReader getReaderForFile(java.io.File file)
          Get an ImageReader for a File (based on file extension).
 javax.imageio.ImageReader getReaderForMIME(java.lang.String mime, java.io.InputStream in)
          Get an ImageReader for the given InputStream for the given MIME type.
 javax.imageio.ImageWriter getWriterForMIME(java.io.OutputStream out, java.lang.String mime)
          Get an ImageWriter for the given OutputStream for the given MIME type.
 javax.imageio.ImageWriter getWriterForMIME(java.lang.String mime)
          Get an image writer for a specific MIME type.
 boolean hasAlpha(java.awt.Image image)
          Returns true if the specified image has transparent pixels.
 void writeJpegStream(java.awt.image.BufferedImage image, java.lang.Long itemId, int quality, java.io.OutputStream out)
          Encode a JPEG image to an OutputStream with the specified quality.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ImageMediaHelper

public ImageMediaHelper()
Method Detail

getBufferedImage

public java.awt.image.BufferedImage getBufferedImage(java.awt.Image image)
Get a BufferedImage from an Image.

Parameters:
image - the image to get a BufferedImage for
Returns:
BufferedImage

hasAlpha

public boolean hasAlpha(java.awt.Image image)
Returns true if the specified image has transparent pixels.

Parameters:
image - the Image to test
Returns:
true if the image has transparent pixels

getWriterForMIME

public javax.imageio.ImageWriter getWriterForMIME(java.lang.String mime)
Get an image writer for a specific MIME type.

This method will be called by getWriterForMIME(OutputStream, String) and will return an ImageWriter registered for item's MIME type, as defined by MediaItem.getMime(). Media handler implementations that wish to generate an output type different than their native type can override this method.

Parameters:
mime - the MIME type to get a ImageWriter for
Returns:
an ImageWriter, or null if none available

getWriterForMIME

public javax.imageio.ImageWriter getWriterForMIME(java.io.OutputStream out,
                                                  java.lang.String mime)
Get an ImageWriter for the given OutputStream for the given MIME type.

This method will create an ImageOutputStream from the given OutputStream.

Parameters:
out - the OutputStream to encode the image to
mime - the MIME type to get an ImageWriter for
Returns:
an ImageWriter set to the OutputStream in

getReaderForFile

public javax.imageio.ImageReader getReaderForFile(java.io.File file)
Get an ImageReader for a File (based on file extension).

Parameters:
file - the file to get the reader for
Returns:
the ImageReader

getReaderForMIME

public javax.imageio.ImageReader getReaderForMIME(java.lang.String mime,
                                                  java.io.InputStream in)
Get an ImageReader for the given InputStream for the given MIME type.

This method will create an ImageInputStream from the given InputStream.

Parameters:
mime - the MIME to get an ImageReader for
in - the InputStream of the image to decode
Returns:
an ImageReader set to the InputStream in

writeJpegStream

public void writeJpegStream(java.awt.image.BufferedImage image,
                            java.lang.Long itemId,
                            int quality,
                            java.io.OutputStream out)
Encode a JPEG image to an OutputStream with the specified quality.

Parameters:
image - the image to encode as a JPEG stream
itemId - the media item ID being written (for logging only)
quality - integer between 0 and 100, with 100 being the higest quality
out - the output stream