JAVA adds watermarks and text to images

Source: Internet
Author: User

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"

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.