1,后台生成水印图片(部门名称+警号)
// 设置响应的类型格式为图片格式
response.setContentType("image/jpeg");
// 禁止图像缓存。
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String bmmc = sysuser.getBmmc();
String jhstr = sysuser.getJh();
//根据部门名称动态生成水印在宽高
int leng = bmmc.length();
int width = 240+leng*19;
int height = 150+leng*11;
//须在根路径下创建tempimg文件夹
String path = request.getRealPath("/")+"tempimg\\"+jhstr+".png";//D:\tools\apache-tomcat-6.0.41\webapps\jcbk\
//System.err.println("路径:"+path);
//System.err.println("******************************************************");
//System.err.println("("+bmmc+")部门名称长度:"+leng);
//System.err.println("生成水印图片长为:"+width+"。宽为:"+height);
//System.err.println("******************************************************");
// 生成水印,部门名称+警号
CreateImage image = new CreateImage(width, height, bmmc+" "+jhstr,path);
CreateImage.java
package com.sunshine.monitor.comm.util.waterMark;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class CreateImage {
// 图片宽度.
private int width = 400;
// 图片高度.
private int height = 150;
// 图片内容.
private String content = "湖 南 省 公 安 厅 shengting";
//保存路径
private String path = "D:image\\876234.png";
// 生成随机数
private Random random = new Random();
// 图片字符串
private String imgStr = null;
// 图片二进制流
private BufferedImage buffImg = null;
public CreateImage(){
this.createCode();
}
public CreateImage(int width, int height){
this.width = width;
this.height = height;
this.createCode();
}
public CreateImage(int width, int height, String content,String path){
this.width = width;
this.height = height;
this.content = content;
this.path = path;
this.createCode();
}
public void createCode(){
int codeX = width;
int codeY = height;
int fontHeight = height / 3;// 字体的高度
String pathstr = path;
String s = content;
OutputStream os = null;
File file = new File(pathstr);
//ImgFontByte imgFont = new ImgFontByte();
//Font font = imgFont.getFont(200);
//Font font = imgFont.getFont(fontHeight);
Font font = new Font("宋体", Font.BOLD, 25);
// 图像buffer
buffImg = new BufferedImage(codeX, codeY, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)buffImg.getGraphics();
// 将图像填充为白色
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, width, height);
// 创建字体
//ImgFontByte imgFont = new ImgFontByte();
//Font font = imgFont.getFont(fontHeight);
g2.setFont(font);
// 设置字体颜色
g2.setColor(getRandomColor());
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(s, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
// 设置旋转
g2.rotate(Math.toRadians(30),(double) codeX / 2, (double) codeY / 2);
// 设置透明度
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,0.5f));
g2.drawString(s, (int)x, (int)baseY);
StringBuffer randomCode = new StringBuffer();
randomCode.append(s);
imgStr = randomCode.toString();
try {
os = new FileOutputStream(file);
ImageIO.write(this.buffImg, "png", os);
} catch (IOException e) {
e.printStackTrace();
}
}
// 图片转为字符串
/** 获取随机颜色 */
private Color getRandomColor() {
int r = getRandomNumber(255);
int g = getRandomNumber(255);
int b = getRandomNumber(255);
return new Color(r, g, b);
}
/** 获取随机数 */
private int getRandomNumber(int number) {
return random.nextInt(number);
}
public String getImgStr() {
return this.imgStr;
}
public void write(OutputStream sos) throws IOException {
ImageIO.write(this.buffImg, "png", sos);
sos.close();
}
public static void main(String argf[]){
CreateImage image = new CreateImage(400, 120, "省厅 licshiju","D:\\tools\\apache-tomcat-6.0.41\\webapps\\jcbk\\tempImg\\876234.png");
}
private byte[] hex2byte(String str) {
if (str == null)
return null;
str = str.trim();
int len = str.length();
if (len == 0 || len % 2 == 1)
return null;
byte[] b = new byte[len / 2];
try {
for (int i = 0; i < str.length(); i += 2) {
b[i / 2] = (byte) Integer.decode(
"0x" + str.substring(i, i + 2)).intValue();
}
return b;
} catch (Exception e) {
return null;
}
}
// 字体文件的十六进制字符串
private String getFontByteStr() {
return null;
}
}
2,jsp显示图片
A,在datagrid中追加方法设置透明 (只支持easyUI1.2.6,;不支持easyUI1.3.6,须修改透明样式)
rowStyler: function(index,row){
<span style="white-space:pre"> </span>return 'background:transparent;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#7f000000,endColorstr=#7f000000);zoom: 1;';
}
B,在datagrid 展示数据的table外层增加div
<div id="secondTabDiv" style="display: block;width:100%; z-index:999999;background: url(${path}/tempimg/${userSession.sysuser.jh}.png);background-size:100%,100%;background-position:-150px -150px;">
<table id="DatagridTab"></table>
</div>