图片工具类(不断更新中)

package com.test;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

/**
 * 图片工具类
 * @author paul
 *
 */
public class ImageUtils {

	public static void main(String[] args) {
		ImageUtils iu = new ImageUtils();
		iu.waterMark("E:\\i.png", "E:\\i_1.png", "T");
		
		List<String> list = new ArrayList<String>();
		list.add("E:\\i.png");
		list.add("E:\\i_1.png");
		iu.append(list, "E:\\i_2.png", true);
	}
	
	/**
	 * 图片添加文字水印(保持背景透明)
	 * @param srcImgPath
	 * @param outImgPath
	 * @param text
	 */
	public void waterMark(String srcImgPath, String outImgPath, String text) {
        try {  
            BufferedImage image = ImageIO.read(new File(srcImgPath));  
            int width = image.getWidth();  
            int height = image.getHeight(); 
            Graphics2D g2d = image.createGraphics();  
            Font font = new Font("宋体", Font.PLAIN, 50);
            
            //设置字体居中
            FontRenderContext context = g2d.getFontRenderContext();
        	Rectangle2D bounds = font.getStringBounds(text, context);
        	double x = (width - bounds.getWidth()) / 2;
        	double y = (height - bounds.getHeight()) / 2;
        	double ascent = -bounds.getY();
        	double baseY = y + ascent;
        	
        	g2d.setFont(font);
        	g2d.drawString(text, (int) x, (int) baseY);
            g2d.dispose(); 
            
            File file = new File(outImgPath);  
            ImageIO.write(image, "png", file);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
	
	/**
	 * 图片拼接(保持背景透明)
	 * @param inputFileNameList
	 * @param outputFileName
	 * @param isX 是否横向拼接
	 */
	public void append(List<String> inputFileNameList, String outputFileName, boolean isX) {  
        try {  
            boolean isFirstPng = true;  
            BufferedImage outputImg = null;  
            int outputImgW = 0;  
            int outputImgH = 0;  
            for (String pngFileName : inputFileNameList) {  
                if (isFirstPng) {  
                    isFirstPng = false;  
                    outputImg = ImageIO.read(new File(pngFileName));  
                    outputImgW = outputImg.getWidth();  
                    outputImgH = outputImg.getHeight();  
                } else {  
                    BufferedImage appendImg = ImageIO.read(new File(pngFileName));  
                    int appendImgW = appendImg.getWidth();  
                    int appendImgH = appendImg.getHeight();  
  
                    if (isX) {  
                        outputImgW = outputImgW + appendImgW;  
                        outputImgH = outputImgH > appendImgH ? outputImgH : appendImgH;  
                    } else {  
                        outputImgW = outputImgW > appendImgW ? outputImgW : appendImgW;  
                        outputImgH = outputImgH + appendImgH;  
                    }  
  
                    // create basic image  
                    Graphics2D g2d = outputImg.createGraphics();  
                    BufferedImage imageNew = g2d.getDeviceConfiguration().createCompatibleImage(outputImgW, outputImgH, Transparency.TRANSLUCENT);  
                    g2d.dispose();  
                    g2d = imageNew.createGraphics();  
                      
                    int oldImgW = outputImg.getWidth();  
                    int oldImgH = outputImg.getHeight();  
                    g2d.drawImage(outputImg, 0, 0, oldImgW, oldImgH, null);  
                    if (isX) {  
                        g2d.drawImage(appendImg, oldImgW, 0, appendImgW, appendImgH, null);  
                    } else {  
                        g2d.drawImage(appendImg, 0, oldImgH, appendImgW, appendImgH, null);  
                    }  
                      
                    g2d.dispose();  
                    outputImg = imageNew;  
                }  
            }  
            File file = new File(outputFileName);  
            ImageIO.write(outputImg, "png", file);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    } 
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值