网站上传图片自定义水印效果

在我们开发网站的时候,都会遇到图片上传的功能,如何保护自己网站图片的版权是一个不可忽视的问题,一般的做法是通过一个后台程序读取图片,然后判断来源地址是否本网站,如果不是,则提示本图片来源于某某网,不可显示的提示信息,还有一种做法是让图片显示,但说显示的图片有本网站的水印效果,类似代码如下:

Java代码 复制代码
  1. /** *//**  
  2.  * 打印文字水印图片  
  3.  *   
  4.  * @param pressText  
  5.  *            --文字  
  6.  * @param targetImg --  
  7.  *            目标图片  
  8.  * @param fontName --  
  9.  *            字体名  
  10.  * @param fontStyle --  
  11.  *            字体样式  
  12.  * @param color --  
  13.  *            字体颜色  
  14.  * @param fontSize --  
  15.  *            字体大小  
  16.  * @param x --  
  17.  *            偏移量  
  18.  * @param y  
  19.  */  
  20.   
  21. public static void pressText(String pressText, String targetImg,   
  22.         String fontName, int fontStyle, Color color, int fontSize, int x,   
  23.         int y) {   
  24.     try {   
  25.         File _file = new File(targetImg);   
  26.         Image src = ImageIO.read(_file);   
  27.         int wideth = src.getWidth(null);   
  28.         int height = src.getHeight(null);   
  29.         BufferedImage image = new BufferedImage(wideth, height,   
  30.                 BufferedImage.TYPE_INT_RGB);   
  31.         Graphics g = image.createGraphics();   
  32.         g.drawImage(src, 00, wideth, height, null);   
  33.         // String s="www.qhd.com.cn";   
  34.         g.setColor(color);   
  35.         g.setFont(new Font(fontName, fontStyle, fontSize));   
  36.   
  37.         g.drawString(pressText, wideth - fontSize - x, height - fontSize   
  38.                 / 2 - y);   
  39.         g.dispose();   
  40.         FileOutputStream out = new FileOutputStream(targetImg);   
  41.         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
  42.         encoder.encode(image);   
  43.         out.close();   
  44.     } catch (Exception e) {   
  45.         System.out.println(e);   
  46.     }   
  47. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值