packagecom.hmw.picMark;
importjava.awt.AlphaComposite;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics2D;
importjava.awt.Image;
importjava.awt.geom.AffineTransform;
importjava.awt.image.AffineTransformOp;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
/**
*图片工具类,图片水印,文字水印,缩放,补白等
*@authorCarlHe
*/
publicfinalclassImageUtils{
/**图片格式:JPG*/
privatestaticfinalStringPICTRUE_FORMATE_JPG="jpg";
privateImageUtils(){}
/**
*添加图片水印
*@paramtargetImg目标图片路径,如:C://myPictrue//1.jpg
*@paramwaterImg水印图片路径,如:C://myPictrue//logo.png
*@paramx水印图片距离目标图片左侧的偏移量,如果x<0,则在正中间
*@paramy水印图片距离目标图片上侧的偏移量,如果y<0,则在正中间
*@paramalpha透明度(0.0--1.0,0.0为完全透明,1.0为完全不透明)
*/
publicfinalstaticvoidpressImage(StringtargetImg,StringwaterImg,intx,inty,floatalpha){
try{
Filefile=newFile(targetImg);
Imageimage=ImageIO.read(file);
intwidth=image.getWidth(null);
intheight=image.getHeight(null);
BufferedImagebufferedImage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2Dg=bufferedImage.createGraphics();
g.drawImage(image,0,0,width,height,null);
ImagewaterImage=ImageIO.read(newFile(waterImg)); //水印文件
intwidth_1=waterImage.getWidth(null);
intheight_1=waterImage.getHeight(null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
intwidthDiff=width-width_1;
intheightDiff=height-height_1;
if(x<0){
x=widthDiff/2;
}elseif(x>widthDiff){
x=widthDiff;
}
if(y<0){
y=heightDiff/2;
}elseif(y>heightDiff){
y=heightDiff;
}
g.drawImage(waterImage,x,y,width_1,height_1,null);//水印文件结束
g.dispose();
ImageIO.write(bufferedImage,PICTRUE_FORMATE_JPG,file);
}catch(IOExceptione){
e.printStackTrace();
}
}
/**
*添加文字水印
*@paramtargetImg目标图片路径,如:C://myPictrue//1.jpg
*@parampressText水印文字,如:中国证券网
*@paramfontName字体名称, 如:宋体
*@paramfontStyle字体样式,如:粗体和斜体(Font.BOLD|Font.ITALIC)
*@paramfontSize字体大小,单位为像素
*@paramcolor字体颜色
*@paramx水印文字距离目标图片左侧的偏移量,如果x<0,则在正中间
*@paramy水印文字距离目标图片上侧的偏移量,如果y<0,则在正中间
*@paramalpha透明度(0.0--1.0,0.0为完全透明,1.0为完全不透明)
*/
publicstaticvoidpressText(StringtargetImg,StringpressText,StringfontName,intfontStyle,intfontSize,Colorcolor,intx,inty,floatalpha){
try{
Filefile=newFile(targetImg);
Imageimage=ImageIO.read(file);
intwidth=image.getWidth(null);
intheight=image.getHeight(null);
BufferedImagebufferedImage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2Dg=bufferedImage.createGraphics();
g.drawImage(image,0,0,width,height,null);
g.setFont(newFont(fontName,fontStyle,fontSize));
g.setColor(color);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
intwidth_1=fontSize*getLength(pressText);
intheight_1=fontSize;
intwidthDiff=width-width_1;
intheightDiff=height-height_1;
if(x<0){
x=widthDiff/2;
}elseif(x>widthDiff){
x=widthDiff;
}
if(y<0){
y=heightDiff/2;
}elseif(y>heightDiff){
y=heightDiff;
}
g.drawString(pressText,x,y+height_1);
g.dispose();
ImageIO.write(bufferedImage,PICTRUE_FORMATE_JPG,file);
}catch(Exceptione){
e.printStackTrace();
}
}
/**
*获取字符长度,一个汉字作为1个字符,一个英文字母作为0.5个字符
*@paramtext
*@return字符长度,如:text="中国",返回2;text="test",返回2;text="中国ABC",返回4.
*/
publicstaticintgetLength(Stringtext){
inttextLength=text.length();
intlength=textLength;
for(inti=0;i<textLength;i++){
if(String.valueOf(text.charAt(i)).getBytes().length>1){
length++;
}
}
return(length%2==0)?length/2:length/2+1;
}
/**
*图片缩放
*@paramfilePath图片路径
*@paramheight高度
*@paramwidth宽度
*@parambb比例不对时是否需要补白
*/
publicstaticvoidresize(StringfilePath,intheight,intwidth,booleanbb){
try{
doubleratio=0;//缩放比例
Filef=newFile(filePath);
BufferedImagebi=ImageIO.read(f);
Imageitemp=bi.getScaledInstance(width,height,BufferedImage.SCALE_SMOOTH);
//计算比例
if((bi.getHeight()>height)||(bi.getWidth()>width)){
if(bi.getHeight()>bi.getWidth()){
ratio=(newInteger(height)).doubleValue()/bi.getHeight();
}else{
ratio=(newInteger(width)).doubleValue()/bi.getWidth();
}
AffineTransformOpop=newAffineTransformOp(AffineTransform.getScaleInstance(ratio,ratio),null);
itemp=op.filter(bi,null);
}
if(bb){
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2Dg=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(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args)throwsIOException{
pressImage("C://pic//jpg","C://pic//test.gif",5000,5000,0f);
pressText("C://pic//jpg","旺仔之印","宋体",Font.BOLD|Font.ITALIC,20,Color.BLACK,0,0,8f);
resize("C://pic//4.jpg",1000,500,true);
}
}