gray Scale Image micro edition - Java javax.microedition.lcdui

Java examples for javax.microedition.lcdui:Graphics

Description

gray Scale Image micro edition

Demo Code


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

public class Main{
    public static Image grayScaleImage(Image sourceImage) {
        int imageWidth = sourceImage.getWidth();
        int imageHeight = sourceImage.getHeight();
        int[] rgbData = new int[imageWidth * imageHeight];
        sourceImage.getRGB(rgbData, 0, imageWidth, 0, 0, imageWidth,
                imageHeight);//from   w ww . j av  a 2 s  .  co m
        int r, g, b;
        int currPoint;
        int grayColor;
        for (int y = 0; y < imageHeight * imageWidth; y++) {
            currPoint = rgbData[y];

            r = currPoint & 0x0000ff;
            g = (currPoint & 0x00ff00) >> 8;
            b = (currPoint & 0xff0000) >> 16;

            grayColor = (r * 76 / 255 + g * 149 / 255 + b * 29 / 255);

            rgbData[y] = grayColor | grayColor << 8 | grayColor << 16;
        }
        return Image
                .createRGBImage(rgbData, imageWidth, imageHeight, false);
    }
}

Related Tutorials