package ma.luan.yiyan.util; import io.vertx.core.Future; import io.vertx.core.buffer.Buffer; import io.vertx.core.eventbus.ReplyException; import io.vertx.core.eventbus.ReplyFailure; import org.apache.commons.text.StringSubstitutor; import java.util.Base64; import java.util.HashMap; import java.util.Map; public class ConvertUtil { /** * @param obj base64 加密后的图片 * @return Buffer 流 */ public static Future<Buffer> getImageFromBase64(String obj) { Future<Buffer> result = Future.future(); if (obj == null) { result.fail(new ReplyException(ReplyFailure.RECIPIENT_FAILURE, 500, "图片读取失败")); return result; } Base64.Decoder decoder = Base64.getDecoder(); byte[] bs; bs = decoder.decode(obj); Buffer buffer = Buffer.buffer(bs); result.complete(buffer); return result; } private static final String svgTemplate = "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"${height}\" width = \"${width}\">\n" + " <!-- Generated by https://api.gushi.ci/ -->\n" + " <g>\n" + " <text text-anchor=\"start\" letter-spacing=\"${spacing}\" font-smoothing=\"antialiased\" font-family='KaiTi, \"Segoe UI\", \"Lucida Grande\", Helvetica, Arial, \"Microsoft YaHei\", FreeSans, Arimo, \"Droid Sans\",\"wenquanyi micro hei\",\"Hiragino Sans GB\", \"Hiragino Sans GB W3\", sans-serif' font-size=\"${font-size}\" id=\"svg_1\" y=\"${font-size}\" x=\"0\" stroke-width=\"0\" stroke=\"#000\" fill=\"#000000\">${content}</text>\n" + " </g>\n" + "</svg>"; public static String getSvg(String content, Double fontSize, Double spacing) { int length = content.length(); double realWidth = length * fontSize + (length-1) * spacing; double realFontSize = fontSize; Map<String, Object> templateValue = new HashMap<>(); templateValue.put("width", realWidth); templateValue.put("font-size", realFontSize); templateValue.put("spacing", spacing); templateValue.put("content", content); templateValue.put("height", realFontSize * 1.1); StringSubstitutor sub = new StringSubstitutor(templateValue); return sub.replace(svgTemplate); } }