Import java. awt. Color;
Import java. awt. Font;
Import java. awt. Graphics;
Import java. awt. Image;
Import java. awt. image. BufferedImage;
Import java. io. File;
Import java. io. FileOutputStream;
Import javax. imageio. ImageIO;
Import com.sun.image.codec.jpeg. Unzip codec;
Import com.sun.image.codec.jpeg. encode imageencoder;
Public class ImgTest {
/***//**
* Print the image to the image.
* @ Param pressImg -- watermark File
* @ Param targetImg -- target file
* @ Param x
* @ Param y
*/
Public final static void pressImage (String pressImg, String targetImg, int x, int y ){
Try {
File _ file = new File (targetImg );
If (! _ File. exists ())
_ File. createNewFile ();
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
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 );
Required imageencoder encoder = required codec. createJPEGEncoder (out );
Encoder. encode (image );
Out. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
/***//**
* Print a text watermark image
* @ Param pressText -- text
* @ Param targetImg -- Target Image
* @ Param fontName -- body name
* @ Param fontStyle -- Font Style
* @ Param color -- font color
* @ Param 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 ){
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 );
Required imageencoder encoder = required codec. createJPEGEncoder (out );
Encoder. encode (image );
Out. close ();
} Catch (Exception e ){
System. out. println (e );
}
}
Public static void main (String [] args ){
// PressImage ("D: // 2.gif"," D: // 2.jpg", 20, 20 );
// PressText ("", targetImg, fontName, fontStyle, color, fontSize, x, y )("");
}
}
Author: ERDP Technical Architecture"