<img>标签传参如何生成图片?

<img src="/GetText.aspx?Text=Post%3A%20200000%20%20E-mail%3A%20mp%40mingpian.sh&FontName=%E6%96%B9%E6%AD%A3%E9%AD%8F%E7%A2%91%E7%AE%80%E4%BD%93&FontSize=11&RGBColor=rgb(223%2C%2053%2C%20142)&Fontweight=Normal&Fontstyle=Normal"/>

https://img3.mukewang.com/5cbd23ec0001e34002280020.jpg

青春有我
浏览 983回答 3
3回答

慕哥9229398

php的话,后端使用gd库,c#的话,后端用gdi+都可以动态生成图片的

慕莱坞森

题主的问题是如何传参,在 JSP 中,一般是通过 el 表达式来完成的。<img src="/getText?text=${text.text}&amp;font_name=${text.font_name}&amp;font_size=${text.font_size}&amp;rgb_color=${text.rgb_color}&amp;font_weight=${text.font_weight}&amp;font_style=${text.font_style}">至于 Java 后端的实现可以是这样的 (这里我以 SpringMVC 为例):@RequestMapping(value = "getText",method = RequestMethod.GET)public void getText(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @RequestParam(value = "text",required = false,defaultValue = "这里输入文字")String text,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @RequestParam(value = "font_name",required = false,defaultValue = "黑体")String font_name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @RequestParam(value = "rgb_color",required = false,defaultValue = "rgb(0,0,0)")String rgb_color,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @RequestParam(value = "font_style",required = false,defaultValue = "0")String font_style,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @RequestParam(value = "font_size",required = false,defaultValue = "14")String font_size,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @RequestParam(value = "font_weight",required = false,defaultValue = "0")String font_weight,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse response) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; Graphics tmp = new BufferedImage(300,200,BufferedImage.TYPE_INT_ARGB).getGraphics();&nbsp; &nbsp; &nbsp; &nbsp; tmp.setFont(DrawUtil.getFont(font_name, Integer.parseInt(font_size), Integer.parseInt(font_style), Integer.parseInt(font_weight)));&nbsp; &nbsp; &nbsp; &nbsp; FontMetrics fm = tmp.getFontMetrics();&nbsp; &nbsp; &nbsp; &nbsp; tmp.dispose();&nbsp; &nbsp; &nbsp; &nbsp; BufferedImage image = new BufferedImage(fm.stringWidth(text),fm.getHeight(),BufferedImage.TYPE_INT_ARGB);&nbsp; &nbsp; &nbsp; &nbsp; Graphics g = image.getGraphics();&nbsp; &nbsp; &nbsp; &nbsp; ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);&nbsp; &nbsp; &nbsp; &nbsp; g.setColor(DrawUtil.getColor(rgb_color));&nbsp; &nbsp; &nbsp; &nbsp; g.setFont(DrawUtil.getFont(font_name, Integer.parseInt(font_size), Integer.parseInt(font_style), Integer.parseInt(font_weight)));&nbsp; &nbsp; &nbsp; &nbsp; g.drawString(text, 0, fm.getHeight()-5);&nbsp; &nbsp; &nbsp; &nbsp; g.dispose();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; try(ByteArrayOutputStream os = new ByteArrayOutputStream();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OutputStream osm = response.getOutputStream()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImageIO.write(image, "png", os);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.setContentType("image/png");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.setCharacterEncoding("UTF-8");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IOUtils.write(os.toByteArray(), osm);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; osm.flush();&nbsp; &nbsp; &nbsp; &nbsp; }catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }想要什么样的文字图片,由前端的 JSP 页面传递对应的参数即可。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java