最近工作上写了点好久没使用了的java绘图代码,都快忘干净了,所以记录一下,最终实现的效果是下图这个样子,红色周边是美工设计的背景图,白色区域是要动态添加内容。
上代码:
1 package com.yueyongyueyou.common.service.impl; 2 3 import cn.hutool.core.img.ImgUtil; 4 import com.yueyongyueyou.common.service.IUploadFileDubboService; 5 import com.yueyongyueyou.common.service.ImageService; 6 import com.yueyongyueyou.config.service.IConfigParamService; 7 import org.springframework.stereotype.Service; 8 9 import javax.annotation.Resource; 10 import java.awt.*; 11 import java.awt.font.TextAttribute; 12 import java.awt.image.BufferedImage; 13 import java.math.BigDecimal; 14 import java.net.URL; 15 import java.text.AttributedString; 16 import java.text.DecimalFormat; 17 18 /** 19 * 图片绘制实现类 20 */ 21 @Service 22 public class ImageServiceImpl implements ImageService { 23 /** 24 * 业务参数获取实现类 25 */ 26 @Resource 27 private IConfigParamService configParamService; 28 /** 29 * 上传oss功能实现类 30 */ 31 @Resource 32 private IUploadFileDubboService uploadService; 33 34 /** 35 * 将商品图、价格绘制到指定得背景图上,并上传到oss服务器 36 * 37 * @param goodsImg 商品图 38 * @param groupPrice 商品拼团价 39 * @param productPrice 商品零售价 40 * @return 上传后得图片访问地址 41 * @throws Exception 42 */ 43 @Override 44 public String generateAppletShareImg(String goodsImg, BigDecimal groupPrice, BigDecimal productPrice) throws Exception { 45 //获取配置好的背景图 46 String imgPrefix = configParamService.getParamValue("yyyy.img.prefix"); 47 String bgImg = configParamService.getParamValue("group:booking:share:bgimg"); 48 49 //背景图 50 BufferedImage bgImage = ImgUtil.read(new URL(imgPrefix + bgImg)); 51 52 //商品图 53 BufferedImage goodsImage = ImgUtil.read(new URL(imgPrefix + goodsImg)); 54 int w = goodsImage.getWidth(); 55 int h = goodsImage.getHeight(); 56 int min = Math.min(w, h); 57 //切成尺寸最大得正方形 58 Image cut = ImgUtil.cut(goodsImage, new Rectangle((w - min) / 2, (h - min) / 2, min, min)); 59 60 //定义颜色和字体 61 Color color_B3BCC9 = ImgUtil.getColor("#B3BCC9"); 62 Color color_FF1355 = ImgUtil.getColor("#FF1355"); 63 Color color_F43011 = ImgUtil.getColor("#F43011"); 64 Font font_ht_33 = new Font("SimHei", Font.PLAIN, 33); 65 Font font_ht_36 = new Font("SimHei", Font.PLAIN, 36); 66 Font font_serif_33 = new Font("Microsoft Sans Serif", Font.PLAIN, 33); 67 Font font_serif_42 = new Font("Microsoft Sans Serif", Font.PLAIN, 42); 68 69 //定义文字变量 70 final String labelText1 = "幸运价:", labelText2 = "立即参与", labelSymbol = "¥", labelDot = "."; 71 72 DecimalFormat decimalFormat = new DecimalFormat("0.00#"); 73 String groupPriceStr = decimalFormat.format(groupPrice == null ? BigDecimal.ZERO : groupPrice); 74 String[] split = groupPriceStr.split("\\."); 75 String integerPart = split[0]; 76 String decimalPart = split[1]; 77 String productPriceStr = decimalFormat.format(productPrice == null ? BigDecimal.ZERO : productPrice); 78 79 //开始绘画 80 Graphics2D graphics = bgImage.createGraphics(); 81 //抗锯齿 82 graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 83 //画白底 84 graphics.setColor(Color.WHITE); 85 graphics.fillRoundRect(30, 144, 570, 327, 18, 18); 86 //画商品 87 graphics.drawImage(cut, 54, 171, 279, 279, null); 88 //写右侧文字 89 graphics.setColor(color_B3BCC9); 90 graphics.setFont(font_ht_33); 91 graphics.drawString(labelText1, 363, 189 + 27); 92 //写团购价人民币符号 93 graphics.setColor(color_FF1355); 94 graphics.setFont(font_serif_33); 95 graphics.drawString(labelSymbol, 363, 246 + 37); 96 //写团购价整数部分 97 FontMetrics symbolMetrics = graphics.getFontMetrics(font_serif_33); 98 int symbolTextLength = symbolMetrics.stringWidth(labelSymbol); 99 symbolTextLength += 3; 100 graphics.setColor(color_FF1355); 101 graphics.setFont(font_serif_42); 102 graphics.drawString(integerPart + labelDot, 363 + symbolTextLength, 246 + 37); 103 //写团购价小数部分 104 FontMetrics numericMetrics = graphics.getFontMetrics(font_serif_33); 105 int numericTextLength = numericMetrics.stringWidth(integerPart + labelDot); 106 int adjust = (integerPart.length() + 1) * 5; //横坐标微调优化 107 graphics.setColor(color_FF1355); 108 graphics.setFont(font_serif_33); 109 graphics.drawString(decimalPart, 363 + symbolTextLength + numericTextLength + adjust, 246 + 37); 110 //写零售价 111 graphics.setColor(color_B3BCC9); 112 AttributedString attrString = new AttributedString(labelSymbol + productPriceStr); 113 attrString.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON); 114 attrString.addAttribute(TextAttribute.FONT, font_serif_33); 115 graphics.drawString(attrString.getIterator(), 363, 306 + 29); 116 //画红色按钮 117 graphics.setColor(color_F43011); 118 graphics.fillRoundRect(351, 378, 225, 72, 72, 72); 119 //写按钮文字 120 graphics.setColor(Color.WHITE); 121 graphics.setFont(font_ht_36); 122 graphics.drawString(labelText2, 393, 384 + 42); 123 124 //上传到oss服务器 125 String url = uploadService.uploadManagerFile(ImgUtil.toBytes(bgImage, ImgUtil.IMAGE_TYPE_PNG), ImgUtil.IMAGE_TYPE_PNG, 1, 0); 126 127 return url; 128 } 129 }