java合成图片

本文介绍了一种在Java中实现图片加水印的方法,并提供了压缩图片以匹配背景尺寸的功能。通过使用`BufferedImage`和`Graphics2D` API,实现了水印的透明度调整、位置设置以及最终图片的输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

public class NewImageUtils {
    /**
     *
     * @Title: 构造图片
     * @Description: 生成水印并返回java.awt.image.BufferedImage
     * @param file
     *            源文件(图片)
     * @param waterFile
     *            水印文件(图片)
     * @param x
     *            距离右下角的X偏移量
     * @param y
     *            距离右下角的Y偏移量
     * @param alpha
     *            透明度, 选择值从.~.: 完全透明~完全不透明
     * @return BufferedImage
     * @throws IOException
     */
    public static BufferedImage watermark(File file, File waterFile, int x,
            int y, float alpha) throws IOException {
        // 获取底图
        BufferedImage buffImg = ImageIO.read(file);
        // 获取层图
        BufferedImage waterImg = ImageIO.read(waterFile);
        // 创建GraphicsD对象,用在底图对象上绘图
        Graphics2D gd = buffImg.createGraphics();
        int waterImgWidth = waterImg.getWidth();// 获取层图的宽度
        int waterImgHeight = waterImg.getHeight();// 获取层图的高度
        // 在图形和图像中实现混合和透明效果
        gd.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                alpha));
        // 绘制
        gd.drawImage(waterImg, x, y, waterImgWidth, waterImgHeight, null);
        gd.dispose();// 释放图形上下文使用的系统资源
        return buffImg;
    }

    /**
     * 输出水印图片
     *
     * @param buffImg
     *            图像加水印之后的BufferedImage对象
     * @param savePath
     *            图像加水印之后的保存路径
     */
    private void generateWaterFile(BufferedImage buffImg, String savePath) {
        int temp = savePath.lastIndexOf(".") + 1;
        try {
            ImageIO.write(buffImg, savePath.substring(temp), new File(savePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 按背景图片尺寸压缩原图图片
     * @param bgFilePath
     * @param oldFilePath
     * @param saveFilePath
     * @return
     */
    public static String getcompressImage(String bgFilePath,String oldFilePath,String saveFilePath){
        try {
            String newFilePath = oldFilePath;
            File bgFile= new File(bgFilePath);
    
            BufferedImage waterImage;
            waterImage = ImageIO.read(bgFile);
            int bgWidth = waterImage.getWidth();// 图片宽度
            int bgHeight = waterImage.getHeight();// 图片高度
            
            //压缩
            compressImage(oldFilePath, newFilePath, bgWidth, bgHeight);
            
            File newFile= new File(newFilePath);
            NewImageUtils newImageUtils = new NewImageUtils();
            // 构建叠加层
            BufferedImage buffImg = NewImageUtils.watermark(bgFile,newFile,0, 0, 1.0f); // 输出水印图片
            newImageUtils.generateWaterFile(buffImg, saveFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return saveFilePath;
    }
    
    /**
     * 根据水印图合成图片
     * @param waterFilePath
     * @param sourceFilePath
     */
    public static void gethechengImage(String waterFilePath,String sourceFilePath){
        try {
            String saveFilePath = sourceFilePath;
            
            File waterFile= new File(waterFilePath);
            File sourceFile= new File(sourceFilePath);
            
            NewImageUtils newImageUtils = new NewImageUtils();
            // 构建叠加层
            BufferedImage buffImg = NewImageUtils.watermark(sourceFile,waterFile,0, 0, 1.0f); // 输出水印图片
            newImageUtils.generateWaterFile(buffImg, saveFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * * 将图片按照指定的图片尺寸压缩 * * @param srcImgPath :源图片路径 * @param outImgPath *
     * :输出的压缩图片的路径 * @param new_w * :压缩后的图片宽 * @param new_h * :压缩后的图片高
     */
    public static void compressImage(String srcImgPath, String outImgPath,
            int new_w, int new_h) {
        BufferedImage src = InputImage(srcImgPath);
        disposeImage(src, outImgPath, new_w, new_h);
    }

    /**
     * /** * 图片文件读取 * * @param srcImgPath * @return
     */
    private static BufferedImage InputImage(String srcImgPath) {
        BufferedImage srcImage = null;
        try {
            FileInputStream in = new FileInputStream(srcImgPath);
            srcImage = javax.imageio.ImageIO.read(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return srcImage;
    }
    
     /** * 处理图片 * * @param src * @param outImgPath * @param new_w * @param new_h */
    private synchronized static void disposeImage(BufferedImage src,
            String outImgPath, int new_w, int new_h) {
        // 得到图片
        int old_w = src.getWidth();
        // 得到源图宽
        int old_h = src.getHeight();
        // 得到源图长
        BufferedImage newImg = null;
        // 判断输入图片的类型
        switch (src.getType()) {
        case 13:
            // png,gifnewImg = new BufferedImage(new_w, new_h,
            // BufferedImage.TYPE_4BYTE_ABGR);
            break;
        default:
            newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
            break;
        }
        Graphics2D g = newImg.createGraphics();
        // 从原图上取颜色绘制新图
        g.drawImage(src, 0, 0, old_w, old_h, null);
        g.dispose();
        // 根据图片尺寸压缩比得到新图的尺寸
        newImg.getGraphics().drawImage(
                src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0,
                null);
        // 调用方法输出图片文件
        OutImage(outImgPath, newImg);
    }
 
    /**
     * * 将图片文件输出到指定的路径,并可设定压缩质量 * * @param outImgPath * @param newImg * @param
     * per
     */
    private static void OutImage(String outImgPath, BufferedImage newImg) {
        // 判断输出的文件夹路径是否存在,不存在则创建
        File file = new File(outImgPath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }// 输出到文件流
        try {
            ImageIO.write(newImg, outImgPath.substring(outImgPath
                    .lastIndexOf(".") + 1), new File(outImgPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }  
    
    /**
     *
     * @param args
     * @throws IOException
     *             IO异常直接抛出了
     * @author bls
     */
    public static void main(String[] args){//可能背景图也需要压缩
        String oldFilePath = "E:/11.jpg";//原始图
        String bgFilePath = "E:/box-bg-4.png";//透明背景图
        String waterFilePath = "E:/box-4.png";//水印图
        String saveFilePath = "E:/22.png";
        String newFilePath = NewImageUtils.getcompressImage(bgFilePath,oldFilePath,saveFilePath);
        NewImageUtils.gethechengImage(waterFilePath,newFilePath);
    }

}


原图:

背景图:


合成后:



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值