公司新闻 行业动态 技术分享

java如何生成微商授权图片

发布日期: 2017-12-16浏览次数: 459

微信公众号开发中,微商商城系统经常会碰到要求系统自动生成授权图片,试想如果会员较多,一个个手工处理费时费力。

一般会有如下要求:
自定义背景图
附带二维码
附带文字信息

  1. public String genQrcode(TMember member) throws Exception {
  2. /** 生成二维码,用于图片合并 */
  3. String url = WebConstants.PATH + "ucenter/member/inviteScan.do?inviteId=" + member.getId();
  4. String logo = ContextUtil.getRealPath() + "/theme/wd/images/qrcode_logo.png";
  5. String desDir = WebConstants.WEB_ROOT + "upload/code/";
  6. String fileName = "code_" + member.getId() + ".jpg";
  7. QRCodeUtil.encode(url, logo, desDir, fileName, true);
  8. /** 生成推广图 */
  9. String shareName = "share_" + member.getId() + ".jpg";
  10. String qrcodeBg = ContextUtil.getRealPath() + "/theme/wd/images/qrcode_bg.png";
  11. BufferedImage image1 = ImageIO.read(new File(qrcodeBg));
  12. BufferedImage image2 = ImageIO.read(new File(WebConstants.WEB_ROOT + "upload/code/" + fileName));
  13. BufferedImage combined = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_INT_RGB);
  14. // paint both images, preserving the alpha channels
  15. Graphics g = combined.getGraphics();
  16. g.drawImage(image1, 0, 0, null);
  17. g.drawImage(image2, 95, 600, 150, 150, null);
  18. // paint both images, preserving the alpha channels
  19. String uname = member.getUname();
  20. Calendar cal = Calendar.getInstance();
  21. cal.setTime(member.getInputDate());
  22. cal.add(Calendar.YEAR, 3);
  23. Font font = new Font("微软雅黑", Font.BOLD, 16);
  24. Color color = new Color(0, 0, 0);
  25. g.drawImage(image1, 0, 0, null);
  26. g.setFont(font);
  27. g.setColor(color);
  28. g.drawString(uname, 200, 385);
  29. g.drawString(DateUtils.formatShortDate(cal.getTime()), 191, 495);
  30. g.drawString(member.getOpenid(), 191, 525);
  31. // Save as new image
  32. ImageIO.write(combined, "JPG", new File(desDir, shareName));
  33. return "upload/code/" + shareName;
  34. }

扫一扫关注我们

厦门奇站网络科技有限公司

电话:13313868605

QQ:3413772931

地址:厦门软件园三期A02

网站地图