FileDocCategorySizeDatePackage
ImageLoaderUtil.javaAPI DocphoneME MR2 API (J2ME)10386Wed May 02 18:00:34 BST 2007com.sun.perseus.j2d

ImageLoaderUtil

public class ImageLoaderUtil extends Object
This class contains utility methods which make ImageLoader implementations easier.
version
$Id: ImageLoaderUtil.java,v 1.12 2006/04/21 06:34:56 st125089 Exp $

Fields Summary
protected RasterImage
brokenImage
Default, broken image returned if an image cannot be loaded.
protected RasterImage
loadingImage
Image used to symbolize loading state for an image.
static final String
BASE64_PREFIX
HREF prefix for all Base64 encoded images
static final int
BROKEN_IMAGE_COLOR
Color used for broken content.
static final int
LOADING_IMAGE_COLOR
Color used for loading content.
static final String
BASE64_JPG_HREF_PREFIX
HREF prefix for Base64 encoded JPEG files
static final String
BASE64_JPG_HREF_PREFIX2
HREF prefix for Base64 encoded JPEG files
static final String
BASE64_PNG_HREF_PREFIX
HREF prefix for Base64 encoded PNG files
static final String
BASE64_HREF_PREFIX
HREF prefix for Base64 encoded images with unspecified media type
static final int
BASE64_JPG_HREF_PREFIX_LENGTH
Length of the HREF prefix for Base64 encoded JPEG files
static final int
BASE64_JPG_HREF_PREFIX2_LENGTH
Length of the HREF prefix for Base64 encoded JPEG files
static final int
BASE64_PNG_HREF_PREFIX_LENGTH
Length of the HREF prefix for Base64 encoded PNG files
static final int
BASE64_HREF_PREFIX_LENGTH
Length of the HREF prefix for Base64 encoded images with unspecified media type
Constructors Summary
public ImageLoaderUtil()
Default constructor


           
      

        createPlaceholderImages();

    
Methods Summary
public RasterImagecreateImage(byte[] imageData)
Creates a RasterImage from a byte array.

param
b the byte array containing the encoded image data.

        Image img = Image.createImage(imageData, 0, imageData.length);

        return (new RasterImage(img));
    
public RasterImagecreateImage(int[] imageData, int width, int height)
Creates a RasterImage from an int array containing the pixel data

	Image img = Image.createRGBImage(imageData, width, height, true);
	return new RasterImage(img);
    
protected voidcreatePlaceholderImages()

        int[] argb = {BROKEN_IMAGE_COLOR};
        Image image = Image.createRGBImage(argb, 1, 1, true);
        brokenImage  = new RasterImage(image);

        argb = new int[] {LOADING_IMAGE_COLOR};
        image = Image.createRGBImage(argb, 1, 1, true);
        loadingImage = new RasterImage(image); 
    
public RasterImagegetBrokenImage()
Returns the image that should be used to represent an image which could not be loaded.

return
the image to represent broken uris or content.

        return brokenImage;
    
public RasterImagegetEmbededImage(java.lang.String uri)
Utility method to get an Image from Base64 encoded image data

param
uri the uri with encoded image data
return
the decoded Image or brokenImage if the encoded data is invalid and could not be decoded.

        int startAt = 0;
        if (uri.startsWith(BASE64_PNG_HREF_PREFIX)) {
            startAt = BASE64_PNG_HREF_PREFIX_LENGTH;
        } else if (uri.startsWith(BASE64_JPG_HREF_PREFIX)) {
            startAt = BASE64_JPG_HREF_PREFIX_LENGTH;
        } else if (uri.startsWith(BASE64_JPG_HREF_PREFIX2)) {
            startAt = BASE64_JPG_HREF_PREFIX2_LENGTH;
        } else if (uri.startsWith(BASE64_HREF_PREFIX)) {
            startAt = BASE64_HREF_PREFIX_LENGTH;
        } else {
            return brokenImage;
        }

        InputStream is = new Base64StringStream(uri, startAt);
        is = new Base64DecodeStream(is);

        // Base64 encodes 3 bytes with 4 characters
        byte[] data = new byte[(uri.length() - startAt) * 3 / 4];
        try {
	    Image img = Image.createImage(is);
	    return (new RasterImage(img));
        } catch (java.io.IOException ioe) {
	    ioe.printStackTrace();

            return brokenImage;
        }  catch (java.lang.IllegalArgumentException iae) {
            iae.printStackTrace();

            return brokenImage;

        } finally {
            try {
                is.close();
            } catch (java.io.IOException ioe) {
            }
        }
    
public RasterImagegetExternalImage(java.lang.String href)
Utility method to turn an image href into an Image. This assumes that the href points to an external resource. This can be tested on the href with the isDataURI method.

param
href the address from which to load the image content.
return
the loaded image or brokenImage if the image could not be loaded.


	System.out.println("getExternalImage(), Image href = " + href);

        Image img = null;
        StreamConnection c = null;
        InputStream s = null;

        try {
            c = (StreamConnection)Connector.open(href);
            s = c.openInputStream();
            img = Image.createImage(s);

        } catch (IOException ioe) {
            ioe.printStackTrace();

	    System.out.println("returning broken image");
            return brokenImage;

        } catch (IllegalArgumentException iae) {
            iae.printStackTrace();

	    System.out.println("returning broken image");
            return brokenImage;

        } finally {

	    try {
		if (s != null) 
		    s.close();
		if (c != null)
		    c.close();
	    } catch (IOException ioe) {

		//note : we have already read the image successfully. 
		//So don't fail, simply print stacktrace.

		ioe.printStackTrace();
	    }

        }
        return (new RasterImage(img));

    
public RasterImagegetLoadingImage()
Returns the image that should be used to represent an image which is loading.

return
the image to use to represent a pending loading.

        return loadingImage;
    
public booleanisDataURI(java.lang.String uri)
Returns true if the input uri is a data uri.

param
uri the URI to analyze.
return
true if the input uri is a data uri.

        return (uri.startsWith(BASE64_PREFIX));