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

java.imageIo给图片添加水印的实现代码

程序员文章站 2023-12-15 17:38:34
复制代码 代码如下:package com.blogs.image;import java.awt.alphacomposite;import java.awt.color...

复制代码 代码如下:

package com.blogs.image;
import java.awt.alphacomposite;
import java.awt.color;
import java.awt.font;
import java.awt.graphics2d;
import java.awt.image;
import java.awt.renderinghints;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.fileoutputstream;
import java.io.inputstream;
import java.io.outputstream;
import javax.imageio.imageio;
import javax.swing.imageicon;
/**
 * 图片水印
 */
public class imageutil {
    /**
     * @param args
     */
    public static void main(string[] args) {
        string srcimgpath = "e:/2.png";
        string iconpath = "e:\\logo.jpg";
        string targerpath = "e:/3.jpg";
        // 给图片添加水印
        imageutil.watermarkimagebyicon(iconpath, srcimgpath, targerpath, 0, 0,
, 0.1f);
        // 给图片添加水印,水印旋转-45
        // imagemarklogobyicon.markimagebyicon(iconpath, srcimgpath,
        // targerpath2, -45);
    }
    /**
     * 给图片添加水印、可设置水印图片旋转角度
     *
     * @param iconpath
     *            水印图片路径
     * @param srcimgpath
     *            源图片路径
     * @param targerpath
     *            目标图片路径
     * @param degree
     *            水印图片旋转角度
     * @param width
     *            宽度(与左相比)
     * @param height
     *            高度(与顶相比)
     * @param clarity
     *            透明度(小于1的数)越接近0越透明
     */
    public static void watermarkimagebyicon(string iconpath, string srcimgpath,
            string targerpath, integer degree, integer width, integer height,
            float clarity) {
        outputstream os = null;
        try {
            image srcimg = imageio.read(new file(srcimgpath));
            system.out.println("width:" + srcimg.getwidth(null));
            system.out.println("height:" + srcimg.getheight(null));
            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);
            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);
            }
            // 水印图象的路径 水印一般为gif或者png的,这样可设置透明度
            imageicon imgicon = new imageicon(iconpath);
            // 得到image对象。
            image img = imgicon.getimage();
            float alpha = clarity; // 透明度
            g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop,
                    alpha));
            // 表示水印图片的位置
            g.drawimage(img, width, height, null);
            g.setcomposite(alphacomposite.getinstance(alphacomposite.src_over));
            g.dispose();
            os = new fileoutputstream(targerpath);
            // 生成图片
            imageio.write(buffimg, "jpg", os);
            system.out.println("添加水印图片完成!");
        } catch (exception e) {
            e.printstacktrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (exception e) {
                e.printstacktrace();
            }
        }
    }
    /**
     * 给图片添加水印、可设置水印图片旋转角度
     *
     * @param logotext
     *            水印文字
     * @param srcimgpath
     *            源图片路径
     * @param targerpath
     *            目标图片路径
     * @param degree
     *            水印图片旋转角度
     * @param width
     *            宽度(与左相比)
     * @param height
     *            高度(与顶相比)
     * @param clarity
     *            透明度(小于1的数)越接近0越透明
     */
    public static void watermarkbytext(string logotext, string srcimgpath,
            string targerpath, integer degree, integer width, integer height,
            float clarity) {
        // 主图片的路径
        inputstream is = null;
        outputstream os = null;
        try {
            image srcimg = imageio.read(new file(srcimgpath));
            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);
            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
            g.setfont(new font("宋体", font.bold, 30));
            float alpha = clarity;
            g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop,
                    alpha));
            // 第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y) .
            g.drawstring(logotext, width, height);
            g.dispose();
            os = new fileoutputstream(targerpath);
            // 生成图片
            imageio.write(buffimg, "jpg", os);
            system.out.println("添加水印文字完成!");
        } catch (exception e) {
            e.printstacktrace();
        } finally {
            try {
                if (null != is)
                    is.close();
            } catch (exception e) {
                e.printstacktrace();
            }
            try {
                if (null != os)
                    os.close();
            } catch (exception e) {
                e.printstacktrace();
            }
        }
    }
}

还有一个图片缩放代码:
复制代码 代码如下:

/**
     * 图片缩放(图片等比例缩放为指定大小,空白部分以白色填充)
     *
     * @param srcpath
     *            源图片路径
     * @param destpath
     *            缩放后图片路径
     */
    public static void zoomimage(string srcpath, string destpath, int destheight, int destwidth) {
        try {
            bufferedimage srcbufferedimage = imageio.read(new file(srcpath));
            int imgwidth = destwidth;
            int imgheight = destheight;
            int srcwidth = srcbufferedimage.getwidth();
            int srcheight = srcbufferedimage.getheight();
            if (srcheight >= srcwidth) {
                imgwidth = (int) math.round(((destheight * 1.0 / srcheight) * srcwidth));
            } else {
                imgheight = (int) math.round(((destwidth * 1.0 / srcwidth) * srcheight));
            }
            bufferedimage destbufferedimage = new bufferedimage(destwidth, destheight, bufferedimage.type_int_rgb);
            graphics2d graphics2d = destbufferedimage.creategraphics();
            graphics2d.setbackground(color.white);
            graphics2d.clearrect(0, 0, destwidth, destheight);
            graphics2d.drawimage(srcbufferedimage.getscaledinstance(imgwidth, imgheight, image.scale_smooth), (destwidth / 2) - (imgwidth / 2), (destheight / 2) - (imgheight / 2), null);
            graphics2d.dispose();
            imageio.write(destbufferedimage, "jpeg", new file(destpath));
        } catch (ioexception e) {
            e.printstacktrace();
        }
    }

上一篇:

下一篇: