在我们开发网站的时候,都会遇到图片上传的功能,如何保护自己网站图片的版权是一个不可忽视的问题,一般的做法是通过一个后台程序读取图片,然后判断来源地址是否本网站,如果不是,则提示本图片来源于某某网,不可显示的提示信息,还有一种做法是让图片显示,但说显示的图片有本网站的水印效果,类似代码如下:
- /** *//**
- * 打印文字水印图片
- *
- * @param pressText
- * --文字
- * @param targetImg --
- * 目标图片
- * @param fontName --
- * 字体名
- * @param fontStyle --
- * 字体样式
- * @param color --
- * 字体颜色
- * @param fontSize --
- * 字体大小
- * @param x --
- * 偏移量
- * @param y
- */
- public static void pressText(String pressText, String targetImg,
- String fontName, int fontStyle, Color color, int fontSize, int x,
- int y) {
- try {
- File _file = new File(targetImg);
- Image src = ImageIO.read(_file);
- int wideth = src.getWidth(null);
- int height = src.getHeight(null);
- BufferedImage image = new BufferedImage(wideth, height,
- BufferedImage.TYPE_INT_RGB);
- Graphics g = image.createGraphics();
- g.drawImage(src, 0, 0, wideth, height, null);
- // String s="www.qhd.com.cn";
- g.setColor(color);
- g.setFont(new Font(fontName, fontStyle, fontSize));
- g.drawString(pressText, wideth - fontSize - x, height - fontSize
- / 2 - y);
- g.dispose();
- FileOutputStream out = new FileOutputStream(targetImg);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(image);
- out.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- }