Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to transform the grayscale of color pictures with JAVA

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use JAVA for grayscale transformation of color pictures". In daily operation, I believe many people have doubts about how to use JAVA for grayscale transformation of color pictures. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to use JAVA to transform color pictures". Next, please follow the editor to study!

-in the process of web page design, we sometimes change color graphics into black-and-white display in order to simulate the display effect of monochrome VGA or to exaggerate the design of a certain image, which requires the use of color to black-and-white grayscale transformation technology.

The algorithm of grayscale transformation is actually very simple, as long as the red, green and blue primary colors of each pixel are extracted, and then according to the formula: grayscale value = red luminance value * 30% + green luminance value * 59% + blue luminance value * 11%, calculate a grayscale value and write it back to display memory as the new values of red, green and blue primary colors.

-specific steps are as follows:

First, call the drawImage () method of the Graphics object and display a color image named TEST.JPG in applet. The calling form of drawImage () is: g.drawImage (name,x,y, width,heigth,this).

Second, define an array of RGB values for the color image. The size of the array is the number of pixels in the image. Use PixelGrabber () to get the RGB value of each pixel.

Third, use the gray transformation formula to calculate the gray value of each pixel, store it back into the array as a new RGB value, and then call the createImage () method to construct a new black-and-white image.

-four, show the black-and-white picture. In order to prevent the flicker phenomenon in the transformation process, the double buffer technology is also used in the program, that is, the image is drawn in the virtual screen first, and then displayed at one time.

-the mouse is used in the program to control the conversion from color to black and white.

-the tt4.java source program for grayscale transformation is as follows:

Import java.applet.*

Import java.awt.*

Import java.awt.image.*

Public class tt4 extends Applet

{

Image art,Buf

Int onced=0

Boolean is_color=true

Graphics Bufg;// uses double buffer technology to suppress flicker

Dimension xy=null

Public void init ()

{art=getImage (getDocumentBase (), "test.jpg")

Resize (640,480); / / load pictures

}

Public void paint (Graphics g)

{if (onced==0)

/ / if the picture is loaded for the first time, it will be displayed directly.

{g.drawImage (art,0,0,this)

}

If ((onced==1) | | (onced==2))

/ / if grayscale conversion is in progress, prompt to wait

{g.setColor (new Color (255pm 200pm 0))

G.drawString ("running!", 1,30)

}

If (onced==3)

/ / if the grayscale conversion is completed, the result is displayed

{if (is_color) g.drawImage (Buf,0,0,this)

Else g.drawImage (art,0,0,this)

The color changes between color and black and white

}

}

Public boolean mouseDown (Event evt, int x, int y)

{if (onced==0)

{onced=1

}

Repaint (); / / trigger the event with the mouse

Return true

}

Public boolean mouseUp (Event evt, int x, int y)

{if (onced==1)

{onced=2

Int wd=art.getWidth (this); / / get the image width

Int ht=art.getHeight (this); / / get the image height

GetPixels (art,0,0,wd,ht); / / call grayscale transformation method

}

Return true

}

Public void GetPixels (Image img,int xreint ymenint wreint h)

{int [] pixels= Newin [w * h]

/ / define a piece of memory space

Int gray

PixelGrabber pg=new PixelGrabber (img,x,y,w,h,pixels,0,w)

Try {pg.grabPixels ()

}

Catch (InterruptedExceptione)

{System.err.println ("interrupted waiting for pixels!")

Return

}

For (intj=0;j > 16) & 0xff) * 0.3)

Gray+= (int) ((pixels [w * junii] > 8) & 0xff) * 0.59)

Gray+= (int) (pixels [w * junii]) & 0xff) * 0.11)

/ / get the grayscale value from the red, green and blue values

Pixels [w*j+i] = (255)

< p align="center" >

< applet code=tt4.class name=tt4 width=250 height=188 >

< /applet >

< /body >

< /html >

-where the parameters width and height are the width and height of the image to be transformed. If you want to change the picture, please rename the picture to test.jpg.

-at run time, you can convert a color picture into a black and white picture by clicking the left mouse button, and then you can restore it to a color image by clicking the left mouse button. This procedure runs under the environment of Windows95, Visual Jacks 1.1 and IE4.0.

At this point, the study on "how to use JAVA for grayscale transformation of color pictures" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report