微信公(gōng)衆号開發中(zhōng),微商(shāng)商(shāng)城系統經常會碰到要求系統自動生成授權圖片,試想如果會員較多(duō),一個個手工(gōng)處理(lǐ)費時費力。
一般會有(yǒu)如下要求:
自定義背景圖
附帶二維碼
附帶文(wén)字信息
public String genQrcode(TMember member) throws Exception {
/** 生成二維碼,用(yòng)于圖片合并 */
String url = WebConstants.PATH + "ucenter/member/inviteScan.do?inviteId=" + member.getId();
String logo = ContextUtil.getRealPath() + "/theme/wd/images/qrcode_logo.png";
String desDir = WebConstants.WEB_ROOT + "upload/code/";
String fileName = "code_" + member.getId() + ".jpg";
QRCodeUtil.encode(url, logo, desDir, fileName, true);
/** 生成推廣圖 */
String shareName = "share_" + member.getId() + ".jpg";
String qrcodeBg = ContextUtil.getRealPath() + "/theme/wd/images/qrcode_bg.png";
BufferedImage image1 = ImageIO.read(new File(qrcodeBg));
BufferedImage image2 = ImageIO.read(new File(WebConstants.WEB_ROOT + "upload/code/" + fileName));
BufferedImage combined = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_INT_RGB);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image1, 0, 0, null);
g.drawImage(image2, 95, 600, 150, 150, null);
// paint both images, preserving the alpha channels
String uname = member.getUname();
Calendar cal = Calendar.getInstance();
cal.setTime(member.getInputDate());
cal.add(Calendar.YEAR, 3);
Font font = new Font("微軟雅黑", Font.BOLD, 16);
Color color = new Color(0, 0, 0);
g.drawImage(image1, 0, 0, null);
g.setFont(font);
g.setColor(color);
g.drawString(uname, 200, 385);
g.drawString(DateUtils.formatShortDate(cal.getTime()), 191, 495);
g.drawString(member.getOpenid(), 191, 525);
// Save as new image
ImageIO.write(combined, "JPG", new File(desDir, shareName));
return "upload/code/" + shareName;
}
- 版權所有(yǒu):奇站網絡 轉載請注明出處
- 廈門奇站網絡科(kē)技(jì )有(yǒu)限公(gōng)司,專業提供網站建設,響應式網站建設,小(xiǎo)程序開發,系統定制開發。
- 軟件開發咨詢熱線(xiàn):吳小(xiǎo)姐 13313868605