smooth Image micro edition - Java javax.microedition.lcdui

Java examples for javax.microedition.lcdui:Graphics

Description

smooth Image micro edition

Demo Code


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

public class Main{
    public static Image smoothImage(Image sourceImage, int imageX,
            int imageY, int imageWidth, int imageHeight) {
        imageWidth -= imageX;/*w ww  .j a  v  a2  s.co m*/
        imageHeight -= imageY;
        int[] rgbData = new int[imageWidth * imageHeight];
        sourceImage.getRGB(rgbData, 0, imageWidth, imageX, imageY,
                imageWidth, imageHeight);
        int currY, prevY, nextY, r, g, b, currPoint, prevYPoint, prevXPoint, nextYPoint, nextXPoint;
        for (int y = 1; y < imageHeight; y++) {
            prevY = (y - 1) * imageWidth;
            currY = y * imageWidth;
            if (y + 1 < imageHeight) {
                nextY = (y + 1) * imageWidth;
            } else {
                nextY = currY;
            }
            for (int x = 1; x < imageWidth - 1; x++) {
                currPoint = rgbData[currY + x];
                prevYPoint = rgbData[prevY + x];
                prevXPoint = rgbData[currY + x - 1];
                nextYPoint = rgbData[nextY + x];
                nextXPoint = rgbData[currY + x + 1];
                b = ((currPoint & 0xff0000) + (prevYPoint & 0xff0000)
                        + (prevXPoint & 0xff0000) + (nextYPoint & 0xff0000) + (nextXPoint & 0xff0000)) / 5 & 0xff0000;
                g = ((currPoint & 0xff00) + (prevYPoint & 0xff00)
                        + (prevXPoint & 0xff00) + (nextYPoint & 0xff00) + (nextXPoint & 0xff00)) / 5 & 0xff00;
                r = ((currPoint & 0xff) + (prevYPoint & 0xff)
                        + (prevXPoint & 0xff) + (nextYPoint & 0xff) + (nextXPoint & 0xff)) / 5 & 0xff;
                rgbData[currY + x] = r | g | b;
            }
        }
        return Image
                .createRGBImage(rgbData, imageWidth, imageHeight, false);
    }
}

Related Tutorials