图片加水印工具类


package com.infinitePossibilities.utils.file;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * 图片水印工具类
 * @version 1.0
 */
public class ImageRemarkUtil {

    // 水印透明度
    private static float alpha = 0.2f;
    // 水印横向位置
    private static int positionWidth = 0;
    // 水印纵向位置
    private static int positionHeight = 0;
    // 水印文字字体
    private static Font font = new Font("宋体", Font.BOLD, 30);
    // 水印文字颜色
    private static Color color = Color.gray;

    /**
     * 给图片添加水印文字、可设置水印文字的旋转角度
     * @param bytes  图片字节
     * @param logoText 水印文字
     * @param degree 倾斜角度
     */
    public static byte[] markImageTextByBytes(byte[] bytes, String logoText, Integer degree) {
        System.out.println("<----添加水印开始---->");
        ByteArrayOutputStream os = null;
        byte[] imageInByte = null;
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            Image srcImg = ImageIO.read(in);

            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 3、设置对线段的锯齿状边缘处理
            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);
            // 4、设置水印旋转
            if (null != degree) {
                g.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth(),
                        (double) buffImg.getHeight());
            }
            // 5、设置水印文字颜色
            g.setColor(color);
            // 6、设置水印文字Font
            g.setFont(font);
            // 7、设置水印文字透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));

            // 8、水印内容
            StringBuffer wechat = new StringBuffer(logoText);
            for(int n=0; n<=5; n++) {
                wechat.append("    " + logoText);
            }
            System.out.println("水印内容----->:"+wechat.toString());

            // 9、drawString 第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
            int num = 0;
            // 平铺水印(需要设置对称值)
            for(int i=1; i<5; i++) {
                g.drawString(wechat.toString(), positionWidth + num, positionHeight + num);
                if(num != 0){
                    g.drawString(wechat.toString(), positionWidth - num, positionHeight - num);
                }
                num += 100;
            }

            // 10、释放资源,转成字节流
            g.dispose();
            os = new ByteArrayOutputStream();
            ImageIO.write(buffImg, "JPG", os);
            os.flush();
            imageInByte = os.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("<----添加水印结束---->");
        return imageInByte;
    }

    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }

    public static void main(String[] args) {

        File file = new File("C:\\Users\\admin\\Desktop\\baidu.jpg");
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[10240];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            byte[] inBytes = bos.toByteArray();
            byte[] outBytes = markImageTextByBytes(inBytes, "北京冬奥会加油!", -45);
            //文件输出路径
            String fileName = "sy-"+file.getName();
            FileOutputStream out = new FileOutputStream("C:\\Users\\admin\\Desktop\\"+fileName);
            out.write(outBytes);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

效果: