加水印并且可以加缩略图的,.水印图最好用透明的png图,
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.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageUtil {
private String sourceDir; // 图片的存放路径
private String destinationDir; // 缩略图的存放路径
private String mode; // 生成缩略图的模式,可选ScaleOnly或ClipAndScale
private String width; // 缩略图的宽度
private String height; // 缩略图的高度
private int sourceWidth;
private int sourceHeight;
// 以下代码段是为了使用Spring注入属性
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;
}
// 生成缩略图的方法,默认缩略图的文件名和原图相同,存放路径不同
public void createMicroImage(String fileName)
throws Exception {
// 判断sourceDir的格式是否为以"\"结尾,并生成完整的路径
String sourceFileName;
String destinationFileName;
if (sourceDir.lastIndexOf('\\') != (sourceDir.length() - 1 )) {
sourceFileName=sourceDir+"\\"+fileName;
destinationFileName = destinationDir+"\\"+fileName;
}
else {
sourceFileName = sourceDir+fileName;
destinationFileName =destinationDir+fileName;
}
//加水印
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);
//水印文件
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);
//水印文件结束
g.dispose();
FileOutputStream out = new FileOutputStream(sourceFileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(imagewater);
out.close();
// 创建文件,并判断原文件是否存在
File sourceFile=new File(sourceDir+"\\"+fileName);
if (!sourceFile.exists()) {
// throw new Exception();
System.out.println("文件不存在");
}
// 根据扩展名判断原文件的格式
String extension = fileName.substring(fileName.lastIndexOf('.') + 1 );
if ( ! extension.equalsIgnoreCase("jpg") && ! extension.equalsIgnoreCase("bmp")
&& ! extension.equalsIgnoreCase("gif") && ! extension.equalsIgnoreCase("png")) {
// throw new Exception();
System.out.println("扩展名格式错误");
}
// 判断缩略图的宽度和高度是否正确,如果不能正确解析则抛出异常
int destinationWidth = Integer.parseInt(width);
int destinationHeight = Integer.parseInt(height);
// 判断缩放模式是否正确,如果配置错误,则抛出异常
if (!mode.equalsIgnoreCase("ScaleOnly")
&& ! mode.equalsIgnoreCase( " ClipAndScale " )) {
throw new Exception();
}
// 读取图像文件,并创建BufferedImage对象,如果不能读取,则抛出异常
BufferedImage image = null ;
image = ImageIO.read(sourceFile);
if (image == null ) {
// throw new Exception();
System.out.println("不能读取");
}
// 获取原图像文件的高度和宽度
sourceWidth = image.getWidth();
sourceHeight = image.getHeight();
// 生成缩略图
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 );
}
// 如果是bmp或者gif,则缩略图为png格式
if (extension.equalsIgnoreCase("bmp") || extension.equalsIgnoreCase("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) {
// 先裁减
int x = sourceWidth - ( int )(sourceHeight * (( float )destinationWidth / destinationHeight));
Image clipedImage = image.getSubimage(( int )( 0.5 * x), 0 , ( int )(sourceHeight * (( float )destinationWidth / destinationHeight)), sourceHeight);
// 后缩放
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 {
// 先裁减
int y = sourceHeight - ( int )(sourceWidth * (( float )destinationHeight / destinationWidth));
Image clipedImage = image.getSubimage( 0 , ( int )( 0.5 * y), sourceWidth, ( int )(sourceWidth * (( float )destinationHeight / destinationWidth)));
// 后缩放
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 );
}
// 如果是bmp或者gif,则缩略图为png格式
if (extension.equalsIgnoreCase("bmp" ) || extension.equalsIgnoreCase("gif" )) {
extension="png" ;
destinationFileName = destinationFileName.substring( 0 , destinationFileName.lastIndexOf('.')) + " . " + extension;
}
File destinationFile = new File(destinationFileName);
ImageIO.write(destinationImage, extension, destinationFile);
}
}
主程序中调用时,可以这样:
ImageUtil util = new ImageUtil();
util.setSourceDir(uploaddir);
util.setDestinationDir(suoluedir);
util.setWidth("125");
util.setHeight("125");
// util.pressImage(watermarkFile,srcFile,0,0);
// 以仅缩放的形式生成缩略图
util.setMode("ScaleOnly");
util.createMicroImage(srcFile);