Java image gradient

Source: Internet
Author: User

The general idea of image gradient is nothing more than this: The rbg of all the pixels of the image, each point minus the same amount, and this amount is a gradient amount. Yes, that's it. Our program is just like this, but it's not as simple as thinking. I only wrote a vertical gradient here, but I haven't written any other complicated gradient. Let's study it later. First look at the effect:

Original Image:


Image fades:


Image fades:


Image brightening gradient:


Image brightening gradient:


For example, we use the image gradient darkness. The same principle of gradient brightness is nothing more than a subtraction or an addition.
1. We need to get the image first. If this is not repeated, we will get the bufferedimage of the image.
2. Obtain the image width, height, and pixel value, and create a directcolormodel DEM class for storing the image pixel information. Then we extract each pixel value from the DEM. We subtract an identical pixel value from each pixel. The height of the image varies, and the value subtracted from each pixel is also a gradient value. Here, we need to note that float beginpart is the starting position of the gradient. The default value is 1, that is, starting from the middle of the image. beginpart <1 starts the gradient from the middle of the image, the smaller the value, the higher the value. Beginpart> 1 the gradient starts from the bottom of the image. The larger the gradient, the higher the gradient.
The Code is as follows:

Private int [] darkerpixels (bufferedimage originalpic, float beginpart) {// obtain the image width. Int imagewidth = originalpic. getwidth (); // obtain the Image Height. Int imageheight = originalpic. getheight (); // obtain the pixel value of the image. Int totalblocks = imagewidth * imageheight; // create a bucket with the same value. Int [] pixels = new int [totalblocks]; // Stores image pixel information. Directcolormodel DEM = new directcolormodel (24, 0xff0000, 0x00ff00, 0x0000ff); float Inc = 255f/imageheight/beginpart; int int_inc = 100; float float_inc = 0; for (INT I = 0; I <totalblocks; I ++) {if (I % imageheight = 0 & I! = 0) {float_inc = float_inc + Inc; int_inc = (INT) (float_inc); If (0> int_inc) {int_inc = 0;} If (255 <int_inc) {int_inc = 255 ;}} int A = DEM. getalphamask ()/2; int r = DEM. getred (pixels [I])-int_inc; If (r <0) {r = 0;} int G = DEM. getgreen (pixels [I])-int_inc; If (G <0) {G = 0;} int B = DEM. getblue (pixels [I])-int_inc; If (B <0) {B = 0 ;} pixels [I] = A <24 | r <16 | G <8 | B;} return pixels ;}

3. Use the changed pixel to generate a new image. The Code is as follows:

Public final bufferedimage getgradualimg (bufferedimage originalpic) {// set the development position of the gradient. 1 is the middle position. the start position is less than 1 and the start position is greater than 1. Float beginpart = 1f; // obtain all gradient pixels of the image. Int [] pixels = darkerpixels (originalpic, beginpart); int imagewidth = originalpic. getwidth (); int imageheight = originalpic. getheight (); memoryimagesource = new memoryimagesource (imagewidth, imageheight, new directcolormodel (24, 0xff0000, 0x00ff00, 0x0000ff), pixels, 0, imagewidth); image imagebuf = NULL; try {memoryimagesource. setanimated (true); memoryimagesource. setfullbufferupdates (true); imagebuf = This. createimage (memoryimagesource); // generate a new memoryimagesource image. newpixels ();} catch (nosuchmethoderror e) {e. printstacktrace ();} bufferedimage changedimage = new bufferedimage (imagewidth, imageheight, bufferedimage. type_3byte_bgr); graphics2d g2d = changedimage. creategraphics (); g2d. drawimage (imagebuf, 0, 0, this); Return changedimage ;}

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.