Java makes watermark effects, fonts and images, adjusts transparency, and uses the Jimi class library.

Source: Internet
Author: User

 Original Image



Watermark to be added



Prepared Image



Source code

Import java. Io. ioexception;
Import java. AWT. graphics;
Import java. AWT .*;
Import java. AWT. image;
Import java. AWT. image. bufferedimage;
Import java. AWT. image. imageproducer;
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;
Import com. Sun. Jimi. Core. Jimi;
Import com. Sun. Jimi. Core. jimiexception;
Import com. Sun. Jimi. Core. jimiwriter;
Import com. Sun. Jimi. Core. Options. jpgoptions;

/**
* Use Java to add watermarks, fonts, and images to adjust transparency.
*
* @ Author myjava_024, old zizhu Java century network (java2000.net)
*
*/
Public class t {

/**
* Convert an image of any format to a jpg image source original image path DEST the path of the converted Image
*/
Public void toreduce (string source, string DEST ){
Try {
Jpgoptions Options = new jpgoptions ();
Options. setquality (100 );
// The image format is within the range of Jimi's decoding format
Imageproducer image = Jimi. getimageproducer (source );
// Image Encoding
Jimiwriter writer = Jimi. createjimiwriter (DEST );
Writer. setsource (image );
// Set the attributes of the output file. The attributes of each format are different.
Writer. setoptions (options );
Writer. putimage (DEST );
} Catch (jimiexception je ){
System. Err. println ("error:" + je );
Je. printstacktrace ();
}
}

/**
* Scale down the image extraction and control the size of the generated image. oldfile: scaled down the original image path newfile. The image path wideths is scaled down to the image width.
* Height of the scaled image after heights
*/
Public void changdimension (string oldfile, string newfile, int wideths,
Int heights) throws jimiexception, ioexception {
String temp = newfile;
File inputfile = NULL;
This. toreduce (oldfile, temp );
// Read the file
Inputfile = new file (temp );
// Construct an image object
Image src = ImageIO. Read (inputfile );
// Double Wideth = (double) SRC. getwidth (null); // obtain the source image width.
// Double height = (double) SRC. getheight (null); // obtain the source image length.
Int iwideth = wideths;
Int iheight = heights;
Bufferedimage tag = NULL;
Try {
Tag = new bufferedimage (iwideth, iheight, bufferedimage. type_int_rgb );
// Draw the reduced Graph
Tag. getgraphics (). drawimage (SRC, 0, 0, iwideth, iheight, null );
Jimiwriter writer = Jimi. createjimiwriter (newfile );
Writer. setsource (TAG );
Writer. putimage (newfile );
} Catch (exception e ){
E. printstacktrace ();
}
}

/**
* Adding an oldfile newfile to an image is unnecessary. Text is the text added to the image.
*/
Public void imagefont (string oldfile, string newfile, string text ){
Image IMG;
String filename = oldfile. substring (oldfile. lastindexof (".") + 1 );
String temp = newfile;

Try {
If (! Filename. Equals ("jpg ")){
This. toreduce (oldfile, newfile );
IMG = ImageIO. Read (new file (temp ));
} Else {
IMG = ImageIO. Read (new file (oldfile ));
}
Int width = IMG. getwidth (null );
Int Height = IMG. getheight (null );
Bufferedimage image = new bufferedimage (width, height,
Bufferedimage. type_int_rgb );
Graphics G = image. creategraphics ();

G. drawimage (IMG, 0, 0, width, height, null );
G. setcolor (color. Red );
G. setfont (new font ("Courier", Font. hanging_baseline, 40 ));
G. drawstring (text, width-360, height-72 );
G. Dispose ();

Fileoutputstream OS = new fileoutputstream (temp );
Required imageencoder encoder = required codec. createjpegencoder (OS );
Encoder. encode (image );

} Catch (exception e ){
E. getmessage ();
}
}

/**
* Add a watermark to the image and control the position and transparency of the watermark image. oldfile newfile does not mean the path of the watermark image to be added in syoldfile. t
* Position added to the watermark image to the transparency of the watermark image
*/
Public void imagese (string oldfile, string newfile, string syoldfile, int t,
Float ){
// File type
String filename = oldfile. substring (oldfile. lastindexof (".") + 1 );
System. Out. println (filename );
Image IMG;
String temp = newfile;
Float alpha =;

Try {
If (! Filename. Equals ("jpg ")){
Toreduce (oldfile, newfile );
IMG = ImageIO. Read (new file (temp ));
System. Out. println ("Create a watermark image for other types of images ");
} Else {
IMG = ImageIO. Read (new file (oldfile ));
}
Int width = IMG. getwidth (null );
Int Height = IMG. getheight (null );
Int Ws = width/3;
Int HS = height/3;


Bufferedimage image = new bufferedimage (width, height,
Bufferedimage. type_int_rgb );
Graphics2d G = image. creategraphics ();
G. drawimage (IMG, 0, 0, width, height, null );
Image logo = ImageIO. Read (new file (syoldfile ));
Int LW = logo. getwidth (null );
Int LH = logo. getheight (null );
// Sets the image transparency.
G
. Setcomposite (alphacomposite. getinstance (alphacomposite. src_atop,
Alpha ));
G. drawimage (logo, null, null );

Switch (t ){
Case 1:
G. drawimage (logo, 0, 0, lw, LH, null );
System. Out. println ("layout 1 ");
Break;
Case 2:
G. drawimage (logo, WS, 0, lw, LH, null );
System. Out. println ("layout 2 ");
Break;
Case 3:
G. drawimage (logo, 2 * WS, 0, lw, LH, null );
System. Out. println ("layout 3 ");
Break;
Case 4:
G. drawimage (logo, 0, HS, lw, LH, null );
System. Out. println ("Layout 4 ");
Break;
Case 5:
G. drawimage (logo, WS, HS, lw, LH, null );
System. Out. println ("layout 5 ");
Break;
Case 6:
G. drawimage (logo, 2 * WS, HS, lw, LH, null );
System. Out. println ("layout 6 ");
Break;
Case 7:
G. drawimage (logo, 0, 2 * HS, lw, LH, null );
System. Out. println ("layout 7 ");
Break;
Case 8:
G. drawimage (logo, WS, 2 * HS, lw, LH, null );
System. Out. println ("layout 8 ");
Break;
Case 9:
G. drawimage (logo, 2 * WS, 2 * HS, lw, LH, null );
System. Out. println ("layout 9 ");
Break;
Default:
Break;
}
G. setcomposite (alphacomposite. getinstance (alphacomposite. src_over ));
G. Dispose ();
// Generate an image
Fileoutputstream OSS = new fileoutputstream (temp );
Required imageencoder encoders = required codec. createjpegencoder (OSS );
Encoders. encode (image );
} Catch (exception e ){
E. getmessage ();
E. printstacktrace ();
}
}

Public static void main (string [] ARGs ){
T test = new T ();
// Alpha indicates transparency. Alpha must be a floating point number in the range of [0.0, 1.0] (including the boundary value ).
Float alpha = 0.1f;
// Generate a watermark image

Test. imagese ("D:/eclipse.jpg", "d:/eclipse_java2000.jpg ",
"D:/java2000.jpg", 1, alpha );
/*
* Test. imagese ("D:/001.jpg"," d:/AA/001.jpg", "d:/002.jpg", 1, alpha );
* Test. imagese ("D:/001.jpg"," d:/AA/002.jpg", "d:/002.jpg", 2, alpha );
* Test. imagese ("D:/001.jpg"," d:/AA/003.jpg", "d:/002.jpg", 3, alpha );
* Test. imagese ("D:/001.jpg"," d:/AA/004.jpg", "d:/002.jpg", 4, alpha );
* Test. imagese ("D:/001.jpg"," d:/AA/005.jpg", "d:/002.jpg", 5, alpha );
* Test. imagese ("D:/001.jpg"," d:/AA/006.jpg", "d:/002.jpg", 6, alpha );
* Test. imagese ("D:/001.jpg"," d:/AA/007.jpg", "d:/002.jpg", 7, alpha );
* Test. imagese ("D:/001.jpg"," d:/AA/008.jpg", "d:/002.jpg", 8, alpha );
* Test. imagese ("D:/001.jpg"," d:/AA/009.jpg", "d:/002.jpg", 9, alpha );
*/

}
}

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.