Package cn.itcast;
Import java.awt.*;
Import java.io.*;
Import java.awt.image.*;
Import com.sun.image.codec.jpeg.*;
Import javax.imageio.*; Public final class Imageutils {public Imageutils () {}/** * Print the picture to the picture * * @param pressimg Watermark file * @param targ Etimg Target file * @param x * @param y */public final static void Pressimage (String pressimg, string targetimg, int x,
int y) {system.setproperty ("java.awt.headless", "true");
try {file _file = new file (targetimg);
Image src = imageio.read (_file);
int wideth = Src.getwidth (null);
int height = src.getheight (null);
BufferedImage image = New BufferedImage (wideth, height, bufferedimage.type_int_rgb);
Graphics g = image.creategraphics ();
G.drawimage (SRC, 0, 0, wideth, height, null);
Watermark File _filebiao = new file (pressimg);
Image Src_biao = Imageio.read (_filebiao);
int wideth_biao = Src_biao.getwidth (null);
int height_biao = Src_biao.getheight (null); G.drawimage (src_biao, Wideth-wideth_biao-x, Height-height_biao-y, Wideth_biao, Height_biao, NULL);
G.dispose ();
FileOutputStream out = new FileOutputStream (targetimg);
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (image);
Out.close ();
catch (Exception e) {e.printstacktrace (); /** * Print Text watermark picture * * @param presstext *--Text * @param targetimg *-Target picture * @param FontName *--font name * @param fontstyle *--font style * @param color *--Font Color * @para M fontsize *--Font Size * @param x *--offset * @param y */public static void Presstext (String Presstext, String targetimg, string fontname, int fontstyle, int color, int fontsize, int x, int y) {SYSTEM.SETP
Roperty ("Java.awt.headless", "true");
try {file _file = new file (targetimg);
Image src = imageio.read (_file);
int wideth = Src.getwidth (null); int height = SRC.GEtheight (NULL);
BufferedImage image = New BufferedImage (wideth, height, bufferedimage.type_int_rgb);
Graphics g = image.creategraphics ();
G.drawimage (SRC, 0, 0, wideth, height, null);
String s= "www.qhd.com.cn";
G.setcolor (color.red);
G.setfont (New Font (FontName, FontStyle, FontSize));
g.DrawString (Presstext, Wideth-fontsize-x, Height-fontsize/2-y);
G.dispose ();
FileOutputStream out = new FileOutputStream (targetimg);
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (image);
Out.close ();
catch (Exception e) {System.out.println (e);
} public static void Main (string[] args) {pressimage ("e:/logo/50.jpg", "e:/logo/logo3.jpg", 300, 300); }
}