//只需修改main方法里的图片路径就可以调试或运行了
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.text.AttributedString;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.text.AttributedCharacterIterator;
public class ImageHandle {
public ImageHandle() {
}
/**
* 给图片添加水印(只支持jpeg/jpg格式)
*
* @param filePath
* 需要添加水印的图片的路径
* @param markContent
* 水印的文字
* @param markContentColor
* 水印文字的颜色
* @param qualNum
* 图片质量
* @param fontType
* 字体
* @param fontsize
* 字体大小
* @return boolean
*/
public boolean createMark(String filePath, String markContent,
Color markContentColor, float qualNum, String fontType, int fontSize) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null);
AttributedString ats = new AttributedString(markContent);
Font f = new Font(fontType, Font.BOLD, fontSize);
ats.addAttribute(TextAttribute.FONT, f, 0, markContent.length());
AttributedCharacterIterator iter = ats.getIterator();
g.drawString(iter,0,f.getSize()); // 添加水印的文字和设置水印文字出现的位置(左上角)
g.dispose();
try {
FileOutputStream out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(qualNum, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
return false;
}
return true;
}
/**
* 生成图片的缩略图
*
* @param filePath
* 图片的路径
* @param width
* 希望生成的缩略图的宽度
* @param height
* 希望生成的缩略图的高度
* @return Icon
*/
public Icon createFixedBoundIcon(String filePath, int width, int height)
throws Exception {
File F = new File(filePath);
double Ratio = 0.0; // 缩放比例
if (!F.isFile())
throw new Exception(F
+ " is not image file error in getFixedBoundIcon! ");
Icon icon = new ImageIcon(filePath);
BufferedImage Bi = ImageIO.read(F);
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();
}
int lastLength = filePath.lastIndexOf(".");
String subFilePath = filePath.substring(0, lastLength);
String fileName=this.GetFileName(subFilePath);//图片的文件名
String fileTypes=filePath.substring(lastLength+1); //图片的类型
File zoomFile = new File(subFilePath+"_" +fileName+"."+fileTypes);//图片的完整路径
Image Itemp = Bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
Itemp = op.filter(Bi, null);
try {
ImageIO.write((BufferedImage) Itemp,"jpg", zoomFile);
icon = new ImageIcon(zoomFile.getPath()); //设定输出路径
} catch (Exception ex) {
ex.printStackTrace();
}
}
return icon;
}
/**
* 获取文件路径中的文件名
*
* @param filePath
* 文件的路径
* @return String
*/
public String GetFileName(String filepath) {
String returnstr = "*.*";
int length = filepath.trim().length();
filepath = filepath.replace('//', '/');
if (length > 0) {
int i = filepath.lastIndexOf("/");
if (i >= 0) {
filepath = filepath.substring(i + 1);
returnstr = filepath;
}
}
return returnstr;
}
public static void main(String[] args)
{
ImageHandle wm = new ImageHandle();
try{
wm.createMark("G://q1.jpg","注册商标",Color.BLUE,5f,"宋体",12);
System.out.println("为图片添加了水印!");
wm.createFixedBoundIcon("G://q3.gif",128,128);
System.out.println("为图片生成了缩略图!");
}catch(Exception e){
e.printStackTrace();
}
}
}