effectutils.java

来自「Mobile 应用程序使用 Java Micro Edition (Java M」· Java 代码 · 共 410 行 · 第 1/2 页

JAVA
410
字号
        }        for (int i = 0; i < data.length; i++) {            data[i] /= total;        }        return data;    }    // =================================================================================================================    // Get/Set Pixels helper methods    /**     * <p>Returns an array of pixels, stored as integers, from a <code>BufferedImage</code>. The pixels are grabbed from     * a rectangular area defined by a location and two dimensions. Calling this method on an image of type different     * from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the     * image.</p>     *     * @param img    the source image     * @param x      the x location at which to start grabbing pixels     * @param y      the y location at which to start grabbing pixels     * @param w      the width of the rectangle of pixels to grab     * @param h      the height of the rectangle of pixels to grab     * @param pixels a pre-allocated array of pixels of size w*h; can be null     * @return <code>pixels</code> if non-null, a new array of integers otherwise     * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h     */    static byte[] getPixels(BufferedImage img,                                   int x, int y, int w, int h, byte[] pixels) {        if (w == 0 || h == 0) {            return new byte[0];        }        if (pixels == null) {            pixels = new byte[w * h];        } else if (pixels.length < w * h) {            throw new IllegalArgumentException("pixels array must have a length >= w*h");        }        int imageType = img.getType();        if (imageType == BufferedImage.TYPE_BYTE_GRAY) {            Raster raster = img.getRaster();            return (byte[]) raster.getDataElements(x, y, w, h, pixels);        } else {            throw new IllegalArgumentException("Only type BYTE_GRAY is supported");        }    }    /**     * <p>Writes a rectangular area of pixels in the destination <code>BufferedImage</code>. Calling this method on an     * image of type different from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code>     * will unmanage the image.</p>     *     * @param img    the destination image     * @param x      the x location at which to start storing pixels     * @param y      the y location at which to start storing pixels     * @param w      the width of the rectangle of pixels to store     * @param h      the height of the rectangle of pixels to store     * @param pixels an array of pixels, stored as integers     * @throws IllegalArgumentException is <code>pixels</code> is non-null and of length &lt; w*h     */    static void setPixels(BufferedImage img,                                 int x, int y, int w, int h, byte[] pixels) {        if (pixels == null || w == 0 || h == 0) {            return;        } else if (pixels.length < w * h) {            throw new IllegalArgumentException("pixels array must have a length >= w*h");        }        int imageType = img.getType();        if (imageType == BufferedImage.TYPE_BYTE_GRAY) {            WritableRaster raster = img.getRaster();            raster.setDataElements(x, y, w, h, pixels);        } else {            throw new IllegalArgumentException("Only type BYTE_GRAY is supported");        }    }    /**     * <p>Returns an array of pixels, stored as integers, from a     * <code>BufferedImage</code>. The pixels are grabbed from a rectangular     * area defined by a location and two dimensions. Calling this method on     * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>     * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>     *     * @param img the source image     * @param x the x location at which to start grabbing pixels     * @param y the y location at which to start grabbing pixels     * @param w the width of the rectangle of pixels to grab     * @param h the height of the rectangle of pixels to grab     * @param pixels a pre-allocated array of pixels of size w*h; can be null     * @return <code>pixels</code> if non-null, a new array of integers     *   otherwise     * @throws IllegalArgumentException is <code>pixels</code> is non-null and     *   of length &lt; w*h     */    public static int[] getPixels(BufferedImage img,                                  int x, int y, int w, int h, int[] pixels) {        if (w == 0 || h == 0) {            return new int[0];        }        if (pixels == null) {            pixels = new int[w * h];        } else if (pixels.length < w * h) {            throw new IllegalArgumentException("pixels array must have a length" +                                               " >= w*h");        }        int imageType = img.getType();        if (imageType == BufferedImage.TYPE_INT_ARGB ||            imageType == BufferedImage.TYPE_INT_RGB) {            Raster raster = img.getRaster();            return (int[]) raster.getDataElements(x, y, w, h, pixels);        }        // Unmanages the image        return img.getRGB(x, y, w, h, pixels, 0, w);    }    /**     * <p>Writes a rectangular area of pixels in the destination     * <code>BufferedImage</code>. Calling this method on     * an image of type different from <code>BufferedImage.TYPE_INT_ARGB</code>     * and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the image.</p>     *     * @param img the destination image     * @param x the x location at which to start storing pixels     * @param y the y location at which to start storing pixels     * @param w the width of the rectangle of pixels to store     * @param h the height of the rectangle of pixels to store     * @param pixels an array of pixels, stored as integers     * @throws IllegalArgumentException is <code>pixels</code> is non-null and     *   of length &lt; w*h     */    public static void setPixels(BufferedImage img,                                 int x, int y, int w, int h, int[] pixels) {        if (pixels == null || w == 0 || h == 0) {            return;        } else if (pixels.length < w * h) {            throw new IllegalArgumentException("pixels array must have a length" +                                               " >= w*h");        }        int imageType = img.getType();        if (imageType == BufferedImage.TYPE_INT_ARGB ||            imageType == BufferedImage.TYPE_INT_RGB) {            WritableRaster raster = img.getRaster();            raster.setDataElements(x, y, w, h, pixels);        } else {            // Unmanages the image            img.setRGB(x, y, w, h, pixels, 0, w);        }    }    /**     * <p>Returns a new <code>BufferedImage</code> using the same color model     * as the image passed as a parameter. The returned image is only compatible     * with the image passed as a parameter. This does not mean the returned     * image is compatible with the hardware.</p>     *     * @param image the reference image from which the color model of the new     *   image is obtained     * @return a new <code>BufferedImage</code>, compatible with the color model     *   of <code>image</code>     */    public static BufferedImage createColorModelCompatibleImage(BufferedImage image) {        ColorModel cm = image.getColorModel();        return new BufferedImage(cm,            cm.createCompatibleWritableRaster(image.getWidth(),                                              image.getHeight()),            cm.isAlphaPremultiplied(), null);    }    /**     * <p>Returns a new translucent compatible image of the specified width and     * height. That is, the returned <code>BufferedImage</code> is compatible with     * the graphics hardware. If the method is called in a headless     * environment, then the returned BufferedImage will be compatible with     * the source image.</p>     *     * @param width the width of the new image     * @param height the height of the new image     * @return a new translucent compatible <code>BufferedImage</code> of the     *   specified width and height     */    public static BufferedImage createCompatibleTranslucentImage(int width,                                                                 int height) {        return isHeadless() ?                new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB) :                getGraphicsConfiguration().createCompatibleImage(width, height,                                                   Transparency.TRANSLUCENT);    }    private static boolean isHeadless() {        return GraphicsEnvironment.isHeadless();    }    // Returns the graphics configuration for the primary screen    private static GraphicsConfiguration getGraphicsConfiguration() {        return GraphicsEnvironment.getLocalGraphicsEnvironment().                    getDefaultScreenDevice().getDefaultConfiguration();    }}

⌨️ 快捷键说明

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