spring boot 生成拼团海报

依赖

<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.7</version>
        </dependency>

生成海报图片

/**
     * 拼团海报
     */
    @AppLog(value = "拼团海报", type = 1)
    @AuthCheck
    @GetMapping("/poster")
    @ApiOperation(value = "拼团海报",notes = "拼团海报")
    public ApiResult<Object> poster(String id) throws IOException {
        YxUser yxUser = LocalUser.getUser();
        String apiUrl = systemConfigService.getData(SystemConfigConstants.API_URL);
        if(StrUtil.isEmpty(apiUrl)){
            throw new YshopException("未配置api地址");
        }
        if(StringUtils.isBlank(id)){
            throw new YshopException("参数 id 不能为空");
        }

        String fileDir = path+"qrcode"+ File.separator;

        YxStorePinkMaster byId = yxStorePinkMasterService.getById(id);
        if(byId == null){
            throw new YshopException("拼团不存在或已结束");
        }

        YxStoreCombination byId1 = yxStoreCombinationService.getById(byId.getPinkProductId());
        if(byId1 == null){
            throw new YshopException("拼团产品不存在");
        }
        YxStorePinkMember yxStorePinkMember = null;
        List<YxStorePinkMember> list = yxStorePinkMemberService.lambdaQuery().eq(YxStorePinkMember::getPId, byId.getId()).list();
        if(!list.isEmpty()){
            yxStorePinkMember = list.get(0);
        }

        String QRCode = StrUtil.format("pink_{}_{}_QRCode.jpg",byId.getPinkProductId(),yxUser.getUid());
        String poster = StrUtil.format("pink_{}_{}_poster.jpg",byId.getPinkProductId(),yxUser.getUid());

        String qrcodeUrl="";
        String posterUrl="";

        YxSystemAttachment info = systemAttachmentService.getInfo(QRCode);
        if (info != null){
            qrcodeUrl = apiUrl + "/api/file/" + info.getSattDir();
        }else{
            QrCodeUtil.generate(StrUtil.format("{}/transferPage?type=joinPink&masterId={}",apiUrl,byId.getId()), 180, 180,
                    FileUtil.file(fileDir+QRCode));
            qrcodeUrl = apiUrl + "/api/file/qrcode/"+QRCode;

            YxSystemAttachment attachment0 =  YxSystemAttachment.builder()
                    .name(QRCode)
                    .attSize(String.valueOf(FileUtil.size(new File(fileDir+QRCode))))
                    .attDir(fileDir+QRCode)
                    .attType("image/jpeg")
                    .sattDir("qrcode/"+QRCode)
                    .build();
            systemAttachmentService.save(attachment0);
        }

        YxSystemAttachment info1 = systemAttachmentService.getInfo(poster);
        if(info1 != null){
            posterUrl = apiUrl + "/api/file/" + info1.getSattDir();
        }else{
            /**
             * 开始创建图片
             */
            BufferedImage img = new BufferedImage(1400, 1040, BufferedImage.TYPE_INT_RGB);
            //开启画图
            Graphics g = img.getGraphics();
            InputStream stream =  getClass().getClassLoader().getResourceAsStream("background.png");
            ImageInputStream background = ImageIO.createImageInputStream(stream);
            BufferedImage back = ImageIO.read(background);
            g.drawImage(back.getScaledInstance(1400, 1040, Image.SCALE_DEFAULT), 0, 0, null); // 绘制缩小后的图
            g.dispose();
            //先将画好的海报写到本地
            File file = new File(fileDir+poster);
            try {
                ImageIO.write(img, "jpg",file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Image image0 = ImageIO.read(new File(fileDir+poster) );

            //商品图
            Image image = ImageIO.read(new URL(byId1.getImage()));
            Image scale = ImgUtil.scale(
                    image,
                    1350,
                    580
            );

            Image image1 = ImgUtil.pressImage(
                    image0,
                    scale, //水印图片
                    0, //x坐标修正值。 默认在中间,偏移量相对于中间偏移
                    -195, //y坐标修正值。 默认在中间,偏移量相对于中间偏移
                    1f
            );
            //头像
            Image image2 = ImageIO.read(new URL(yxUser.getAvatar()));
            Image scale4 = ImgUtil.scale(
                    image2,
                    160,
                    160
            );
            Image image3 = ImgUtil.pressImage(
                    image1,
                    scale4, //水印图片
                    -596, //x坐标修正值。 默认在中间,偏移量相对于中间偏移
                    220, //y坐标修正值。 默认在中间,偏移量相对于中间偏移
                    1f
            );
            //二维码
            Image image5 = ImageIO.read(new URL(qrcodeUrl));
            Image scale5 = ImgUtil.scale(
                    image5,
                    250,
                    250
            );
            Image image6 = ImgUtil.pressImage(
                    image3,
                    scale5, //水印图片
                    540, //x坐标修正值。 默认在中间,偏移量相对于中间偏移
                    350, //y坐标修正值。 默认在中间,偏移量相对于中间偏移
                    1f
            );

            //邀请语
            Image image7 = ImgUtil.pressText(image6, yxUser.getNickname() + " " + "邀请您加入拼团:", Color.BLACK, new Font("黑体", Font.BOLD, 40), -200, 180, 1F);

            //已支付人数
            image7 = ImgUtil.pressText(image7, StrUtil.format("已支付人数:{}",byId.getPaidPeopleNumber()), Color.BLACK, new Font("黑体", Font.BOLD, 40), -350, 260, 1F);
            //结束时间
            image7 = ImgUtil.pressText(image7, StrUtil.format("结束时间:{}", DateUtil.format(byId.getFinishDate(), "yyyy-MM-dd HH:mm:ss")), Color.BLACK, new Font("黑体", Font.BOLD, 40), 200, 260, 1F);

            //扫码提示
            image7 = ImgUtil.pressText(image7, "扫描二维码加入拼团", Color.BLACK, new Font("黑体", Font.BOLD, 30), 505, 505, 1F);

            if(yxStorePinkMember != null){
                //原价
                image7 = ImgUtil.pressText(image7, StrUtil.format("原价:{}",yxStorePinkMember.getBeforeDiscountAmount()), Color.BLACK, new Font("黑体", Font.BOLD, 40), -365, 340, 1F);
                //当前阶梯团优惠价
                image7 = ImgUtil.pressText(image7, StrUtil.format("当前人数优惠价:{}", yxStorePinkMember.getAfterDiscountAmount()), Color.BLACK, new Font("黑体", Font.BOLD, 40), 100, 340, 1F);
            }

            ImgUtil.write(image7,file);
            YxSystemAttachment attachment0 =  YxSystemAttachment.builder()
                    .name(poster)
                    .attSize(String.valueOf(FileUtil.size(new File(fileDir+poster))))
                    .attDir(fileDir+poster)
                    .attType("image/jpeg")
                    .sattDir("qrcode/"+poster)
                    .build();
            systemAttachmentService.save(attachment0);

            posterUrl = apiUrl + "/api/file/qrcode/"+poster;
        }
        return ApiResult.ok(posterUrl);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值