Add a watermark and add thumbnails. It is best to use a transparent PNG image for a watermark image,
Import java. AWT. image. bufferedimage;
Import java. Io. file;
Import java. AWT. image;
Import java. AWT. graphics2d;
Import java. util. Random;
Import java. AWT. Font;
Import java. AWT. color;
Import java. AWT. Font;
Import java. AWT. graphics;
Import java. AWT. image;
Import java. AWT. image. bufferedimage;
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 imageutil {
Private string sourcedir; // path for storing images
Private string destinationdir; // storage path of the thumbnail
Private string mode; // the mode in which thumbnails are generated. You can choose scaleonly or clipandscale.
Private string width; // The width of the thumbnail.
Private string height; // The height of the thumbnail.
Private int sourcewidth;
Private int sourceheight;
// The following Code To use spring injection attributes.
Public void setcharacterstorage (string characterstorage ){
This. characterstorage = characterstorage;
}
Public void setdestinationdir (string destinationdir ){
This. destinationdir = destinationdir;
}
Public void setheight (string height ){
This. Height = height;
}
Public void setmode (string mode ){
This. mode = mode;
}
Public void setsourcedir (string sourcedir ){
This. sourcedir = sourcedir;
}
Public void setwidth (string width ){
This. width = width;
}
// The method for generating thumbnails. By default, the file name of the thumbnail is the same as that of the source image, and the storage path is different.
Public void createmicroimage (string filename)
Throws exception {
// Determine whether the sourcedir format ends with "\" and generate the complete path
String sourcefilename;
String destinationfilename;
If (sourcedir. lastindexof ('\\')! = (Sourcedir. Length ()-1 )){
Sourcefilename = sourcedir + "\" + filename;
Destinationfilename = destinationdir + "\" + filename;
}
Else {
Sourcefilename = sourcedir + filename;
Destinationfilename = destinationdir + filename;
}
// Add a watermark
String pressimg = "d :\\ tomcat6 \ webapps \ springlover \ images \ nanshawatermark.png ";
File _ file = new file (sourcefilename );
Image src = ImageIO. Read (_ file );
Int widethw = SRC. getwidth (null );
Int heightw = SRC. getheight (null );
Bufferedimage imagewater = new bufferedimage (widethw, heightw,
Bufferedimage. type_int_rgb );
Graphics G = imagewater. creategraphics ();
G. drawimage (SRC, 0, 0, widethw, heightw, 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, (widethw-wideth_biao)/2,
(Heightw-height_biao)/2, wideth_biao, height_biao, null );
// Watermark file ended
G. Dispose ();
Fileoutputstream out = new fileoutputstream (sourcefilename );
Required imageencoder encoder = required codec. createjpegencoder (out );
Encoder. encode (imagewater );
Out. Close ();
// Create a file and determine whether the original file exists
File sourcefile = new file (sourcedir + "\" + filename );
If (! Sourcefile. exists ()){
// Throw new exception ();
System. Out. println ("file does not exist ");
}
// Determine the format of the original file based on the extension
String extension = filename. substring (filename. lastindexof ('.') + 1 );
If (! Extension. equalsignorecase ("jpg ")&&! Extension. semantic signorecase ("BMP ")
&&! Extension. inclusignorecase ("GIF ")&&! Extension. inclusignorecase ("PNG ")){
// Throw new exception ();
System. Out. println ("Incorrect extension format ");
}
// Determine whether the width and height of the thumbnail are correct. If the width and height of the thumbnail cannot be correctly parsed, an exception is thrown.
Int destinationwidth = integer. parseint (width );
Int destinationheight = integer. parseint (height );
// Determine whether the scaling mode is correct. If the configuration is incorrect, an exception is thrown.
If (! Mode. equalsignorecase ("scaleonly ")
&&! Mode. equalsignorecase ("clipandscale ")){
Throw new exception ();
}
// Read the image file and create a bufferedimage object. If the object cannot be read, an exception is thrown.
Bufferedimage image = NULL;
Image = ImageIO. Read (sourcefile );
If (image = NULL ){
// Throw new exception ();
System. Out. println ("cannot read ");
}
// Obtain the height and width of the original image file
Sourcewidth = image. getwidth ();
Sourceheight = image. getheight ();
// Generate a thumbnail
If (mode. equalsignorecase ("scaleonly ")){
Bufferedimage destinationimage;
If (float) sourcewidth/destinationwidth> (float) sourceheight/destinationheight ){
Image tempimage = image. getscaledinstance (destinationwidth, (INT) (destinationwidth * (float) sourceheight/sourcewidth), image. scale_default );
Destinationimage = new bufferedimage (destinationwidth, (INT) (destinationwidth * (float) sourceheight/sourcewidth), bufferedimage. type_int_rgb );
Graphics2d graphics = destinationimage. creategraphics ();
Graphics. drawimage (tempimage, 0, 0, null );
} Else {
Image tempimage = image. getscaledinstance (INT) (destinationheight * (float) sourcewidth/sourceheight), destinationheight, image. scale_default );
Destinationimage = new bufferedimage (INT) (destinationheight * (float) sourcewidth/sourceheight), destinationheight, bufferedimage. type_int_rgb );
Graphics2d graphics = destinationimage. creategraphics ();
Graphics. drawimage (tempimage, 0, 0, null );
}
// If it is BMP or GIF, the thumbnail is in PNG format
If (extension. inclusignorecase ("BMP") | extension. inclusignorecase ("GIF ")){
Extension = "PNG ";
Destinationfilename = destinationfilename. substring (0, destinationfilename. lastindexof ('.') + "." + extension;
}
File destinationfile = new file (destinationfilename );
ImageIO. Write (destinationimage, extension, destinationfile );
} Else {
Bufferedimage destinationimage;
If (float) sourcewidth/destinationwidth> (float) sourceheight/destinationheight ){
// Cut first
Int x = sourcewidth-(INT) (sourceheight * (float) destinationwidth/destinationheight ));
Image clipedimage = image. getsubimage (INT) (0.5 * X), 0, (INT) (sourceheight * (float) destinationwidth/destinationheight), sourceheight );
// Zoom back
Image scaledimage = clipedimage. getscaledinstance (destinationwidth, destinationheight, image. scale_default );
Destinationimage = new bufferedimage (destinationwidth, destinationheight, bufferedimage. type_int_rgb );
Graphics2d graphics = destinationimage. creategraphics ();
Graphics. drawimage (scaledimage, 0, 0, null );
} Else {
// Cut first
Int y = sourceheight-(INT) (sourcewidth * (float) destinationheight/destinationwidth ));
Image clipedimage = image. getsubimage (0, (INT) (0.5 * Y), sourcewidth, (INT) (sourcewidth * (float) destinationheight/destinationwidth )));
// Zoom back
Image scaledimage = clipedimage. getscaledinstance (destinationwidth, destinationheight, image. scale_default );
Destinationimage = new bufferedimage (destinationwidth, destinationheight, bufferedimage. type_int_rgb );
Graphics2d graphics = destinationimage. creategraphics ();
Graphics. drawimage (scaledimage, 0, 0, null );
}
// If it is BMP or GIF, the thumbnail is in PNG format
If (extension. inclusignorecase ("BMP") | extension. inclusignorecase ("GIF ")){
Extension = "PNG ";
Destinationfilename = destinationfilename. substring (0, destinationfilename. lastindexof ('.') + "." + extension;
}
File destinationfile = new file (destinationfilename );
ImageIO. Write (destinationimage, extension, destinationfile );
}
}
MasterProgramWhen calling, you can do this:
Imageutil util = new imageutil ();
Util. setsourcedir (uploaddir );
Util. setdestinationdir (suoluedir );
Util. setwidth ("125 ");
Util. setheight ("125 ");
// Util. pressimage (watermarkfile, srcfile, 0, 0 );
// Generate a thumbnail in the form of scaling only
Util. setmode ("scaleonly ");
Util. createmicroimage (srcfile );