Java Dynamic Image Processing

Source: Internet
Author: User
Tags transparent image
It is very convenient to use Java to dynamically process images and can be used in many application scenarios, such as dynamic input of stock lines, thumbnails, dynamic banner images, and dynamic interface styles. I also used these technologies in the software development process, so that the UI Color Style of the software can be customized by users, greatly enhancing the personalized adaptability of the software interface. The following describes the entire process of image processing using Java technology:

1. image loading. To process images, we must first load image objects to the Java memory space. Java uses many classes to support image loading, such
A. load images based on the byte array of image files:
Public Image LoadImage (byte [] data ){
Image T = toolkit. getdefatooltoolkit (). createimage (data );
Trackimage (T, "loading byte array images ");
Return T;
}
B. Load Images Based on urls:
Public Image LoadImage (string urlpath ){
Image T = NULL;
If (A instanceof applet ){
Try {
URL u = getresourceurl (urlpath );
T = getapplet (). getimage (U );
Trackimage (T, urlpath); // detects the image loading process
} Catch (exception e ){
Policylistener (medialoadevent. media_load_failed, false, E. getmessage (); // loading failed
}
} Else {
T = toolkit. getdefatooltoolkit (). getimage (urlpath );
Trackimage (T, urlpath); // detects the image loading process
}
Return T;
}
Public Image LoadImage (string urlpath, Boolean isurl ){
Image T = NULL;
If (isurl ){
Try {
URL u = getresourceurl (urlpath );
T = toolkit. getdefatooltoolkit (). getimage (U );
Trackimage (T, urlpath );
} Catch (exception e ){
Policylistener (medialoadevent. media_load_failed, false, E. getmessage (); // loading failed
}
} Else {
T = toolkit. getdefatooltoolkit (). getimage (urlpath );
Trackimage (T, urlpath); // detects the image loading process
}
Return T;
}
2. After the image is loaded, we can process the image and change the color, brightness, contrast, flip, image, and other processing operations. Here I will only detail the bottom-layer pixel transformation and Adjustment Technology. Many other methods are encapsulated based on this.
A. Make the image transparent Based on the specified color value and a fixed range of chromatic aberration:
Public Image transpanent (image repimage, color oldcolor, int errorrange ){
Int W = repimage. getwidth ();
Int H = repimage. getheight ();
Int [] imgdata = parimage (repimage, W, H );
Int RR, GG, BB;
RR = oldcolor. getred ();
Gg = oldcolor. getgreen ();
BB = oldcolor. getblue ();
Int Offr, offg, offb;
For (INT I = 0; I For (Int J = 0; j <W; j ++) {// column-by-column Scan
Int which = I * w + J;
Int r = (imgdata [which] & 0xff0000 >>> 16 ));
Int G = (imgdata [which] & 0xff00 >>> 8 ));
Int B = (imgdata [which] & 0xff );
Offr = math. Abs (R-rr );
Offg = math. Abs (G-GG );
Offb = math. Abs (B-bb );
If (Offr + offg + offb <errorrange) imgdata [I * w + J] = 16777215;
}
}
Image T = toolkit. getdefatooltoolkit (). createimage (New memoryimagesource (W, H, imgdata, 0, W ));
Trackimage (T, "loading transparent image processing ");
Return T;
}
B. Cut the image. Here the image processing filter class cropimagefilter is used. There are many similar filters to achieve different image processing transformations.
Public Image crop (int x, int y, int W, int H, image IMG ){
Imagefilter filter = new cropimagefilter (X, Y, W, H );
Imageproducer producer = new filteredimagesource (IMG. getsource (), filter );
Image T = A. createimage (producer );
Trackimage (T, "loading and cutting images ");
Return T;
}
3. Image Combination and Image Rendering:
Using the Java plotting class, you can overlay multiple images and customize various graphic images, such as image watermarks, verification code images, and banner images.

Public static void createimagepng (string signcode, outputstream out ){
Bufferedimage offimg = NULL;
Graphics offgra = NULL;
While (offimg = NULL | offgra = NULL ){
If (offimg = NULL) offimg = new bufferedimage (50, 18, bufferedimage. type_int_rgb );
If (offgra = NULL & offimg! = NULL) offgra = offimg. getgraphics ();
}
Offgra. setcolor (new color (235,235,235 ));
Offgra. fillrect (0, 0, 50, 18 );
/* For (INT I = 0; I <12; I ++) {// generates a background for obfuscation. You can also use a background image for obfuscation.
Offgra. setcolor (color. Decode ("#" + color [systemtool. getrandom (0, color. Length-1)]);
Offgra. filloval (systemtool. getrandom (), systemtool. getrandom );
}*/
Offgra. setcolor (color. Red );
// Offgra. setcolor (color. Decode ("#" + color [systemtool. getrandom (0, color. Length-1)]);
Try {
Font font = font. createfont (font. truetype_font, Com. sunstudio. util. classloaderex. getresourceinputstream (systemconfig. getparameter ("validate_font"); // fileinputstream ("C :\\\ comicbd. TTF "));
Offgra. setfont (font. derivefont (font. Bold, 18f ));
} Catch (exception e ){}
Offgra. drawstring (signcode, 1, 14 );
Pngencoder PNG = new pngencoder (Toolkit. getdefatooltoolkit (). createimage (offimg. getsource (), pngencoder. encode_alpha, pngencoder. filter_last, 3 );
Tryappsout.write(png.png encode ();} catch (exception e ){}
Offgra. Dispose ();
}

4. Image Encoding output:
JAVA image objects are the Unified Data Objects used for image loading and processing. When output to an output device is required, they must be encoded and saved in the specified image file format, common image encoders include JPG, BMP, gif89, gif87, and PNG. I collected a lot of image format encoder, If you need to contact me, linlichao@163.com.
for the output of images in different formats, the process is the same. The encoders encode the output of image objects according to the requirements of different file formats. These are international standards, we only need to call it.
pngencoder PNG = new pngencoder (Toolkit. getdefatooltoolkit (). createimage (offimg. getsource (), pngencoder. encode_alpha, pngencoder. filter_last, 3);
trydeskout.write(png.png encode ();} catch (exception e) {}

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.