import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.apache.log4j.Logger; /** * 图片合成工具类 * */ public class PictureSynthesisUtil { private static Logger logger = Logger.getLogger(PictureSynthesisUtil.class); /** * 签名合成,图片坐标系左上角为(0,0) * * @param imgPath 图片路径 * @param signaturePath 签名图片路径 * @param outFilePath 合成后图片路径 * @param xPlace 签名图片放置的x坐标 * @param yPlace 签名图片放置的y坐标 * @return 是否合成成功 * @throws IOException */ public static Boolean signatureSynthesis(String imgPath, String signaturePath, String outFilePath, int xPlace, int yPlace) throws Exception { logger.info("进入签名合成方法"); BufferedImage backImg = ImageIO.read(new File(imgPath)); BufferedImage signatureImg = ImageIO.read(new File(signaturePath)); File outFile = new File(outFilePath); // 假如目标路径不存在,则新建该路径 if (!outFile.getParentFile().exists()) { logger.info("合成后图片目标路径不存在,新建该路径"); outFile.getParentFile().mkdirs(); } // 假如目标文件不存在,则新建该文件 if (!outFile.exists()) { logger.info("合成后图片目标文件不存在,新建该文件"); outFile.createNewFile(); } Graphics2D backG = backImg.createGraphics(); logger.info("开始合成签名"); backG.drawImage(signatureImg, xPlace, yPlace, signatureImg.getWidth(), signatureImg.getHeight(), null); backG.dispose(); ImageIO.write(backImg, "png", new File(outFilePath)); logger.info("合成签名完成"); return true; } /** * 签名合成,图片坐标系左上角为(0,0) * * @param imgPath 图片路径 * @param signaturePath 签名图片路径 * @param outFilePath 合成后图片路径 * @param xPlace 签名图片放置的x坐标 * @param yPlace 签名图片放置的y坐标 * @param signatureWidth 签名图片的宽 * @param signatureHeight 签名图片的高 * @return 是否合成成功 * @throws IOException */ public static Boolean signatureSynthesis(String imgPath, String signaturePath, String outFilePath, int xPlace, int yPlace,int signatureWidth,int signatureHeight ) throws Exception { logger.info("进入签名合成方法"); BufferedImage backImg = ImageIO.read(new File(imgPath)); BufferedImage signatureImg = ImageIO.read(new File(signaturePath)); File outFile = new File(outFilePath); // 假如目标路径不存在,则新建该路径 if (!outFile.getParentFile().exists()) { logger.info("合成后图片目标路径不存在,新建该路径"); outFile.getParentFile().mkdirs(); } // 假如目标文件不存在,则新建该文件 if (!outFile.exists()) { logger.info("合成后图片目标文件不存在,新建该文件"); outFile.createNewFile(); } Graphics2D backG = backImg.createGraphics(); logger.info("开始合成签名"); backG.drawImage(signatureImg, xPlace, yPlace, signatureWidth, signatureHeight, null); backG.dispose(); ImageIO.write(backImg, "png", new File(outFilePath)); logger.info("合成签名完成"); return true; } /** * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx" * * @param inputFilePath * @return */ public static String getPostfix(String inputFilePath) { return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1); } }