/**
* 给图片添加水印、可设置水印旋转角度
* @param logoText 水印
* @param srcImgPath 源图片路径
* @param targerPath 目标图片路径
* @param position 水印位置
* @param degree 水印图片旋转角度
* @param r_margin 向右移动的多少
* @param b_margin 向下移动多少
* @param pellucidity 水印透明度
* @param W_size 字体的大小
*/
public static String markByText(String logoText,Image srcImg,int position,
Integer degree,float pellucidity,int r_margin,int b_margin,int size,String fileName) {
// 主图片的路径
InputStream is = null;
OutputStream os = null;
if(srcImg==null){
log.info(srcImg+"----源图链接错误");
return "{\"result\":1,\"detail\":\"源图链接错误\"}";
}
if(logoText==null){
log.info("水印文字不能为空");
return "{\"result\":1,\"detail\":\"水印文字不能为空\"}";
}
String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
String targerPath="/opt/cloudstorage/"+fileName;
if(position<1||position>9){
log.info(position+"----位置设置错误");
return "{\"result\":1,\"detail\":\"位置设置错误\"}";
}
if(pellucidity<0||pellucidity>1){
log.info(pellucidity+"----透明度设置错误");
return "{\"result\":1,\"detail\":\"透明度设置错误\"}";
}
try {
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
// 得到画笔对象
// Graphics g= buffImg.getGraphics();
Graphics2D g = buffImg.createGraphics();
// 设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//原图的宽度
int srcWidth=srcImg.getWidth(null);
//原图的高度
int srcHeight=srcImg.getHeight(null);
// System.out.println("srcWidth:"+srcWidth+"---"+"srcHeight:"+srcHeight);
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);
}
// 设置颜色
g.setColor(Color.red);
// 设置 Font
Font font=new Font("宋体", Font.BOLD, size);
JLabel label = new JLabel(logoText);
FontMetrics metrics = label.getFontMetrics(font);
int textH = metrics.getHeight(); //字符串的高, 只和字体有关
int textW = metrics.stringWidth(label.getText());
// System.out.println("textW:"+textW+"-----"+"textH:"+textH);
g.setFont(font);
//透明度
float alpha = pellucidity;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
//判断位置
int x;
int y;
switch (position) {
case 1:
x=50;
y=textH+30;
break;
case 2:
x=(srcWidth-textW)/2;
y=textH+30;
break;
case 3:
x=srcWidth-textW-50;
y=textH+30;
break;
case 4:
x=50;
y=(srcHeight+textH)/2;
break;
case 5:
x=(srcWidth-textW)/2;
y=(srcHeight+textH)/2;
break;
case 6:
x=srcWidth-textW-50;
y=(srcHeight+textH)/2;
break;
case 7:
x=50;
y=srcHeight-10;
break;
case 8:
x=(srcWidth-textW)/2;
y=srcHeight-30;
break;
default:
x=srcWidth-textW-50;
y=srcHeight-30;
break;
}
//添加用户调整的坐标,确定最后的坐标
x=x+r_margin;
y=y+b_margin;
//判断这个坐标是否正确
if(x<0||x>srcWidth-textW||y<0||y>srcHeight){
log.info("x为:"+x+"--y为:"+y+"-----超出边界了");
return "{\"result\":1,\"detail\":\"超出边界了,请检查参数是否正确\"}";
}
// 第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y) .
g.drawString(logoText, x, y);
g.dispose();
os = new FileOutputStream(targerPath);
// 生成图片
ImageIO.write(buffImg,prefix, os);
buffImg=null;
srcImg=null;
System.gc();
} catch (Exception e) {
e.printStackTrace();
srcImg=null;
System.gc();
log.info("文字---添加水印失败");
return "{\"result\":1,\"detail\":\"添加水印失败\"}";
} finally {
try {
if (null != is)
is.close();
if(null!=os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
srcImg=null;
System.gc();
return "success";
}