直接上代码:
public static void createGTImage(GtInfo resultObj) { String backPath = resultObj.getBackPath(); String enterpriseName = resultObj.getEnterpriseName(); String gtResultPath = resultObj.getGtResultPath(); int gtResultPathX = resultObj.getGtResultPathX(); String bingPic = resultObj.getBingPic(); String iName = resultObj.getIName(); String tanXiaoValue = resultObj.getTanXiaoValue(); String levelPath = resultObj.getLevelPath(); int levelPathY = resultObj.getLevelPathY(); String totalTan = resultObj.getTotalTan(); String totalTanPercent = resultObj.getTotalTanPercent(); String resultPath = resultObj.getResultPath(); try { // 目标文件 File _file = new File(backPath); // 目标图片对象 Image src = ImageIO.read(_file); // 目标图片宽度 int wideth = src.getWidth(null); // 目标图片高度 int height = src.getHeight(null); // 实例化缓存图片对象 BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB); // 缓存图片对象画笔 Graphics g = image.createGraphics(); // 在缓存图片上先画目标图片 g.drawImage(src, 0, 0, wideth, height, null); g.setColor(Color.white); Font font = new Font(FONTNAME, Font.PLAIN, 100); // 添加字体的属性设置 g.setFont(font); //文字水平居中显示 2520图片最大宽度 FontMetrics fm = g.getFontMetrics(font); int markWidth = fm.stringWidth(enterpriseName); int widthX = (2520 - markWidth) / 2; //将水印绘入图片 g.drawString(enterpriseName,widthX,750); if(!StringUtils.isEmpty(levelPath)){ // 水印文件 File _filebiao = new File(levelPath); // 水印图片对象 Image src_biao = ImageIO.read(_filebiao); // 水印图片宽度 int wideth_biao = src_biao.getWidth(null); // 水印图片高度 int height_biao = src_biao.getHeight(null); // 在缓存图片上再画水印图片 g.drawImage(src_biao, 800,levelPathY , wideth_biao, height_biao, null); } if(!StringUtils.isEmpty(gtResultPath)){ // 水印文件 File _filebiao = new File(gtResultPath); // 水印图片对象 Image src_biao = ImageIO.read(_filebiao); // 水印图片宽度 int wideth_biao = src_biao.getWidth(null); // 水印图片高度 int height_biao = src_biao.getHeight(null); // 在缓存图片上再画水印图片 g.drawImage(src_biao, gtResultPathX,2200 , wideth_biao, height_biao, null); } if(!StringUtils.isEmpty(bingPic)){ // 水印文件 File _filebiao = new File(bingPic); // 水印图片对象 Image src_biao = ImageIO.read(_filebiao); // 水印图片宽度 int wideth_biao = src_biao.getWidth(null); // 水印图片高度 int height_biao = src_biao.getHeight(null); // 在缓存图片上再画水印图片 g.drawImage(src_biao, 1400,1020 , wideth_biao, height_biao, null); } // Font font=new Font("微软雅黑", Font.PLAIN, 80); // g.setFont(font); if(!StringUtils.isEmpty(iName)){ g.drawString(iName,800,2720); } if(!StringUtils.isEmpty(totalTan)){ g.drawString(totalTan,1750,2920); } if(!StringUtils.isEmpty(tanXiaoValue)){ g.drawString(tanXiaoValue,1750,3093); } if(!StringUtils.isEmpty(totalTanPercent)){ g.drawString(totalTanPercent,1750,3265); } // 结束 g.dispose(); ImageIO.write(image, "png", new File(resultPath)); } catch (Exception e) { log.info("生成高碳报告异常:"+e); } }
GtInfo 类:
import lombok.Data; @Data public class GtInfo { private String enterpriseName; private String levelPath ; private int levelPathY ; private String gtResultPath ; private int gtResultPathX ; private String tanXiaoValue ; private String bingPic ; private String iName ; private String totalTan ; private String totalTanPercent ; private String backPath ; private String resultPath ; }
生成文字图片,图片背景透明
public static String makewatermark(String mark,String resultPicPath){ Font font=new Font("微软雅黑", Font.PLAIN, 110); Integer width=2250; Integer height=125; // 创建图片 BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_BGR); Graphics2D g2d = image.createGraphics(); //设置图片透明 image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); g2d = image.createGraphics(); g2d.setColor(Color.white); //设置倾斜角度 // g2d.rotate(Math.toRadians(-35), (double) image.getWidth() / 2, (double) image.getHeight() / 2); g2d.setFont(font); // 消除java.awt.Font字体的锯齿 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 0.3f是透明系数 ,透明系数取值范围是 0 ~ 1.0 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f)); FontMetrics fm = g2d.getFontMetrics(font); int markWidth = fm.stringWidth(mark); int widthX = (width - markWidth) / 2; //将水印绘入图片 g2d.drawString(mark,widthX,105); g2d.dispose(); //返回图片的base64码 String base64 = null; try { // ByteArrayOutputStream stream = new ByteArrayOutputStream(); ImageIO.write(image, "png", new File(resultPicPath)); // ImageIO.write(image, "png", stream); // base64 = Base64.encode(stream.toByteArray()); // stream.flush(); // stream.close(); }catch (IOException e){ e.printStackTrace(); } return base64; }
原图:
叠加+文字处理后: