Image processing tools,
Including: Reading local images, online images, image synthesis (image watermark), writing text on images (Text watermark), hexadecimal conversion, and conversion of color objects
Package COM. chat; import Java. AWT. alphacomposite; import Java. AWT. color; import Java. AWT. font; import Java. AWT. graphics2d; import Java. AWT. image; import Java. AWT. image. bufferedimage; import Java. AWT. image. colormodel; import Java. AWT. image. pixelgrabber; import Java. io. file; import Java. io. ioexception; import java.net. URL; import javax. imageIO. imageIO;/*** image processing tool class ** @ author Daven **/public class imgutil {pub LIC static void main (string [] ARGs) {imgutil. composepic ("D:/back.png", "http://t3.qlogo.cn/mbloghead/d387538f629cfb53c2b0/100", "d:/output.png", 53,161) ;}/ *** composepic (similar to image watermarks) * @ Param backimage background image object * @ Param headimage user profile object * @ Param outputpath output path (D:/img/out.png) * @ Param leftx offset from the left side of the target image * @ Param lefty offset from the left side of the target image * @ throws interruptedexception * @ throws ioexception */public static void com Posepic (image backimage, image headimage, string outputpath, int leftx, int lefty) throws interruptedexception, ioexception {// Image Height/width int bwidth = backimage. getwidth (null); int bheight = backimage. getheight (null); int hwidth = headimage. getwidth (null); int hheight = headimage. getheight (null); int alphatype = bufferedimage. type_int_rgb;/* If (hasalpha (backimage) {alphatype = bufferedimage. type_int_arg B;} * // drawing bufferedimage backgroundimage = new bufferedimage (bwidth, bheight, alphatype); graphics2d graphics2d = backgroundimage. creategraphics (); graphics2d. drawimage (backimage, 0, 0, null); graphics2d. setcomposite (alphacomposite. getinstance (alphacomposite. src_atop, 1); graphics2d. drawimage (headimage, leftx, Lefty, hwidth, hheight, null); // output ImageIO. write (backgroundimage, "PNG", new file (outputpath );}/*** Synthesize image * @ Param backpicpath local path of background image * @ Param headpicurl user profile network URL * @ Param outputpath output path (D:/img/out.png) * @ Param leftx offset from the left side of the target image * @ Param lefty offset from the left side of the target image */public static void composepic (string backpicpath, string headpicurl, string outputpath, int leftx, int lefty) {try {// read the background image backimage = imgutil. loadimagelocal (backpicpath); // Add the nickname int Ln = imgutil. getlength ("deep water fish vest") * 24; Backimage = imgutil. presstext ("deep water fish vest", backimage, "", Font. bold, imgutil. string2color ("# ffffff"), 24,175 + (266-ln)/2,188, 1); // score backimage = imgutil. presstext ("100 points", backimage, "Hua Kang W12 (p)", Font. layout_no_limit_context, imgutil. string2color ("# fd6e10"), 22,280,233, 1); // return backimage = imgutil. presstext ("85%", backimage, "Mini Jian lingbo", Font. layout_no_limit_context, imgutil. string2color ("# e600 00 "), 24,368,264, 1); // Title: int lc = imgutil. getlength (" has the potential to worship the gods! ") * 32; backimage = imgutil. presstext (" has the potential to worship the gods! ", Backimage," W12 (p) ", Font. layout_no_limit_context, imgutil. string2color ("#880000"), 32, (488-lc)/2,310, 1); // read the Avatar image headimage = imgutil. loadimageurl (headpicurl); // image imgutil. composepic (backimage, headimage, outputpath, leftx, lefty);} catch (ioexception e) {e. printstacktrace ();} catch (interruptedexception e) {e. printstacktrace () ;}}/*** add text (Text watermark) to the image * @ Param presstext watermark text * @ Param Targetimg target image * @ Param fontname * @ Param fontstyle font style, such as bold and italic (font. bold | font. italic) * @ Param color font color * @ Param fontsize font size * @ Param x watermark text offset from the left side of the target image * @ Param y watermark text offset from the top side of the target image * @ Param alpha transparency (0.0 -- 1.0, 0.0 is completely transparent, 1.0 is completely opaque) * @ throws ioexception */public static bufferedimage presstext (string presstext, image targetimg, string fontname, int fontstyle, color, int fontsize, I Nt x, int y, float alpha) throws ioexception {// image width and height int width = targetimg. getwidth (null); int Height = targetimg. getheight (null); // style Font font = new font (fontname, fontstyle, fontsize); bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb); graphics2d G = image. creategraphics (); G. drawimage (targetimg, 0, 0, width, height, null); G. setcolor (color); // color G. setfont (font); G. set Composite (alphacomposite. getinstance (alphacomposite. src_atop, alpha); G. drawstring (presstext, x, y); G. dispose (); Return image;}/*** whether to enable alpha channel ** @ Param image * @ return * @ throws interruptedexception */public static Boolean hasalpha (image) throws interruptedexception {If (image instanceof bufferedimage) {bufferedimage bimage = (bufferedimage) image; return bimage. getcolormodel (). hasalpha () ;} Pixelgrabber pgrabber = new pixelgrabber (image, 0, 0, 1, 1, false); pgrabber. grabpixels (); colormodel = pgrabber. getcolormodel (); Return colormodel. hasalpha ();}/*** import the local image to the buffer * @ Param imgpath local image address * @ return * @ throws ioexception */public static bufferedimage loadimagelocal (string imgpath) throws ioexception {return ImageIO. read (new file (imgpath);}/*** import the network image to the buffer * @ Param imgurl Network Image URL * @ return * @ throws ioexception */public static bufferedimage loadimageurl (string imgurl) throws ioexception {URL url = new URL (imgurl); Return ImageIO. read (URL);}/*** hexadecimal conversion color object * @ Param Str * @ return */public static color string2color (string Str) {int I = integer. parseint (Str. substring (1), 16); return new color (I);}/*** convert the color object to hexadecimal format * @ Param color * @ return */public static string color2s Tring (color) {string r = integer. tohexstring (color. getred (); r = R. Length () <2? ('0' + r): R; string B = integer. tohexstring (color. getblue (); B = B. Length () <2? ('0' + B): B; string G = integer. tohexstring (color. getgreen (); G = G. Length () <2? ('0' + G): G; return '#' + R + B + G;}/*** get the character length. One Chinese character is used as one character, an English letter is the length of 0.5 characters * @ Param text * @ return, for example, text = "China". 2 * text = "test" is returned ", returns 2 * text = "China ABC", 4 */public static int getlength (string text) {int textlength = text. length (); int length = textlength; For (INT I = 0; I <textlength; I ++) {If (string. valueof (text. charat (I )). getbytes (). length> 1) {length ++;} return (length % 2 = 0 )? Length/2: length/2 + 1 ;}}
Original Image:
Slice: