Pixel-Level Image Processing in midp2.0 (3)

Source: Internet
Author: User

In our games, we often need to plot the current scenario into a grayscale scenario. For example, when a role dies, we will make the scene gray, so how can we implement a gray scenario? One way is to make a gray image and draw it, but this will occupy the size of our JAR file, now let's take a look at how to plot a color image into a gray image.

Like a color image, our gray images are also composed of RGB three primary colors. However, a gray image has a feature that its rgb3 values are equal, however, there is no special relationship between the three values of the color image rgb3, so what we need to do is to change the three values of the color image rgb3 to an equal value, we only need to find the average value of the rgb3 values of the color image, and then change the RGB values to the average value.

Obtain the rgb3 values first.

R = (argb [I] & 0x00ff0000)> 16; // obtain the red value

G = (argb [I] & 0x0000ff00)> 8; // obtain the green value

B = argb [I] & 0x000000ff; // obtain the blue value

Then calculate the average value of R, G, and B.

Temp = (R + G + B)/3;

Then combine the new pixel values:

R = temp <16;

G = temp <8;

B = temp;

Argb [I] = 0xff000000 | r | G | B; // combine three colors

Finally, draw the modified RGB array.

Import java. Io. ioexception;

Import javax. microedition. lcdui. Canvas;
Import javax. microedition. lcdui. graphics;
Import javax. microedition. lcdui. image;

/**
* @ Author Liu Jun
* @ Version 1.0
*/
Public class TCanvas extends canvas {
Image image; // the image to be processed
Int argb [];
Int A = 0; // set the initial transparency value of the pixel to 0, and then add this value continuously in the thread.
Public TCanvas (){
Super ();
Try {
Image = image. createimage ("/test.png"); // import the image
} Catch (ioexception e ){
E. printstacktrace ();
}
Argb = new int [image. getwidth () * image. getheight ()];
Image. getrgb (argb, 0, image. getwidth (), image. getwidth (), image. getheight (); // obtain the argb value of the image.
Int temp;
Int R, G, B;

For (INT I = 0; I <argb. length; I ++)
{
R = (argb [I] & 0x00ff0000)> 16; // obtain the red value
G = (argb [I] & 0x0000ff00)> 8; // obtain the green value
B = argb [I] & 0x000000ff; // obtain the blue value
Temp = (R + G + B)/3; // calculate their average value, and the values of R, G, and B are equal to this value, when the three values of RGB are equal, the color of the pixel is gray.
R = temp <16;
G = temp <8;
B = temp;
Argb [I] = 0xff000000 | r | G | B; // combine three colors
}
}

Protected void paint (Graphics g ){
G. setcolor (0 xffffff );
G. fillrect (0, 0, getwidth (), getheight ());
G. setcolor (0 );
G. drawimage (image, 0, 0, graphics. Top | graphics. Left );
G. drawrgb (argb, 0, image. getwidth (), 0,100, image. getwidth (), image. getheight (), true); // draw a pixel array
G. drawstring (a + "", 10, 90, graphics. Top | graphics. Left );
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.