Java Image Watermark

Source: Internet
Author: User

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;
 
/**
* Image tools, image watermarks, text watermarks, scaling, and whitelist
* @ Author Carl he
*/
Public final class imageutils {
/** Image format: JPG */
Private Static final string pictrue_formate_jpg = "jpg ";

Private imageutils (){}
/**
* Add an image watermark
* @ Param targetimg target image path, for example, C: // mypictrue // 1.jpg
* @ Param waterimg watermark image path, for example, C: // mypictrue // logo.png
* @ Param x the offset between the watermark image and the left side of the target image. If X is less than 0, it is in the middle.
* @ Param y the offset between the watermark image and the target image. If y is less than 0, it is in the middle.
* @ Param Alpha transparency (0.0 -- 1.0, 0.0 is completely transparent, and 1.0 is completely opaque)
*/
Public final static void pressimage (string targetimg, string waterimg, 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 = 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 end
G. Dispose ();
ImageIO. Write (bufferedimage, pictrue_formate_jpg, file );
} Catch (ioexception e ){
E. printstacktrace ();
}
}
 
/**
* Add a text watermark
* @ Param targetimg target image path, for example, C: // mypictrue // 1.jpg
* @ Param presstext watermark text, such as: China Securities Network
* @ Param fontname: font name, for example,
* @ Param fontstyle font style, such as bold and italic (font. Bold | font. italic)
* @ Param fontsize font size, in pixels
* @ Param color font color
* @ Param x the offset between the watermark text and the left side of the target image. If X is less than 0, it is in the middle.
* @ Param y the offset between the watermark text and the target image. If y is less than 0, it is in the middle.
* @ Param Alpha transparency (0.0 -- 1.0, 0.0 is completely transparent, and 1.0 is completely opaque)
*/
Public static void presstext (string targetimg, string presstext, string fontname, int fontstyle, int fontsize, 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 = 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 ();
}
}

/**
* Get the length of a character. One Chinese character serves as one character, and one English letter serves as 0.5 characters.
* @ Param text
* @ Return: returns 2; text = "test"; returns 2; text = "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;
}
 
/**
* Image Scaling
* @ Param filepath: Image path
* @ Param height
* @ Param width: width
* @ Param BB: whether to fill in the white when the proportion is incorrect
*/
Public static void resize (string filepath, int height, int width, Boolean bb ){
Try {
Double ratio = 0; // scaling ratio
File F = new file (filepath );
Bufferedimage Bi = ImageIO. Read (f );
Image itemp = Bi. getscaledinstance (width, height, bufferedimage. scale_smooth );

// Calculate the proportion
If (Bi. getheight ()> height) | (Bi. getwidth ()> width )){
If (Bi. getheight ()> bi. getwidth ()){
Ratio = (New INTEGER (height). doublevalue ()/Bi. getheight ();

} 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), color. 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,500 0, 0f );
Presstext ("C: // PIC // jpg", "Wang Zi printing", "", Font. bold | font. italic, 20, color. black, 0, 0, 8f );
Resize ("C: // PIC // 4.jpg", 1000,500, true );
}
}

Related Article

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.