读书人

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

发布时间: 2012-11-08 08:48:11 作者: rapoo

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

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

    /** *//**     * 打印文字水印图片     *      * @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);        }    }
? 1 楼 zhouzijing 2008-11-03
代码恐怕只能适用于jpg之类普通图片格式,像特殊的gif图片就有大问题了,应该注明适用范围,否则很容易误导别人。
2 楼 kangsg219 2008-11-03 png格式测试通过,
gif的确不行! 3 楼 phoenix007 2008-11-04 试了试,提示java.io.FileNotFoundException,图片文件拒绝访问! 4 楼 phoenix007 2008-11-04 phoenix007 写道
试了试,提示java.io.FileNotFoundException,图片文件拒绝访问!

跟图片文件的属性有关, 把“只读”属性去掉就可以了。

读书人网 >软件架构设计

热点推荐