public class ImageRemarkUtils {
private static Logger LOG = Logger.getLogger(ImageRemarkUtils.class);
public static final String IMG_TEMP_FILE_PATH = ConfigUtil.getServerPath() + "/tempImg/";
public static final String IMG_COMPOSITE_PATH = ConfigUtil.getServerPath() + "/compositeImg/";
public static String imageJointAndRemark(String img1Path, String img2Path, String resImgPath, String content1, String content2) {
// File file = new File(IMG_TEMP_FILE_PATH);
// if (!file.exists()) {
// file.mkdirs();
// }
// File file2 = new File(IMG_COMPOSITE_PATH);
// if (!file2.exists()) {
// file2.mkdirs();
// }
LOG.debug("------target pic path=:" + resImgPath);
try {
File fileOne = new File(img1Path);
BufferedImage imageFirst = ImageIO.read(fileOne);
int[] imageArrayFirst = new int[imageFirst.getWidth() * imageFirst.getHeight()];
imageArrayFirst = imageFirst.getRGB(0, 0, imageFirst.getWidth(), imageFirst.getHeight(), imageArrayFirst, 0, imageFirst.getWidth());
File fileTwo = new File(img2Path);
BufferedImage imageSecond = ImageIO.read(fileTwo);
int[] imageArraySecond = new int[imageSecond.getWidth() * imageSecond.getHeight()];
imageArraySecond = imageSecond.getRGB(0, 0, imageSecond.getWidth(), imageSecond.getHeight(), imageArraySecond, 0, imageSecond.getWidth());
int width = imageFirst.getWidth() + imageSecond.getWidth();
int height = imageFirst.getHeight() > imageSecond.getHeight() ? imageFirst.getHeight() : imageSecond.getHeight();
int fontBgHeight = 120;
BufferedImage imageResult = new BufferedImage(width, height + fontBgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = imageResult.createGraphics();
Color color = new Color(250, 5, 5, 255);
Color color_bg = new Color(245, 245, 245, 255);
Font font = new Font("微软雅黑", Font.BOLD, 40);
// Font font = new Font("仿宋", Font.BOLD, 40);
g2d.setBackground(color_bg);
g2d.clearRect(0, 0, width, fontBgHeight);
g2d.setColor(color);
g2d.setFont(font);
g2d.drawString(content1, 12, 50);
g2d.drawString(content2, 12, 100);
g2d.dispose();
imageResult.setRGB(0, 0, width, 0, new int[width * 10], 0, width);
imageResult.setRGB(0, fontBgHeight, imageFirst.getWidth(), imageFirst.getHeight(), imageArrayFirst, 0, imageFirst.getWidth());
imageResult.setRGB(imageFirst.getWidth(), fontBgHeight, imageSecond.getWidth(), imageSecond.getHeight(), imageArraySecond, 0,
imageSecond.getWidth());
File outFile = new File(resImgPath);
LOG.debug("new file path=" + resImgPath);
boolean flag = ImageIO.write(imageResult, "jpg", outFile);
return flag ? resImgPath : null;
} catch (Exception e) {
LOG.error("img jointing failed!", e);
}
return null;
}