欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 图片加水印实例代码

程序员文章站 2023-12-18 18:02:46
复制代码 代码如下:try {            stri...
复制代码 代码如下:

try {
            string targetimg = "d:/blue hills.jpg";
//          string pressimg = "d:/20130311220300.jpg";
            string pressimg = "d:/html_original.jpg";

            file f1 = new file(targetimg);
            image src = imageio.read(f1);
            int width = src.getwidth(null);
            int height = src.getheight(null);

            bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb);
            graphics g =  image.creategraphics();
            g.drawimage(src, 0, 0, width, height, null);

            file f2 = new file(pressimg);
            image src_pao = imageio.read(f2);
            int width_pao = src_pao.getwidth(null);
            int height_pao = src_pao.getheight(null);
            g.drawimage(src_pao, (width-+width_pao)/2, (height-height_pao)/2,width_pao,height_pao, null);
//          g.drawimage(src_pao, 100, 100,width,height, null);

            g.dispose();
            fileoutputstream out = new fileoutputstream(targetimg);
            jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
            encoder.encode(image);
            out.close();
            system.out.println("水印添加成功!!");

        } catch (exception e) {
            // todo: handle exception
            system.out.println("水印添加失败!!");
            e.printstacktrace();
        }

上一篇:

下一篇: