一、前言
通过javax.imageio包定义ImageUtils图像工具类,实现图像snapshot快照、图片添加pressText文字水印、给图片添加pressImage图片水印及计算text的长度等代码示例。
二、代码示例
import java.awt.AlphaComposite;@b@import java.awt.Color;@b@import java.awt.Font;@b@import java.awt.Graphics;@b@import java.awt.Graphics2D;@b@import java.awt.Image;@b@import java.awt.Rectangle;@b@import java.awt.image.BufferedImage;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.InputStream;@b@import java.util.Iterator;@b@@b@import javax.imageio.ImageIO;@b@import javax.imageio.ImageReadParam;@b@import javax.imageio.ImageReader;@b@import javax.imageio.stream.ImageInputStream;@b@@b@public class ImageUtils {@b@@b@ /**@b@ * 图像快照@b@ * @param srcImageFile 源图像文件地址@b@ * @param result 快照的图像地址@b@ * @param height 快照的高度@b@ * @param width 快照的宽度 @b@ */@b@ public final static void snapshot(String srcImageFile, String result, int width, int height) throws Exception {@b@ if (srcImageFile == null || result == null || height == 0 || width == 0) {@b@ return ;@b@ }@b@ Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName("jpg"); @b@ InputStream in = null;@b@ ImageInputStream iis = null;@b@ try {@b@ ImageReader reader = (ImageReader)iterator.next(); @b@ in = new FileInputStream(srcImageFile); @b@ iis = ImageIO.createImageInputStream(in); @b@ reader.setInput(iis, true); @b@ ImageReadParam param = reader.getDefaultReadParam(); @b@ int imageIndex = 0; @b@ int srcWidth = reader.getWidth(imageIndex);@b@ int srcHeight = reader.getHeight(imageIndex);@b@ if (srcWidth <= 0 || srcHeight <= 0) {@b@ return ;@b@ } else if (srcWidth == width && srcHeight == height && result.equals(srcImageFile)) {@b@ return ;@b@ }@b@ double ratioW = (double)srcWidth / width;@b@ double ratioH = (double)srcHeight / height;@b@ double ratio = ratioW > ratioH ? ratioH : ratioW;@b@ int destWidth = (int) (width * ratio);@b@ int destHeight = (int) (height * ratio);@b@ Rectangle rect = new Rectangle((srcWidth-destWidth)/2, (srcHeight-destHeight)/2, destWidth, destHeight); @b@ param.setSourceRegion(rect); @b@ BufferedImage bi = reader.read(0,param); @b@ @b@ Image image = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);@b@ BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);@b@ Graphics g = tag.getGraphics();@b@ g.drawImage(image, 0, 0, null); // 绘制缩小后的图@b@ g.dispose();@b@ ImageIO.write(tag, "jpg", new File(result));// 输出到文件流@b@ } finally {@b@ try { if (in != null) in.close(); } finally { if (iis != null) iis.close(); }@b@ }@b@ }@b@@b@ /**@b@ * 给图片添加文字水印@b@ * @param pressText 水印文字@b@ * @param srcImageFile 源图像地址@b@ * @param destImageFile 目标图像地址@b@ * @param fontName 水印的字体名称@b@ * @param fontStyle 水印的字体样式@b@ * @param color 水印的字体颜色@b@ * @param fontSize 水印的字体大小@b@ * @param x 修正值@b@ * @param y 修正值@b@ * @param alpha 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字@b@ */@b@ public final static void pressText(String pressText,@b@ String srcImageFile, String destImageFile, String fontName,@b@ int fontStyle, Color color, int fontSize,int x,@b@ int y, float alpha) throws Exception {@b@ File img = new File(srcImageFile);@b@ Image src = ImageIO.read(img);@b@ int width = src.getWidth(null);@b@ int height = src.getHeight(null);@b@ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);@b@ Graphics2D g = image.createGraphics();@b@ g.drawImage(src, 0, 0, width, height, null);@b@ g.setColor(color);@b@ g.setFont(new Font(fontName, fontStyle, fontSize));@b@ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));@b@ // 在指定坐标绘制水印文字@b@ g.drawString(pressText, (width - (getLength(pressText) * fontSize))@b@ / 2 + x, (height - fontSize) / 2 + y);@b@ g.dispose();@b@ ImageIO.write((BufferedImage) image, "JPEG", new File(destImageFile));// 输出到文件流@b@ }@b@@b@ /**@b@ * 给图片添加图片水印@b@ * @param pressImg 水印图片@b@ * @param srcImageFile 源图像地址@b@ * @param destImageFile 目标图像地址@b@ * @param x 修正值。 默认在中间@b@ * @param y 修正值。 默认在中间@b@ * @param alpha 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字@b@ */@b@ public final static void pressImage(String pressImg, String srcImageFile,String destImageFile,@b@ int x, int y, float alpha) throws Exception {@b@ File img = new File(srcImageFile);@b@ Image src = ImageIO.read(img);@b@ int wideth = src.getWidth(null);@b@ int height = src.getHeight(null);@b@ BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);@b@ Graphics2D g = image.createGraphics();@b@ g.drawImage(src, 0, 0, wideth, height, null);@b@ // 水印文件@b@ Image src_biao = ImageIO.read(new File(pressImg));@b@ int wideth_biao = src_biao.getWidth(null);@b@ int height_biao = src_biao.getHeight(null);@b@ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,@b@ alpha));@b@ g.drawImage(src_biao, (wideth - wideth_biao) / 2,@b@ (height - height_biao) / 2, wideth_biao, height_biao, null);@b@ // 水印文件结束@b@ g.dispose();@b@ ImageIO.write((BufferedImage) image, "JPEG", new File(destImageFile));@b@ }@b@@b@@b@ /**@b@ * 计算text的长度(一个中文算两个字符)@b@ * @param text@b@ * @return@b@ */@b@ public final static int getLength(String text) {@b@ int length = 0;@b@ for (int i = 0; i < text.length(); i++) {@b@ if (new String(text.charAt(i) + "").getBytes().length > 1) {@b@ length += 2;@b@ } else {@b@ length += 1;@b@ }@b@ }@b@ return length / 2;@b@ }@b@}