Package Com.hmw.picMark;
Import Java.awt.AlphaComposite;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import Java.awt.geom.AffineTransform;
Import java.awt.image.AffineTransformOp;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import Javax.imageio.ImageIO; /** * Picture Tool class, image watermark, Text watermark, zoom, filler, etc. * @author Carl He/public final class Imageutils {/** picture format: jpg*/private static final
String pictrue_formate_jpg = "JPG"; Private Imageutils () {}/** * Add picture watermark * @param targetimg target picture path, such as: C://mypictrue//1.jpg * @param waterimg watermark image path, such as: c:/ /mypictrue//logo.png * @param x watermark picture The offset from the left side of the target picture, if x<0, in the middle * @param y watermark picture is offset from the upper side of the target picture, if y<0, in the middle * @para M 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, I
NT x, int y, float Alpha) {try {File file = new file (targetimg);
Image image = Imageio.read (file); Intwidth = image.getwidth (null);
int height = image.getheight (null);
BufferedImage bufferedimage = new BufferedImage (width, height, bufferedimage.type_int_rgb);
Graphics2D g = bufferedimage.creategraphics ();
G.drawimage (image, 0, 0, width, height, null); Image waterimage = Imageio.read (new File (waterimg));
Watermark file int width_1 = Waterimage.getwidth (null);
int height_1 = Waterimage.getheight (null);
G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha));
int widthdiff = width-width_1;
int 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 End G.dispose ();
Imageio.write (BufferedImage, pictrue_formate_jpg, file);
catch (IOException e) {e.printstacktrace (); }
/** * Add text watermark * @param targetimg target image path, such as: C://mypictrue//1.jpg * @param presstext watermark text, such as: China Securities Net * @param font Name font names, such as: Arial * @param fontstyle font style, such as bold and italic (font.bold| Font.Italic * @param fontsize font size, in pixels * @param color Font colors * @param x Watermark text is offset from the left side of the target picture, if x<0, in the middle * @param Y watermark text from the offset on the top side of the target picture, if y<0, the alpha transparency (0.0--1.0, 0.0 is fully transparent, 1.0 is completely opaque) in the middle * @param * * public static void Presstex T (string targetimg, String Presstext, string fontname, int fontstyle, int fontsize, color color, int x, 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 bufferedimage = new BufferedImage (width, height, bufferedimage.type_int_rgb);
Graphics2D g = bufferedimage.creategraphics ();
G.drawimage (image, 0, 0, width, height, null);
G.setfont (New Font (FontName, FontStyle, FontSize));
G.setcolor (color); G.setcomposiTe (alphacomposite.getinstance (alphacomposite.src_atop, Alpha));
int width_1 = fontsize * GetLength (Presstext);
int height_1 = fontsize;
int widthdiff = width-width_1;
int 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 (Presstext, x, y + height_1);
G.dispose ();
Imageio.write (BufferedImage, pictrue_formate_jpg, file);
catch (Exception e) {e.printstacktrace (); /** * Gets character length, one Chinese character as 1 characters, one English letter as 0.5 characters * @param text * @return character length, such as: text= "China", return 2;text= "test", return 2;t
ext= "China ABC", returns 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; /** * Picture Scaling * @param filePath picture path * @param height Height * @param width width * @param bb If the ratio is not correct need filler/public static void Resize (String filePath, int height, int width, boolean bb) {try {double ratio = 0;//zoom ratio File
f = new File (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 = (new Integer (height)). Doublevalue ()/Bi.getheigh
T ();
else {ratio = (new Integer (width)). Doublevalue ()/bi.getwidth ();
} affinetransformop op = new Affinetransformop (affinetransform.getscaleinstance (ratio, ratio), null);
itemp = op.filter (bi, NULL); } if (BB) {bufferedimage image = new BufferedImage (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); else G.drawimage (itemp, (Width-itemp.getwidth (NULL))/2, 0, itemp.getwidth (null), itemp.getheight (null), Col
Or.white, NULL);
G.dispose ();
itemp = image;
} imageio.write ((bufferedimage) itemp, "JPG", f);
catch (IOException e) {e.printstacktrace (); } public static void Main (string[] args) throws IOException {pressimage ("c://pic//jpg", "C://pic//test.gif", 5000,
5000, 0f); Presstext ("C://pic//jpg", "Wang-Yin", "Song Body", font.bold|
Font.Italic, Color.Black, 0, 0, 8f);
Resize ("C://pic//4.jpg", 1000, true); }
}