Java生成带水印的图片

Java生成带水印的图片

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

/**
 * @Date: 13:54 2019-01-05
 * @Description: 生成带水印图片
 */
public class ImageWaterMarkUtil {

    /**
     * 水印的x轴偏移量、y轴偏移量
     **/
    private static final int xMove = 120;
    private static final int yMove = 150;

    /**
     * @Description newImage
     * @Explain 新建图片(指定图片尺寸)
     **/
    public static BufferedImage newImage(int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        g2.setBackground(Color.WHITE);
        g2.clearRect(0, 0, width, height);
        return image;
    }

    /**
     * @Description addWaterMark
     * @Explain 生成带水印图片
     **/
    public static void addWaterMark(int widths, int heights, String tarImgPath, String waterMarkContent, String fileExt) {
        /**
         * 水印的字体、大小、字体颜色、水印透明度、水印旋转角度
         **/
        Font font = new Font("宋体", Font.PLAIN, 18);
        Color markContentColor = Color.gray;
        Integer degree = -20;
        float alpha = 0.3f;
        OutputStream outImgStream = null;
        try {
            /**
             * 源图片
             **/
            BufferedImage srcImg = newImage(widths, heights);
            int srcImgWidth = srcImg.getWidth(null);
            int srcImgHeight = srcImg.getHeight(null);
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            /**
             * 得到画笔并设置好字体
             **/
            Graphics2D g = bufImg.createGraphics();
            g.setBackground(Color.gray);
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            g.setColor(markContentColor);
            g.setFont(font);
            /**
             * 对线段的锯齿边缘进行处理
             **/
            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);
            /**
             * 抗锯齿处理
             **/
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            /**
             * 设置水印文字透明度
             **/
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            /**
             * 设置水印旋转角度
             **/
            if (null != degree) {
                g.rotate(Math.toRadians(degree), (double) widths / 2.0, (double) heights / 2.0);
            }
            JLabel label = new JLabel(waterMarkContent);
            FontMetrics metrics = label.getFontMetrics(font);
            /**
             * 文字水印的宽度
             **/
            int fontWidth = metrics.stringWidth(label.getText());
            int fontHeight = font.getSize();
            int x = -widths / 2;
            int y = -heights / 2;
            while (x < widths * 1.5) {
                y = -heights / 2;
                while (y < heights * 1.5) {
                    g.drawString(waterMarkContent, x, y);
                    y += fontHeight + yMove;
                }
                x += fontWidth + xMove;
            }
            g.dispose();
            outImgStream = new FileOutputStream(tarImgPath);
            ImageIO.write(bufImg, fileExt, outImgStream);
        } catch (Exception e) {
            e.printStackTrace();
            e.getMessage();
        } finally {
            try {
                if (outImgStream != null) {
                    outImgStream.flush();
                    outImgStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                e.getMessage();
            }
        }
    }
}

展开阅读全文

没有更多推荐了,返回首页