生成方式的util已经在另一篇文章里说过啦,这篇文章要参考这以下的链接才能看得懂
1、第一种是生成base64的格式这种也是比较常见的
@ApiOperation(value = "海报生成,返回的是base64图片")
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "publishId", value = "分享海报发布id", paramType = "query", dataType = "Long")
})
@GetMapping(value = "/index")
public Result<Object> posterUtils(
@RequestParam Long publishId
) throws Exception {
PublishDO publishDO = findPetService.findPublishById(publishId);
//作为场景值
Long userId;
if (publishDO.getUserId() == 0) {
userId = Long.parseLong(publishDO.getExtObj().getString("oid"));
} else {
UserDO userDO = userServices.findUserById(publishDO.getUserId());
userId = userDO.getId();
}
Set<String> t = publishDO.getImages();
Iterator<String> it = t.iterator();
String str = "";
while (it.hasNext()) {
str = it.next();
}
BufferedImage bufferedImage=PosterUtils.createPoster(userServices.getAccessToken(), publishDO.getTitle(), userId, publishDO.getRewardAmount().toString(), str);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", os);
String encodeStr = Base64.getEncoder().encodeToString(os.toByteArray());
os.close();
return new ResultUtil<>().setData(encodeStr);
}
2、生成流方式,这分为读取流和下载流 ,先说读取流 这个有点问题,当时使用的是小程序,在ios端是没问题但是在安卓端出现了不能下载的问题,但是还是把代码贴出来
@ApiOperation(value = "海报生成,返回的是图片文件")
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "publishId", value = "分享海报发布id", paramType = "query", dataType = "Long")
})
@GetMapping(value = "/mp")
public void posterUtils(
@RequestParam Long publishId,
ServletResponse response
) throws Exception {
PublishDO publishDO = findPetService.findPublishById(publishId);
//作为场景值
Long userId;
if (publishDO.getUserId() == 0) {
userId = Long.parseLong(publishDO.getExtObj().getString("oid"));
} else {
UserDO userDO = userServices.findUserById(publishDO.getUserId());
userId = userDO.getId();
}
Set<String> t = publishDO.getImages();
Iterator<String> it = t.iterator();
String str = "";
while (it.hasNext()) {
str = it.next();
}
BufferedImage bufferedImage=PosterUtils.createPoster(userServices.getAccessToken(), publishDO.getTitle(), userId, publishDO.getRewardAmount().toString(), str);
ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
}
3、接下来是通用方法
@GetMapping(value = "/postImg/{publishId}/{fileName}")
public void postImg(
@PathVariable("publishId") Long publishId,
@PathVariable("fileName") String fileName,
ServletResponse response
) throws Exception {
PublishDO publishDO = findPetService.findPublishById(publishId);
//作为场景值
Long userId;
if (publishDO.getUserId() == 0) {
userId = Long.parseLong(publishDO.getExtObj().getString("oid"));
} else {
UserDO userDO = userServices.findUserById(publishDO.getUserId());
userId = userDO.getId();
}
Set<String> t = publishDO.getImages();
Iterator<String> it = t.iterator();
String str = "";
while (it.hasNext()) {
str = it.next();
}
BufferedImage bufferedImage=PosterUtils.createPoster(userServices.getAccessToken(), publishDO.getTitle(), userId, publishDO.getRewardAmount().toString(), str);
ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
}
这种使用参数然后把生成的流拼接成像图片一样的格式就ok啦