制作水印图

制作水印图:

图片1:

图片2:

将图片2叠加到图片1上,输出

package org.example;

import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;



public class Main {
    public static enum WatermarkLocation {
        TOP_LEFT,
        TOP_RIGHT,
        BOTTOM_LEFT,
        BOTTOM_RIGHT,
        CENTER
    }

    public static void main(String[] args) {
        String inputImagePath = "C:\\Users\\Administrator\\Pictures\\images\\pic1.jpg"; // 原始图片路径
        String outputDirectory  = "C:\\Users\\Administrator\\Pictures\\images\\water"; // 输出图片路径
        String watermarkImagePath = "C:\\Users\\Administrator\\Pictures\\images\\pic2.jpg"; // 水印图片路径


        // 添加水印到不同位置
        addWatermark(inputImagePath, outputDirectory, watermarkImagePath, WatermarkLocation.TOP_LEFT);//左上
        addWatermark(inputImagePath, outputDirectory, watermarkImagePath, WatermarkLocation.TOP_RIGHT);//左下
        addWatermark(inputImagePath, outputDirectory, watermarkImagePath, WatermarkLocation.BOTTOM_LEFT);//右上
        addWatermark(inputImagePath, outputDirectory, watermarkImagePath, WatermarkLocation.BOTTOM_RIGHT);//右下
        addWatermark(inputImagePath, outputDirectory, watermarkImagePath, WatermarkLocation.CENTER);//居中
    }

    public static void addWatermark(String inputImagePath, String outputDirectory, String watermarkImagePath, WatermarkLocation location) {
        try {
            File inputFile = new File(inputImagePath);  //原图文件流
            File watermarkFile = new File(watermarkImagePath);//水印图文件流

            BufferedImage inputImage = ImageIO.read(inputFile);
            BufferedImage watermarkImage = ImageIO.read(watermarkFile);

            // 创建一个与原始图片大小相同的 BufferedImage
            BufferedImage outputImage = new BufferedImage(inputImage.getWidth(), inputImage.getHeight(), BufferedImage.TYPE_INT_RGB);

            // 获取图形上下文以在输出图像上绘制
            Graphics2D graphics = outputImage.createGraphics();

            // 将原始图片绘制到输出图像
            graphics.drawImage(inputImage, 0, 0, null);

            int watermarkX = 0;
            int watermarkY = 0;
            double widthPercentage=0.5;
            double heightPercentage=0.5;
            // 计算水印图片的宽度和高度,以原图的百分比为基础
            int watermarkWidth = (int) (inputImage.getWidth() * widthPercentage);
            int watermarkHeight = (int) (inputImage.getHeight() * heightPercentage);

            // 根据位置设置水印的坐标
            switch (location) {
                case TOP_LEFT:
                    watermarkX = 0;
                    watermarkY = 0;
                    break;
                case TOP_RIGHT:
                    watermarkX = inputImage.getWidth() - watermarkWidth - 0;
                    watermarkY = 0;
                    break;
                case BOTTOM_LEFT:
                    watermarkX = 0;
                    watermarkY = inputImage.getHeight() - watermarkHeight - 0;
                    break;
                case BOTTOM_RIGHT:
                    watermarkX = inputImage.getWidth() - watermarkWidth - 0;
                    watermarkY = inputImage.getHeight() - watermarkHeight - 0;
                    break;
                case CENTER:
                    watermarkX = (inputImage.getWidth() - watermarkWidth) / 2;
                    watermarkY = (inputImage.getHeight() - watermarkHeight) / 2;
                    break;
            }

            // 添加水印图片到输出图像
            //drawImage(Image img, int x, int y,int width, int height,ImageObserver observer)
            graphics.drawImage(watermarkImage, watermarkX, watermarkY,watermarkWidth, watermarkHeight,null);

            // 生成输出文件的名称
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String timestamp = dateFormat.format(new Date());
            String outputFileName = "watermarked_" + timestamp + ".jpg";

            // 保存输出图像到指定文件夹
            String outputFilePath = outputDirectory + File.separator + outputFileName;
            File outputFile = new File(outputFilePath);
            ImageIO.write(outputImage, "jpg", outputFile);

            // 释放资源
            graphics.dispose();
            System.out.println("水印已成功添加到图片:" + outputFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

执行代码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java-请多指教

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值