Spring Boot + hutool 创建海报图片

Spring Boot + hutool 创建海报图片

/**
     * 分享,生成图片
     * @param id
     * @return
     */
    @GetMapping("/getShareImg")
    public void getShareImg(String id,HttpServletResponse response) throws IOException {
        CouponConsignSaleClassify byId = couponConsignSaleClassifyService.getById(id);
        if(byId == null){
            throw new GlobalException("查询 id 为空 ");
        }

        CouponConsignSaleGoods one = couponConsignSaleGoodsService.lambdaQuery().eq(CouponConsignSaleGoods::getClassifyId, id).ge(CouponConsignSaleGoods::getStock, 1)
                .orderByAsc(CouponConsignSaleGoods::getPurchasePrice).last(" limit 0,1").one();

        /*final int w = 270 * 2;
        final int h = 340 * 2;*/

        final int w = 405;
        final int h = 550;

        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_BGR);
        Graphics2D graphics2D = bi.createGraphics();
        //绘制白色背景
        graphics2D.setColor(Color.white);
        graphics2D.fillRect(0, 0, w, h);

        //释放资源
        graphics2D.dispose();

        String tempPath = System.getProperty("java.io.tmpdir");

        //System.out.println(tempPath);

        File outputfile = new File(tempPath+"poster.jpg");
        ImageIO.write(bi, "jpg", outputfile);

        //分类图片
        Image image = ImgUtil.getImage(new URL(transformStyle(byId.getImg())));

        File scaleImg = new File(tempPath+"imgScale.jpg");
        ImgUtil.scale(image,scaleImg,1.2f);
        BufferedImage read = ImageIO.read(scaleImg);
        //画图片
        ImgUtil.pressImage(outputfile,outputfile,read,0,-140,1f);

        //商品名称
        String name = byId.getName();
        ImgUtil.pressText(outputfile,outputfile,name,Color.black,new Font("黑体", Font.PLAIN, 20),-170+(name.length() * 10),0,0.8f);

        String lowestPrice=byId.getMaxPrice()==null?"":byId.getMaxPrice().toPlainString();
        if(one != null){
            lowestPrice = one.getPrice().toPlainString();
        }

        //最低价格
        String p="最低价格:"+lowestPrice;
        ImgUtil.pressText(outputfile,outputfile,p,Color.red,new Font("黑体", Font.PLAIN, 22),-200 + (p.length() * 11),35,0.8f);

        //原价
        ImgUtil.pressText(outputfile,outputfile,"原价:"+(byId.getMaxPrice()==null?"-":byId.getMaxPrice()),Color.black,new Font("黑体", Font.PLAIN, 15),50,30,0.8f);

        String url="http://qy.gsjf.cc/h5/#/pages/buy/consignBuy/consignBuy?id="+byId.getId();
        String qrFile=tempPath+"qrFile.jpg";
        File file = FileUtil.file(qrFile);

        //生成二维码
        QrCodeUtil.generate(url, 180, 180,
                file);
        BufferedImage qrFile0 = ImageIO.read(file);
        //显示二维码
        ImgUtil.pressImage(outputfile,outputfile,qrFile0,0,130,1f);

        //显示文字
        ImgUtil.pressText(outputfile,outputfile,"识别二维码查看详情",Color.black,new Font("黑体", Font.PLAIN, 16),-07,250,0.6f);

       /* ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bi, "jpg", os);*/


        byte[] bFile = Files.readAllBytes(outputfile.toPath());

        response.setContentType("image/jpg"); //设置返回的文件类型
        OutputStream respOut = response.getOutputStream();
        respOut.write(bFile);
        respOut.flush();
        respOut.close();
    }

    /**
     * 对中文字符进行UTF-8编码
     * @param source 要转义的字符串
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String transformStyle(String source) throws UnsupportedEncodingException
    {
        char[] arr = source.toCharArray();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < arr.length; i++)
        {
            char temp = arr[i];
            if(isChinese(temp))
            {
                sb.append(URLEncoder.encode("" + temp, "UTF-8"));
                continue;
            }
            sb.append(arr[i]);
        }
        return sb.toString();
    }
    /**
     * 判断是不是中文字符
     * @param c
     * @return
     */
    public static boolean isChinese(char c)
    {

        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

        if(ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS

                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS

                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A

                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION

                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION

                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)
        {

            return true;

        }

        return false;

    }

效果

相关推荐
陈大爷(有低保)8 分钟前
UDP Socket聊天室(Java)
java·网络协议·udp
kinlon.liu22 分钟前
零信任安全架构--持续验证
java·安全·安全架构·mfa·持续验证
王哲晓43 分钟前
Linux通过yum安装Docker
java·linux·docker
java6666688881 小时前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存1 小时前
源码分析:LinkedList
java·开发语言
执键行天涯1 小时前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
Adolf_19931 小时前
Flask-JWT-Extended登录验证, 不用自定义
后端·python·flask
Jarlen1 小时前
将本地离线Jar包上传到Maven远程私库上,供项目编译使用
java·maven·jar
蓑 羽1 小时前
力扣438 找到字符串中所有字母异位词 Java版本
java·算法·leetcode
叫我:松哥1 小时前
基于Python flask的医院管理学院,医生能够增加/删除/修改/删除病人的数据信息,有可视化分析
javascript·后端·python·mysql·信息可视化·flask·bootstrap