package com.hmw.picmark; Import java.awt.alPhacomposite; Import Java.awt.Color; Import Java.awt.font; Import Java.Graphics2d; Import a.awt.image; import java.awt.geom.affinetransform ; Import java.awt.image.affinetransformop; Import Java.awt.image.buffRedimage; Import java.io.file; Import Java.io.ioxception; Import x.imageio.imageio;/*** picture tool category, picture water marks , Text watermark, zoom, supplement white, etc.*@AUTHOR CARL HE*/Public Final Class Imageutils {/** Image Format: JPG*/Private Static Final String Pictrue_Formate_JPG = "JPG"; PRIVATE Imageutils () {}/*** Add Image watermark * @param targetimg target picture path, such as: C: //myPictrue//1.jpg * @param watermg watermark picture path, such as: C: //myPictrue//logo.png * @Param x watermark picture distance target The offset on the left side of the picture, if x <0, then in the middle* @param y watermark picture distance from the upper side of the target picture, if y <0, in the middle* @param alpha transparency (0.0- -1.0, 0.0 is completely transparent, 1.0 is completely opaque) */Public Final Static Void Pressimage (String Targetimg, String Waterimg, Int X, Inty, Float Alpha) le (targetimg); Image Image = Imageio.read (File); int Width = Image.getWidth (NULL); int height = Image.getheight (NULL); buffredimage bufferedImage = new bufferedim Age (width, height, buffredimage.type_int_rgb); Graphics2d g = bufferedImage.creategraphics ( ); DrawImage (Image, 0, 0, Width, Height, NULL); Image Waterimage = Imageio.read (New File (Waterimg)); // Watermark file int width_1 = (null); int height_1 = WaterImage.getheight (null); G.Setcomposite (AlphaComposite.getInstance (alphaComposite.src_atop, Alpha)); int Widthdiff = Width_1; i nt heightdiff = 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 ending g.dispose (); Imageio.write (bufferedImage, PICTRUE_FORMATE_JPG, FILE);} Catch (IOEXCEPTION E) Stacktrace ();}}/*** add Text Watermark* @Param Targetimg target picture path, such as: C: //myPictrue//1.jpg* @param Presterstext Watermark text, such as: China Securities Network* @Param Fontname font name, such as: Song style* @param fontstyle font style style , Such as: Font.bold | FONT.ITALIC)* @param Fontsize font size, the unit is pixel* @param color font color* @param x watermark text is offset from the left side of the target picture, if x <0, in the middle* @param y watermark text, the offset of the upper side of the target picture, if Y <0, in the middle* @param alpha transparency (0.0 - 1.0, 0.0 is completely transparent, 1.0 is 1.0 is the Completely opaque) */Public Static Void Presterstext (String Targetimg, String Presterstext, String Fontname, Int FontStyle, Int Fontsize, Color COLOR, Int Y, FLOAT ALPHA) {try {file file = new file (targetimg); Image Image = Imageio.read (File); int Width = Image.getWidth (NULL); int height = Image.getheight (NULL); bufferedImage buffered = new buff EREDIMAGE (Width, Height, BufferedImage.type_int_rgb); Graphics2d g = buffredImage.createGraphics (); G.DRAWIMAGE (Image, 0, 0, Width, Height, NULL); G.Setfont (New Font (Fontname, FONTSTYLE, FONTSIZE)); G.SetComposite (ALPHA Composite.getinstance ( AlphaComposite.src_atop, Alpha); int Width_1 = fontsize * getlength (prestext); int height_1 = fontsize; int Widthdiff = width_1; t heightDiff = 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 (P. Restext, x, y + height_1); g.dispose (); Imageio.write (bufferedImage, Piction_Formate_jpg, File);} Catch (Exception E) {e.printstacktrace ();}/** * Get the character length, a Chinese character as 1 character , An English letter as 0.5 characters * @param text * @Return character length, such as: text = "China", return 2; text = "test", return 2; text = "China ABC", return 4. */ */ Public Static int GetLength (String Text) {int TextLength = Text.Length (); int length = textlength; for (int i = 0; I <textLength; I ++) {if . Valueof (text.charat (i)) .getBytes (). LENGTH> 1) {length ++;}} Return (length % 2 == 0)? Length / 2: specific / 2+1; Height height* @Param width width* @param BB proportion is not right to blank*/public static void resize (String filepath, int height, int Width, Boolean BB) {Try {Double Ratio = 0; // A zoom ratio File F = new file (filepath); bufferedImage bi = Imageio.ream (f); Image itemp = bi.getScaledinStance (Width, Height, buffredimage.scale_smooth); // Calculation ratio Example if ((BI.Getheight ()> Height) || (BI.GetWidth ()> Width) {if (bi.getheight ()> bi.getWidth ()) {ratio = (New Integer (Height)). DoubleValue () / bi.getheight (); O = (New Integer (Width). DoubleValue () / bi.getWidth ();} Affinetransformop OP = New Affinetransformop (AFFINETRANSFORM.GetScaleinstance, Ratio), null); itemp = OP.Filter (bi, null);} if (bb) {buffredImage Image = new bufferedImage (width, height, buffredimage.type_int_rgb); Graphics2d g = Image.creategraphics (); g.Setc OLOR (color.white); g.fillRed (0, 0, width, height) ; if (width == itmp.getWidth (null)) g.DrawImage (Itemp, 0, (Height -Itemp.getheight (NULL)) / 2, Itemp.getWidth (null), itemp.getheight ( null), color. White, null);;; Item -itemp.getWidth (null) / 2, 0, Itemp.getWidth (null), ITEMP.Getheight (NULL), COLOR.WHITE ll); g. dispose (); itemp = Image;} Imageio.write ((buffredimage) itmp, "jpg", f); iors (IOEXCEPTION E) {e.Printstacktrace ();} public IC VOID Main (String [] ARGS) ThROWS IOEXception {Pressimage ("C: // PIC // JPG", "C: //pic/test.gif", 5000, 5000, 0F); Presterstext ("C: // Pic // JPG", "旺仔之印", "宋体", Font.BOLD|Font.ITALIC, 20, Color.BLACK, 0, 0, 8f);resize("C://pic//4.jpg", 1000, 500, true);}}