可根据自己需求调整水印样式、、、
public class ImageUtil {
private static final int INITTOP = 100;
private static final int FONDCOLOR = 9999990;
private static final String FOND = "微软雅黑";
/**
* 创建水印图片
* @param width
* @param height
*/
public static void createImg(String realname, String policeNo,
String idCard, String ip, String sessionId) {
int width = 446; //图片宽度
int height = 350; //图片高度
createImg(width, height, realname, policeNo, idCard, ip, sessionId);
}
/**
* 绘制姓名
* @param realname
*/
private static int grapRealname(Graphics g, String realname) {
if (realname == null || "".equals(realname)) {
return 0;
}
int top = INITTOP;
int left = 80;
g.setColor(new Color(FONDCOLOR));
g.setFont(new Font(FOND, Font.PLAIN, 20));
if (realname.length() == 2) {
for (int i = 0; i < realname.length(); i++) {
String str = realname.substring(i, i + 1);
g.drawString(str, left + 20 * i, top);
}
left = left + 20 * realname.length();
} else if (realname.length() == 3) {
for (int i = 0; i < realname.length(); i++) {
String str = realname.substring(i, i + 1);
g.drawString(str, left + 20 * i, top);
}
left = left + 20 * realname.length();
} else if (realname.length() == 4) {
g.setFont(new Font(FOND, Font.PLAIN, 20));
for (int i = 0; i < realname.length(); i++) {
String str = realname.substring(i, i + 1);
g.drawString(str, left + 20 * i, top);
}
left = left + 20 * realname.length();
}
return left;
}
/**
* 绘制编号
* @param g
* @param policeNo
*/
private static void grapPoliceNo(Graphics g, String policeNo, int toLeft) {
if (policeNo == null || "".equals(policeNo)) {
return;
}
int top = INITTOP;
int left = 10;
g.setColor(new Color(FONDCOLOR));
g.setFont(new Font(FOND, Font.PLAIN, 20));
// top=top+60;
left += toLeft;
for (int i = 0; i < policeNo.length(); i++) {
String str = policeNo.substring(i, i + 1);
g.drawString(str, left + 20 * i, top);
}
}
/**
* 绘制身份证
* @param g
* @param policeNo
*/
private static void grapIdCard(Graphics g, String idCard) {
int top = INITTOP;
int left = 15;
g.setColor(new Color(FONDCOLOR));
g.setFont(new Font(FOND, Font.PLAIN, 20));
top = top + 40;
for (int i = 0; i < idCard.length(); i++) {
String str = idCard.substring(i, i + 1);
g.drawString(str, left + 20 * i, top);
}
}
/**
* 绘制IP
* @param g
* @param policeNo
*/
private static void grapIp(Graphics g, String ip) {
int top = INITTOP;
int left = 25;
g.setColor(new Color(FONDCOLOR));
g.setFont(new Font(FOND, Font.PLAIN, 20));
top = top + 80;
for (int i = 0; i < ip.length(); i++) {
String str = ip.substring(i, i + 1);
g.drawString(str, left + 20 * i, top);
}
}
/**
* 绘制日期
* @param g
* @param policeNo
*/
private static void grapDate(Graphics g, String newDate) {
int top = INITTOP;
int left = 25;
g.setColor(new Color(FONDCOLOR));
g.setFont(new Font(FOND, Font.PLAIN, 20));
top = top + 120;
for (int i = 0; i < newDate.length(); i++) {
String str = newDate.substring(i, i + 1);
g.drawString(str, left + 20 * i, top);
}
}
/**
* 创建水印图片
* @param width
* @param height
*/
public static void createImg(int width, int height,String realname,String policeNo,String idCard) {
width=340;
height=300;
//首先判断水印图片是否存在
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String newDate=sdf.format(new Date());
//System.out.println("newDate:"+newDate);
String newDateStr=newDate.replaceAll("-", "");
//System.out.println("newDateStr:"+newDateStr);
File file=new File("项目目录"+"/image/shuiyin/"+idCard+"_"+policeNo+"_"+newDateStr+".png");
if(file.exists()){
return;
}
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 设定背景色
// g.setColor(new Color(0xffffff));
// g.fillRect(0, 0, width, height);
// 画边框
// g.setColor(Color.black);
// g.drawRect(0, 0, width - 1, height - 1);
// //写入姓名
int toLeft=grapRealname(g, realname);
//写入警号
grapPoliceNo(g, policeNo,toLeft);
//写入日期
grapDate(g, newDate);
// 释放图形上下文
g.dispose();
//旋转图片
BufferedImage img = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
Graphics g2 = img.getGraphics();
// 设定背景色
// g2.setColor(new Color(0xffffff));
// g2.fillRect(0, 0, width, height);
Graphics2D graphics2d;
(graphics2d = img
.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(-45), width / 2, height / 2); //逆时针旋转45度
graphics2d.drawImage(image, 0, 0, null);
graphics2d.dispose();
try {
// 输出图像到页面
ImageIO.write(img, "png",file);
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println("ok");
}
/**
* 创建水印图片
*
* @param width
* @param height
*/
public static void createImg(int width, int height, String realname,
String policeNo, String idCard, String ip, String sessionId) {
System.out.println(sessionId);
width = 440;
height = 350;
// 首先判断水印图片是否存在
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String newDate = sdf.format(new Date());
File file = new File(Sungoal.getInstance().getSungoalHome()+"/images/shuiyin/"+sessionId + ".jpeg");
if (file.exists()) {
return;
}
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 设定背景色
g.setColor(new Color(0xffffff));
g.fillRect(0, 0, width, height);
// //写入姓名
int toLeft = grapRealname(g, realname);
// 写入编号
grapPoliceNo(g, policeNo, toLeft);
// 写入身份证号
grapIdCard(g, idCard);
// 写入IP
grapIp(g, ip);
// 写入日期
grapDate(g, newDate);
// 释放图形上下文
g.dispose();
// 旋转图片
BufferedImage img = new BufferedImage(width, height, image
.getColorModel().getTransparency());
Graphics g2 = img.getGraphics();
// 设定背景色
g2.setColor(new Color(0xffffff));
g2.fillRect(0, 0, width, height);
Graphics2D graphics2d;
(graphics2d = img.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(-45), width / 2, height / 2); // 逆时针旋转45度
graphics2d.drawImage(image, 0, 0, null);
graphics2d.dispose();
try {
// 输出图像到页面
ImageIO.write(img, "JPEG", file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
如图水印效果…