resize Image With Alpha Proportional micro edition - Java javax.microedition.lcdui

Java examples for javax.microedition.lcdui:Graphics

Description

resize Image With Alpha Proportional micro edition

Demo Code


import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Main{
    public static Image resizeImageWithAlphaProportional(Image sourceImage,
            int prcSize, int prcAlpha) {
        if (prcSize > 100) {
            prcSize = 100;/*  ww w .ja  v  a  2  s.c  om*/
        } else if (prcSize <= 0) {
            return Image.createImage(1, 1);
        }
        int imageWidth = sourceImage.getWidth();
        int imageHeight = sourceImage.getHeight();
        int destWidth = imageWidth * prcSize / 100;
        int destHeight = imageHeight * prcSize / 100;
        /** Buffers **/
        int[] rgbData = new int[imageWidth * imageHeight];
        int[] rgbDest = new int[destWidth * destHeight];
        /** Fill buffer **/
        sourceImage.getRGB(rgbData, 0, imageWidth, 0, 0, imageWidth,
                imageHeight);
        int currY, xx, yy = 0, currPoint;
        int a, r, g, b;
        for (int y = 0; y < destHeight; y++) {
            currY = y * destWidth;
            xx = 0;
            for (int x = 0; x < destWidth; x++) {
                try {
                    currPoint = rgbData[(yy * 100 + xx) / 100];
                    a = currPoint & 0xff000000;
                    r = currPoint & 0xff;
                    g = currPoint & 0xff00;
                    b = currPoint & 0xff0000;
                    if (a == 0xff000000) {
                        a = 256;
                    } else {
                        a >>= 24;
                    }
                    a -= (255 * prcAlpha) / 100;
                    if (a >= 256) {
                        a = 0xff000000;
                    } else {
                        a <<= 24;
                    }
                    rgbDest[currY + x] = a | r | g | b;
                } catch (Throwable ex) {
                }
                xx += 10000 / prcSize;
            }
            yy = (y * imageHeight / destHeight) * imageWidth;
        }
        return Image.createRGBImage(rgbDest, destWidth, destHeight, true);
    }
}

Related Tutorials