博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java合成图片
阅读量:4069 次
发布时间:2019-05-25

本文共 5261 字,大约阅读时间需要 17 分钟。

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);
    }

}

原图:

背景图:

合成后:

你可能感兴趣的文章
拉格朗日插值计算器
查看>>
java中的synchronized关键字
查看>>
svn在windows下创建服务
查看>>
hibernate的二级缓存的积累
查看>>
include servlet的问题解决
查看>>
solaris下安装自动安装工工具以及环境变量设置
查看>>
solaris下安装自动安装工工具以及环境变量设置
查看>>
solaris下vi的使用 转的。。和linux下的不太一样
查看>>
solaris10下vnc的安装
查看>>
sqlserver 2005导出数据为sql的办法
查看>>
Solaris Sparcv9下jdk64位的安装
查看>>
容易忽视但是功能灰常强大的Java API
查看>>
转来的一篇关于hibernate的查询资料,很不错
查看>>
httpclient3.1下的请求头和params
查看>>
java中的image 的一些概念
查看>>
easyui中不显示出来的一个问题
查看>>
httpclient自动执行http的302重定向
查看>>
真正最正确的用BAT运行JAVA不显示DOS窗口(连闪一下都不闪)
查看>>
把log4j的日志信息打印到swing组件上
查看>>
把log4j的日志信息打印到swing组件上
查看>>