imagedatafactory.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 764 行 · 第 1/3 页

JAVA
764
字号
/* * * * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package javax.microedition.lcdui;import java.io.InputStream;import java.io.IOException;/** * ImageFactory implementation based on putpixel graphics library and stores * data on Java heap. */class ImageDataFactory implements AbstractImageDataFactory {    /**     * PNG Header Data     */    private static final byte[] pngHeader = new byte[] {         (byte)0x89, (byte)0x50, (byte)0x4e, (byte)0x47,         (byte)0x0d, (byte)0x0a, (byte)0x1a, (byte)0x0a,         (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0d,         (byte)0x49, (byte)0x48, (byte)0x44, (byte)0x52    };    /**     * JPEG Header Data     */    private static final byte[] jpegHeader = new byte[] {         (byte)0xff, (byte)0xd8, (byte)0xff, (byte)0xe0    };    /**     * RAW Header Data     */    private static final byte[] rawHeader = new byte[] {         (byte)0x89, (byte)0x53, (byte)0x55, (byte)0x4E    };    /** Reference to a image cache. */    private SuiteImageCache imageCache;    /** Initialize the image cache factory. */    private ImageDataFactory() {        imageCache = SuiteImageCacheFactory.getCache();    }    /**     * ImageDataFactory singleton used for <code>ImageData</code>     * creation.     */    private static ImageDataFactory imageDataFactory =        new ImageDataFactory();    /**     * Returns the singleton <code>ImageDataFactory</code> instance.     *     * @return the singleton <code>ImageDataFactory</code> instance.     */    public static AbstractImageDataFactory getImageDataFactory() {        return imageDataFactory;    }    /**     * Creates a new, mutable image for off-screen drawing. Every pixel     * within the newly created image is white.  The width and height of the     * image must both be greater than zero.     *     * @param width the width of the new image, in pixels     * @param height the height of the new image, in pixels     * @return the created image     */    public ImageData createOffScreenImageData(int width, int height) {        return new ImageData(width, height, true, true, false);    }    /**     * Creates an immutable <code>ImageData</code> from     * a <code>mutableSource ImageData</code>.     * If the source image data is mutable, an immutable copy is created and     * returned.  If the source image data is immutable, the implementation may     * simply return it without creating a new image.  If an immutable source     * image data contains transparency information,     * this information is copied to the new image data unchanged.     *     * <p> This method is useful for placing the contents of mutable images     * into <code>Choice</code> objects.  The application can create     * an off-screen image     * using the     * {@link #createImage(int, int) createImage(w, h)}     * method, draw into it using a <code>Graphics</code> object     * obtained with the     * {@link #getGraphics() getGraphics()}     * method, and then create an immutable copy of it with this method.     * The immutable copy may then be placed into <code>Choice</code>     * objects. </p>     *     * @param mutableSource the source mutable image to be copied     * @return the new immutable image data     *     * @throws NullPointerException if <code>source</code> is <code>null</code>     */    public ImageData createImmutableCopy(ImageData mutableSource) {        int width  = mutableSource.getWidth();        int height = mutableSource.getHeight();        int length = width * height * 2;        return  new ImageData(width, height, false,                              mutableSource.getPixelData());    }    /**     * Creates an immutable <code>ImageData</code>     * from decoded image data obtained from the     * named resource.  The name parameter is a resource name as defined by     * {@link Class#getResourceAsStream(String)     * Class.getResourceAsStream(name)}.  The rules for resolving resource     * names are defined in the     * <a href="../../../java/lang/package-summary.html">     * Application Resource Files</a> section of the     * <code>java.lang</code> package documentation.     *     * @param name the name of the resource containing the image data in one of     * the supported image formats     * @return the created image data     * @throws NullPointerException if <code>name</code> is <code>null</code>     * @throws java.io.IOException if the resource does not exist,     * the data cannot     * be loaded, or the image data cannot be decoded     */    public ImageData createResourceImageData(String name) throws IOException {        ImageData data = new ImageData();        /*         * Load native image data from cache and create         * image, if available. If image is not cached,         * proceed to load and create image normally.         */        if (!loadCachedImage(data, name)) {            createImageFromStream(data,                                  ImageData.class.getResourceAsStream(name));        }        return data;    }    /**     * Creates an immutable <code>ImageData</code>     * which is decoded from the data stored in     * the specified byte array at the specified offset and length. The data     * must be in a self-identifying image file format supported by the     * implementation, such as <a href="#PNG">PNG</A>.     *     * <p>The <code>imageoffset</code> and <code>imagelength</code>     * parameters specify a range of     * data within the <code>imageData</code> byte array. The     * <code>imageOffset</code> parameter     * specifies the     * offset into the array of the first data byte to be used. It must     * therefore lie within the range     * <code>[0..(imageData.length-1)]</code>. The     * <code>imageLength</code>     * parameter specifies the number of data bytes to be used. It must be a     * positive integer and it must not cause the range to extend beyond     * the end     * of the array. That is, it must be true that     * <code>imageOffset + imageLength &lt; imageData.length</code>. </p>     *     * <p> This method is intended for use when loading an     * image from a variety of sources, such as from     * persistent storage or from the network.</p>     *     * @param imageBytes the array of image data in a supported image format     * @param imageOffset the offset of the start of the data in the array     * @param imageLength the length of the data in the array     *     * @return the created image     * @throws ArrayIndexOutOfBoundsException if <code>imageOffset</code>     * and <code>imageLength</code>     * specify an invalid range     * @throws NullPointerException if <code>imageData</code> is     * <code>null</code>     * @throws IllegalArgumentException if <code>imageData</code> is incorrectly     * formatted or otherwise cannot be decoded     */    public ImageData createImmutableImageData(byte[] imageBytes,                                              int imageOffset,                                              int imageLength) {        ImageData data = new ImageData();        // parse the pixel data        decode(data, imageBytes, imageOffset, imageLength);        return data;    }    /**     * Creates an immutable <code>ImageData</code>     * using pixel data from the specified     * region of a source <code>ImageData</code>, transformed as specified.     *     * <p>The source image dara may be mutable or immutable.     * For immutable source image data,     * transparency information, if any, is copied to the new     * image data unchanged.</p>     *     * <p>On some devices, pre-transformed images may render more quickly     * than images that are transformed on the fly using     * <code>drawRegion</code>.     * However, creating such images does consume additional heap space,     * so this technique should be applied only to images whose rendering     * speed is critical.</p>     *     * <p>The transform function used must be one of the following, as defined     * in the {@link javax.microedition.lcdui.game.Sprite Sprite} class:<br>     *     * <code>Sprite.TRANS_NONE</code> - causes the specified image     * region to be copied unchanged<br>     * <code>Sprite.TRANS_ROT90</code> - causes the specified image     * region to be rotated clockwise by 90 degrees.<br>     * <code>Sprite.TRANS_ROT180</code> - causes the specified image     * region to be rotated clockwise by 180 degrees.<br>     * <code>Sprite.TRANS_ROT270</code> - causes the specified image     * region to be rotated clockwise by 270 degrees.<br>     * <code>Sprite.TRANS_MIRROR</code> - causes the specified image     * region to be reflected about its vertical center.<br>     * <code>Sprite.TRANS_MIRROR_ROT90</code> - causes the specified image     * region to be reflected about its vertical center and then rotated     * clockwise by 90 degrees.<br>     * <code>Sprite.TRANS_MIRROR_ROT180</code> - causes the specified image     * region to be reflected about its vertical center and then rotated     * clockwise by 180 degrees.<br>     * <code>Sprite.TRANS_MIRROR_ROT270</code> - causes the specified image     * region to be reflected about its vertical center and then rotated     * clockwise by 270 degrees.<br></p>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?