Java converts a color image to a gray image

Source: Internet
Author: User

Import Java. applet. *; <br/> Import Java. AWT. *; <br/> Import Java. AWT. image. *; </P> <p> @ suppresswarnings ("serial") <br/> public class changeimagecolor extends applet {<br/>/** the grayscale conversion algorithm is actually very simple, you only need to extract the three primary colors of red, green, and blue for each pixel, and then follow the formula: gray value = red Brightness Value * 30% + green Brightness Value * 59% + blue Brightness Value * 11%, calculate a gray value and use it as a new value of the red, green, and blue colors to re-write it back to the video. */<Br/> image art, Buf; <br/> int onced = 0; <br/> Boolean is_color = true; <br/> graphics bufg; // use double buffer technology to suppress flickering; <br/> dimension xy = NULL; </P> <p> Public void Init () {<br/> art = getimage (getdocumentbase (), "test2.png"); <br/> resize (640,480); // load the image; <br/>}</P> <p> Public void paint (Graphics g) {<br/> G. setcolor (new color (0 xffffff); <br/> G. fillrect (0, 0, getwidth (), getheight (); <br/> If (Onced = 0) <br/> // if the image is loaded for the first time, it is displayed directly. <br/>{< br/> G. drawimage (art, 0, 0, this); <br/>}< br/> If (onced = 1) | (onced = 2 )) <br/> // If the grayscale conversion is in progress, a message indicating waiting is displayed. <br/>{< br/> G. setcolor (new color (255,200, 0); <br/> G. drawstring ("running! ", 1, 30); <br/>}< br/> If (onced = 3) <br/> // If the grayscale conversion is complete, the result is displayed; <br/>{< br/> If (is_color) <br/> G. drawimage (BUF, 0, 0, this); <br/> else <br/> G. drawimage (art, 0, 0, this); <br/> is_color =! Is_color; // between color and black and white; <br/>}</P> <p> Public Boolean mousedown (event EVT, int X, int y) {<br/> If (onced = 0) {<br/> onced = 1; <br/>}< br/> repaint (); // trigger the event with the mouse; <br/> return true; <br/>}</P> <p> Public Boolean mouseup (event EVT, int X, int y) {<br/> If (onced = 1) {<br/> onced = 2; <br/> int WD = art. getwidth (this); // obtain the image width. <br/> int ht = art. getheight (this); // obtain the Image Height. <br/> getpix Els (art, 0, 0, WD, HT); // call the grayscale conversion method; <br/>}< br/> return true; <br/>}</P> <p> Public void getpixels (image IMG, int X, int y, int W, int H) {<br/> int [] pixels = new int [w * H]; <br/> // defines a memory space; <br/> pixelgrabber Pg = new pixelgrabber (IMG, X, Y, W, H, pixels, 0, W); <br/> try {<br/> PG. grabpixels (); <br/>} catch (interruptedexception e) {<br/> system. err. println ("interrupted waiting for Pixel S! "); <Br/> return; <br/>}< br/> for (INT I = 0; I <p; I ++) {<br/> for (Int J = 0; j <W; j ++) {<br/> // obtain the pixel color <br/> int color = pixels [w * I + J]; <br/> int alpha = (color & 0xff000000)> 24; <br/> int Red = (INT) (color & 0x00ff0000)> 16) * 0.3); <br/> int Green = (INT) (color & 0x0000ff00)> 8) * 0.59); <br/> int Blue = (INT) (color & 0x000000ff) * 0.11); <br/> color = Red + green + blue; <br/> color = (alpha <24) | (color <16) | (color <8) | color; <br/> // system. out. println (integer. tohexstring (color); <br/> // The value ranges from red, green, and blue to Gray. <br/> pixels [w * I + J] = color; <br/>}< br/> image PIC = createimage (New memoryimagesource (W, H, pixels, 0, W); <br/> bufg. drawimage (PIC, 0, 0, this); // displays black-and-white images; <br/> onced = 3; <br/> repaint (); <br/>}</P> <p> Public void Update (Graphics g) {<br/> If (xy = NULL) {<br/> xy = getsize (); <br/> Buf = createimage (XY. width, XY. height); <br/> bufg = Buf. getgraphics (); <br/>}< br/> paint (g); // modify the update method to avoid flickering; <br/>}< br/>}

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.