BufferedImage manipulating picture notes (GO)

Source: Internet
Author: User

BufferedImage is a subclass of image, BufferedImage generated picture in memory has an image buffer, the use of this buffer we can easily manipulate this image, usually used to do picture modification operations such as size transformation, image gray, Set the picture to be transparent or opaque, and so on.

BufferedImage bufferedimage = Imageio.read (new FileInputStream (FilePath));  

Make a picture gray

There are two ways to make a picture gray, one is to use a ready-made class, and one is to manipulate each pixel yourself.

Using out-of-the-box class Colorconvertop, it works by converting a picture of a color pattern into another picture of a color pattern. Color mode is such as RGB color mode, grayscale color mode, such as determining the color of the picture, such as a pair of RGB color mode of the picture is color, but we copy it into a grayscale color mode of the picture, the picture becomes gray.

 Publicbufferedimage getgraypicture (bufferedimage originalimage) {bufferedimage graypicture; intImageWidth =originalimage.getwidth (); intImageHeight =originalimage.getheight (); Graypicture=Newbufferedimage (ImageWidth, ImageHeight, BUFFEREDIMAGE.TYPE_3BYTE_BGR); Colorconvertop CCO=Newcolorconvertop (colorspace getinstance (colorspace.cs_gray),NULL);             Cco.filter (Originalimage, graypicture); returngraypicture; }  

You manipulate the image of the pixel to make the picture gray. General algorithm for Gray Image: Take out the R, G, b values of a pixel, then recalculate R, G, b values, calculate the formula as R=r*0.3+g*0.59+b*0.11,g=r,b=g, and then re-write the RGB value back to the pixel.

You can use the following three functions

Remove R, G, B, respectively. ColorModel is a class used to convert the RGB values of a point in a picture to R, G, B, and alpha equivalents. The bufferedimage itself can only take out a complete RGB value, unable to separate the individual R, G, b equivalents.

Bufferedimage.getcolormodel (). getred (int pixel)

Bufferedimage.getcolormodel (). getgreen (int pixel)

Bufferedimage.getcolormodel (). getBlue (int pixel)

However, it is important to note that when you use this method to remove R, G, and B, it is possible to eject an error message--illegalargumentexception:more than one component per pixel. This error is usually reported when you use this method to manipulate images in JPG and other formats. This is because some images, such as JPG, that do not support the use of pixels with a single int value, should be read using pixel values of type object, which is Bufferedimage.getcolormodel (). Getgreen (Object Indata

 Publicbufferedimage getgraypicture (bufferedimage originalimage) {intGreen=0,red=0,blue=0, RGB; intImageWidth =originalimage.getwidth (); intImageHeight =originalimage.getheight ();  for(inti = Originalimage.getminx (); i < imagewidth; i++)              {                   for(intj = Originalimage.getminy (); J < ImageHeight; J + +)                  {  //the pixel point of the picture is actually a matrix, which uses two for loops to manipulate each pixel .Object data = Routeimage.getraster (). getDataElements (I, J,NULL);//gets the pixel of the point and is represented by the object typeRed =Routeimage.getcolormodel (). getred (data); Blue=Routeimage.getcolormodel (). GetBlue (data); Green=Routeimage.getcolormodel (). Getgreen (data); Red= (red*3 + green*6 + blue*1)/10; Green=Red; Blue=Green; /*here, R, G, and B are converted to RGB values because BufferedImage does not provide a way to set a single color, only RGB can be set. RGB Maximum is 8388608, when greater than this value, should be subtracted 255*255*255 that is 16777216*/RGB= (red*256 + green) *256+Blue; if(rgb>8388608) {RGB= rgb-16777216; }  //write RGB values back to pictureRouteimage.setrgb (i, J, RGB); }                                }                       returnOriginalimage; }   

BufferedImage manipulating picture notes (GO)

Related Article

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.