All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.soento.core.util.ImageUtil Maven / Gradle / Ivy

package com.soento.core.util;

import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public final class ImageUtil {

    /**
     * 添加图片水印
     *
     * @param targetPath    目标图片路径
     * @param watermarkPath 水印图片路径
     * @param x             水印图片距离目标图片左侧的偏移量,如果x<0, 则在正中间
     * @param y             水印图片距离目标图片上侧的偏移量,如果y>0, 则在正中间
     * @param alpha         透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)
     */
    public final static void photoWatermark(String targetPath, String watermarkPath, int x, int y, float alpha) {
        try {
            File file = new File(targetPath);
            Image image = ImageIO.read(file);
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);

            // 水印文件
            Image waterImage = ImageIO.read(new File(watermarkPath));
            int widthWater = waterImage.getWidth(null);
            int heightWater = waterImage.getHeight(null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

            int widthDiff = width - widthWater;
            int heightDiff = height - heightWater;
            if (x < 0) {
                x = widthDiff / 2;
            } else if (x > widthDiff) {
                x = widthDiff;
            }
            if (y < 0) {
                y = heightDiff / 2;
            } else if (y > heightDiff) {
                y = heightDiff;
            }
            // 水印文件结束
            g.drawImage(waterImage, x, y, widthWater, heightWater, null);
            g.dispose();
            String extension = file.getName().substring(file.getName().lastIndexOf(".") + 1).toLowerCase();
            if (StringUtil.isBlank(extension)) {
                extension = "jpg";
            }
            ImageIO.write(bufferedImage, extension, file);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 添加文字
     *
     * @param targetPath 目标图片路径
     * @param word       文字
     * @param x          文字距离目标图片左侧的偏移量,如果x<0, 则在正中间
     * @param y          文字距离目标图片上侧的偏移量,如果y>0, 则在正中间
     * @param alpha      透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)
     */
    public static void photoWords(String targetPath, String word, Font font, Color color, int x, int y, float alpha) {
        try {
            File file = new File(targetPath);
            Image image = ImageIO.read(file);
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);

            AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
            g.setComposite(ac);

            //设置文字字体名称、样式、大小
            g.setFont(font);
            //设置字体颜色
            g.setColor(color);
            //输入水印文字及其起始x、y坐标
            g.drawString(word, x, y);
            g.dispose();
            String extension = file.getName().substring(file.getName().lastIndexOf(".") + 1).toLowerCase();
            if (StringUtil.isBlank(extension)) {
                extension = "jpg";
            }
            ImageIO.write(bufferedImage, extension, file);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy