packagecom.test1433;importjava.awt.AlphaComposite;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics2D;importjava.awt.Image;importjava.awt.RenderingHints;importjava.awt.geom.AffineTransform;importjava.awt.image.BufferedImage;importjava.awt.image.ColorModel;importjava.awt.image.WritableRaster;importjava.io.File;importjava.io.FileOutputStream;importjavax.imageio.ImageIO;importcom.sun.image.codec.jpeg.JPEGCodec;importcom.sun.image.codec.jpeg.JPEGImageEncoder;public classImageHelper {/*** 生成缩略图
保存:ImageIO.write(BufferedImage, imgType[jpg/png/...], File);
*
*@paramsource
* 原图片
*@paramwidth
* 缩略图宽
*@paramheight
* 缩略图高
*@paramb
* 是否等比缩放
**/
public static BufferedImage Thumb(BufferedImage source, intwidth,int height, booleanb) {//targetW,targetH分别表示目标长和宽
int type =source.getType();
BufferedImage target= null;double sx = (double) width /source.getWidth();double sy = (double) height /source.getHeight();if(b) {if (sx >sy) {
sx=sy;
width= (int) (sx *source.getWidth());
}else{
sy=sx;
height= (int) (sy *source.getHeight());
}
}if (type == BufferedImage.TYPE_CUSTOM) { //handmade
ColorModel cm =source.getColorModel();
WritableRaster raster=cm.createCompatibleWritableRaster(width,
height);boolean alphaPremultiplied =cm.isAlphaPremultiplied();
target= new BufferedImage(cm, raster, alphaPremultiplied, null);
}elsetarget= newBufferedImage(width, height, type);
Graphics2D g=target.createGraphics();//smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();returntarget;
}/*** 图片水印
*
*@paramimgPath
* 待处理图片
*@parammarkPath
* 水印图片
*@paramx
* 水印位于图片左上角的 x 坐标值
*@paramy
* 水印位于图片左上角的 y 坐标值
*@paramalpha
* 水印透明度 0.1f ~ 1.0f
**/
public static void waterMark(String imgPath, String markPath, int x, inty,floatalpha) {try{//加载待处理图片文件
Image img = ImageIO.read(newFile(imgPath));
BufferedImage image= new BufferedImage(img.getWidth(null), img
.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g=image.createGraphics();
g.drawImage(img,0, 0, null);//加载水印图片文件
Image src_biao = ImageIO.read(newFile(markPath));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
g.drawImage(src_biao, x, y,null);
g.dispose();//保存处理后的文件
FileOutputStream out = newFileOutputStream(imgPath);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}catch(Exception e) {
e.printStackTrace();
}
}/*** 文字水印
*
*@paramimgPath
* 待处理图片
*@paramtext
* 水印文字
*@paramfont
* 水印字体信息
*@paramcolor
* 水印字体颜色
*@paramx
* 水印位于图片左上角的 x 坐标值
*@paramy
* 水印位于图片左上角的 y 坐标值
*@paramalpha
* 水印透明度 0.1f ~ 1.0f*/
public static voidtextMark(String imgPath, String text, Font font,
Color color,int x, int y, floatalpha) {try{
Font Dfont= (font == null) ? new Font("宋体", 20, 13) : font;
Image img= ImageIO.read(newFile(imgPath));
BufferedImage image= new BufferedImage(img.getWidth(null), img
.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g=image.createGraphics();
g.drawImage(img,0, 0, null);
g.setColor(color);
g.setFont(Dfont);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
g.drawString(text, x, y);
g.dispose();
FileOutputStream out= newFileOutputStream(imgPath);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}catch(Exception e) {
System.out.println(e);
}
}
}