Picture tool class, image watermark, Text watermark, zoom, filler, etc.

Source: Internet
Author: User

ImportJava.awt.AlphaComposite;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics2D;ImportJava.awt.Image;ImportJava.awt.geom.AffineTransform;Importjava.awt.image.AffineTransformOp;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException; ImportJavax.imageio.ImageIO; /*** Image Tool class, image watermark, Text watermark, zoom, filler, etc.*/  Public Final classImageutils {/**image format: JPG*/     Private Static FinalString pictrue_formate_jpg = "JPG"; Privateimageutils () {}/*** Add Image watermark *@paramtargetimg target picture path, such as: C://mypictrue//1.jpg *@paramwaterimg watermark Picture path, such as: C://mypictrue//logo.png *@paramThe x-watermark image is offset from the left side of the target image, if x<0, in the middle of the@paramy watermark image offset from the upper side of the target image, if y<0, in the middle of the@paramAlpha Transparency (0.0--1.0, 0.0 full transparent, 1.0 completely opaque)*/      Public Final Static voidPressimage (String targetimg, String waterimg,intXintYfloatAlpha) {             Try{File File=NewFile (TARGETIMG); Image Image=imageio.read (file); intwidth = image.getwidth (NULL); intHeight = image.getheight (NULL); BufferedImage BufferedImage=Newbufferedimage (width, height, bufferedimage.type_int_rgb); Graphics2D g=Bufferedimage.creategraphics (); G.drawimage (Image,0, 0, width, height,NULL); Image Waterimage= Imageio.read (NewFile (waterimg));//Watermark File                 intwidth_1 = Waterimage.getwidth (NULL); intHeight_1 = Waterimage.getheight (NULL);                                  G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha)); intWidthdiff = width-width_1; intHeightdiff = height-height_1; if(X < 0) {x= WIDTHDIFF/2; }Else if(X >Widthdiff) {x=Widthdiff; }                 if(Y < 0) {y= HEIGHTDIFF/2; }Else if(Y >Heightdiff) {y=Heightdiff; } g.drawimage (Waterimage, x, Y, width_1, height_1,NULL);//Watermark File EndG.dispose ();             Imageio.write (BufferedImage, pictrue_formate_jpg, file); } Catch(IOException e) {e.printstacktrace (); }     }      /*** Add text watermark *@paramtargetimg target picture path, such as: C://mypictrue//1.jpg *@parampresstext watermark text, such as: China Securities Network *@paramfontname font name, such as: Song Body *@paramFontStyle font styles, such as: Bold and Italic (font.bold| Font.Italic) *@paramfontSize font size in pixels *@paramColor Font *@paramThe offset of the X-watermark text from the left side of the target image, if x<0, in the middle of the@paramThe offset of the Y watermark text from the upper side of the target image, if y<0, in the middle of the@paramAlpha Transparency (0.0--1.0, 0.0 full transparent, 1.0 completely opaque)*/      Public Static voidPresstext (String targetimg, String Presstext, String fontname,intFontStyle,intFontSize, color color,intXintYfloatAlpha) {         Try{File File=NewFile (TARGETIMG); Image Image=imageio.read (file); intwidth = image.getwidth (NULL); intHeight = image.getheight (NULL); BufferedImage BufferedImage=Newbufferedimage (width, height, bufferedimage.type_int_rgb); Graphics2D g=Bufferedimage.creategraphics (); G.drawimage (Image,0, 0, width, height,NULL); G.setfont (NewFont (FontName, FontStyle, fontSize));             G.setcolor (color);                          G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha)); intwidth_1 = FontSize *GetLength (Presstext); intHeight_1 =fontSize; intWidthdiff = width-width_1; intHeightdiff = height-height_1; if(X < 0) {x= WIDTHDIFF/2; }Else if(X >Widthdiff) {x=Widthdiff; }             if(Y < 0) {y= HEIGHTDIFF/2; }Else if(Y >Heightdiff) {y=Heightdiff; } g.drawstring (Presstext, x, y+height_1);             G.dispose ();         Imageio.write (BufferedImage, pictrue_formate_jpg, file); } Catch(Exception e) {e.printstacktrace (); }     }          /*** Get character length, a Chinese character as 1 characters, an English letter as 0.5 characters *@paramtext *@returncharacter length, such as: text= "China", return 2;text= "test", return 2;text= "China ABC", return 4.*/      Public Static intgetlength (String text) {intTextLength =text.length (); intLength =textLength;  for(inti = 0; i < textLength; i++) {             if(String.valueof (Text.charat (i)). GetBytes (). length > 1) {length++; }         }         return(length% 2 = = 0)? LENGTH/2: LENGTH/2 + 1; }      /*** Image Zoom *@paramfilePath Picture Path *@paramHeight *@paramWidth *@paramif the BB ratio is not correct, need filler*/      Public Static voidResize (String FilePath,intHeightintWidthBooleanBB) {         Try {             DoubleRatio = 0;//Zoom ratioFile f =NewFile (FilePath); BufferedImage Bi=Imageio.read (f); Image itemp=bi.getscaledinstance (width, height, bufferedimage.scale_smooth); //Calculate scale             if((Bi.getheight () > height) | | (Bi.getwidth () >width)) {                    if(Bi.getheight () >bi.getwidth ()) {ratio= (NewInteger (height)). Doublevalue ()/bi.getheight (); } Else{ratio= (NewInteger (width)). Doublevalue ()/bi.getwidth (); } affinetransformop op=NewAffinetransformop (affinetransform.getscaleinstance (ratio, ratio),NULL); Itemp= Op.filter (BI,NULL); }                if(BB) {bufferedimage image=Newbufferedimage (width, height, bufferedimage.type_int_rgb); Graphics2D g=Image.creategraphics ();                    G.setcolor (Color.White); G.fillrect (0, 0, width, height); if(Width = = Itemp.getwidth (NULL)) G.drawimage (itemp,0, (Height-itemp.getheight (NULL))/2, Itemp.getwidth (NULL), Itemp.getheight (NULL), Color.White,NULL); ElseG.drawimage (itemp, (width-Itemp.getwidth (NULL))/2, 0, Itemp.getwidth (NULL), Itemp.getheight (NULL), Color.White,NULL);                    G.dispose (); Itemp=image; } imageio.write (BufferedImage) itemp,"JPG", F); } Catch(IOException e) {e.printstacktrace (); }     }       Public Static voidMain (string[] args)throwsIOException {//Add a text watermark//presstext ("d:/1.jpg", "Test", "Song Body", Font.Bold, Color.Red, 0.3f); //Picture ZoomResize ("D:/1.jpg", 100, 600,false); System.out.println ("The process is complete!" "); }      }

Picture tool class, image watermark, Text watermark, zoom, filler, etc.

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.