当前位置: 首页 > news >正文

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;}

效果

http://www.lryc.cn/news/242635.html

相关文章:

  • 0002Java程序设计-springboot在线考试系统小程序
  • Linux(Centos)上使用crontab实现定时任务(定时执行脚本)
  • 【Leetcode合集】20. 有效的括号
  • OpenGL 绘制线(Qt)
  • Java | 多线程并发编程CountDownLatch实践
  • 分布式定时任务系列6:XXL-job触发日志过大引发的CPU告警
  • Spark RDD、DataFrame和Dataset的区别和联系
  • 代码随想录算法训练营第四十五天|139.单词拆分、背包问题总结
  • 深度学习卫星遥感图像检测与识别 -opencv python 目标检测 计算机竞赛
  • wxWidgets 3.2.4发布 —— 发布于2023年11月11日
  • PyQt6运行QTDesigner生成的ui文件程序
  • 基于mediapipe的人手21点姿态检测模型—CPU上检测速度惊人
  • 系统架构设计: 21 论敏捷软件开发方法及其应用
  • 【深度学习】脸部修复,CodeFormer,论文,实战
  • OpenGL_Learn14(光照贴图)
  • 【JVM精讲与GC调优教程(概述)】
  • 蓝桥杯物联网竞赛_STM32L071_2_继电器控制
  • python之pyqt专栏2-项目文件解析
  • Kafka 集群如何实现数据同步
  • opencv- CLAHE 有限对比适应性直方图均衡化
  • IOS免签封装打包苹果APP的方法
  • Springboot引入分布式搜索引擎Es RestAPI
  • Lua脚本解决redis实现的分布式锁多条命令原子性问题
  • Vatee万腾独特科技力量的前沿探索:Vatee的数字化奇点
  • C++面试,const的使用
  • 小总结----长度
  • 【深度学习】如何选择神经网络的超参数
  • jQuery 3.0 新增了哪些特性?(jQuery 3 所引入的那些最重要的变化)
  • MindStudio学习一 整体介绍
  • excel表中慎用合并单元格,多用跨列居中