前几天有个需求,需要根据后台返回的数据动态生成图片显示到页面上,猛然想到使用验证码图片的方式动态生成图片,然后网上找到方法给图片添加水印,包括文字水印和图片水印。
水印实现效果参考如下网址:http://www.oschina.net/code/snippet_120166_13511
package com.utility;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class StudyImageUtil {
// 宽度
private static final int WIDTH = 1000;
// 高度
private static final int HEIGHT = 950;
// 双字节字符字体大小
private static final int DOUBLE_FONT_SIZE = 15;
// 单字节字符字体大小
private static final int SINGLE_FONT_SIZE = 8;
// 行间距
private static final int LINE_HEIGHT = 30;
// 水印透明度
private static float alpha = 0.8f;
// 水印横向位置
private static int positionWidth = 150;
// 水印纵向位置
private static int positionHeight = 300;
// 水印文字字体
private static Font font = new Font("宋体", Font.BOLD, 115);
// 水印文字颜色
private static Color color = new Color(230, 230, 230);
/**
* 生成图片
* @throws IOException
*/
public static void createImage(String[] strList, int[] sizes, String basePath, String targetPath) throws IOException {
OutputStream os = null;
// 1.创建空白图片
BufferedImage image = new BufferedImage(
WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 2.获取图片画笔
Graphics graphic = image.getGraphics();
// 3.设置画笔颜色
graphic.setColor(Color.white);
// 4.绘制矩形背景
graphic.fillRect(0, 0, WIDTH, HEIGHT);
// 5.绘制矩形边框
graphic.setColor(Color.lightGray);
graphic.drawRect(0, 0, WIDTH-10, HEIGHT-10);
/**
* strList:字符串数组,里面存储的是需要显示的文字,每个元素换行显示
* sizes:长度数组,对应上面的字符串数组,对于每个字符串,记录标题长度,需要加粗显示
*/
int height = 0;//记录文字行数
int sumLength = 0;
String regex = "[^\\x00-\\xff]";//匹配双字节字符
Matcher matcher = null;
Pattern pattern = Pattern.compile(regex);
boolean rs = false;//记录当前字符的匹配情况
boolean flag = false;//记录前一字符的匹配情况
for(int j = 0; j WIDTH - DOUBLE_FONT_SIZE*2){
//每一行的前后都要留有一定空白,横坐标已经超出长度,需要折行
height = height + 1;
if(rs){ //前面是双字节字符,横坐标设置为15
sumLength = DOUBLE_FONT_SIZE;
}else{ //前面是单字节字符,横坐标设置为8
sumLength = SINGLE_FONT_SIZE;
}
}
// 画字符
graphic.drawString(schar, sumLength, height*LINE_HEIGHT);
}
}
// 6.返回图片
os = new FileOutputStream(targetPath);
ImageIO.write(image, "JPG", os);
System.out.println("返回图片。。。。。。");
}
//添加文字水印
public static void markImageByText(String logoText, String srcImgPath,
String targerPath, Integer degree) {
InputStream is = null;
OutputStream os = null;
try {
// 1、源图片
Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
// 2、得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 3、设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
// 4、设置水印旋转
if (null != degree) {
g.rotate(Math.toRadians(degree),(double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
}
// 5、设置水印文字颜色
g.setColor(color);
// 6、设置水印文字Font
g.setFont(font);
// 7、设置水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
// 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
g.drawString(logoText, positionWidth, positionHeight);
// 9、释放资源
g.dispose();
// 10、生成图片
os = new FileOutputStream(targerPath);
ImageIO.write(buffImg, "JPG", os);
System.out.println("图片完成添加水印文字");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//添加图片水印
public static void markImageByIcon(String iconPath, String srcImgPath,
String targerPath, Integer degree) {
OutputStream os = null;
try {
Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
// 得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg
.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
if (null != degree) {
// 设置水印旋转
g.rotate(Math.toRadians(degree),
(double) buffImg.getWidth() / 2, (double) buffImg
.getHeight() / 2);
}
// 水印图象的路径 水印一般为gif或者png的,这样可设置透明度
ImageIcon imgIcon = new ImageIcon(iconPath);
// 得到Image对象。
Image img = imgIcon.getImage();
float alpha = 0.1f; // 透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
// 表示水印图片的位置
g.drawImage(img, 0, 0, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
os = new FileOutputStream(targerPath);
// 生成图片
ImageIO.write(buffImg, "JPG", os);
System.out.println("图片完成添加Icon印章。。。。。。");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String[] contents = new String[4];
contents[0] = "姓名:张三";
contents[1] = "性别:男";
contents[2] = "出生年月:1988-01";
contents[3] = "个人经历:1995年9月进入XXX小学,2000年9月进入某中学,2003年顺利升入本市高中,2006年考入某一本大学成为一名大学生,2010年考入北京一所大学成为一名研究生,2013年顺利毕业进入职场。";
int[] sizes = new int[24];
sizes[0] = 6;
sizes[1] = 11;
sizes[2] = 5;
sizes[3] = 5;
try {
createImage(contents, sizes,
"E:/MyEclipseWorkSpace/CreditTraffic/WebRoot",
"e:/test/createImage.jpg");
} catch (IOException e) {
e.printStackTrace();
}
markImageByText("测试水印","e:/test/createImage.jpg","e:/test/createImage_text.jpg",90);
markImageByIcon("E:/MyEclipseWorkSpace/CreditTraffic/WebRoot/img/watermark.png",
"e:/test/createImage.jpg", "e:/test/createImage_icon.jpg", 135);
}
}
效果图如下所示:
在SpringMVC Controller中调用的方式如下,需要把两个数组封装好,传递过去
String basePath = request.getSession().getServletContext().getRealPath("/");
//利用图片工具生成图片
Object objs = ImageUtil.createImage(output, sizes, basePath);
//将图片输出给浏览器
BufferedImage image = (BufferedImage) objs;
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
ImageIO.write(image, "png", os);