package com.yeshi.fanli.service.impl.user; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.yeshi.fanli.dao.mybatis.share.ShareMapper; import com.yeshi.fanli.service.inter.user.QrCodeService; import com.yeshi.fanli.service.inter.user.SpreadUserImgService; import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.FileUtil; import com.yeshi.fanli.util.ImageUtil; import com.yeshi.fanli.util.StringUtil; import org.yeshi.utils.HttpUtil; import org.yeshi.utils.QRCodeUtil; import org.yeshi.utils.tencentcloud.COSManager; @Service public class QrCodeServiceImpl implements QrCodeService { @Resource private ShareMapper shareMapper; @Resource private SpreadUserImgService spreadUserImgService; @Override public String getPortrait(Long uid) { return shareMapper.getPortrait(uid); } @Override public String drawInviteQrCode(String url, Long uid, String portrait) throws IOException { if (url != null && !url.equals("")) { String targetPath = FileUtil.getCacheDir() + "/share_" + uid + "_" + System.currentTimeMillis() + ".jpg"; String erCodeTempPath = FileUtil.getCacheDir() + "/" + uid + "_" + System.currentTimeMillis() + ".jpg"; String erCode = HttpUtil.getShortLink("http://" + Constant.wxGZConfig.getLoginHost() + "/" + Constant.systemCommonConfig.getProjectName() + "/client/threeShareNew?uid=" + uid); // 生成 try { QRCodeUtil.getInstance(250).encode(erCode, erCodeTempPath); } catch (Exception e) { e.printStackTrace(); } InputStream urlInputStream = HttpUtil.getAsInputStream(url); // 背景 InputStream portraitInputStream = HttpUtil.getAsInputStream(portrait); // 头像 InputStream erCodeInputStream = new FileInputStream(new File(erCodeTempPath)); // 二维码 // 开始根据 url(背景图), qrCode(二维码), portrait(头像) 生成图片 ImageUtil.inviteFriendImg(urlInputStream, portraitInputStream, erCodeInputStream, targetPath); if (new File(targetPath).exists() && new File(targetPath).length() > 0) { // 上传文件 // 将url 转为 md5 让到下面中添加 在比较md5的值 int index = url.lastIndexOf("/"); String newUrl = url.substring(index + 1); String urlMd5 = newUrl.substring(0, newUrl.lastIndexOf(".")); String imgUrl = COSManager.getInstance() .uploadFile(new File(targetPath), "ercode/" + "ercode_" + uid + "_" + System.currentTimeMillis() + "_" + urlMd5 + ".jpg") .getUrl(); if (new File(erCodeTempPath).exists()) new File(erCodeTempPath).delete(); if (new File(targetPath).exists()) new File(targetPath).delete(); return imgUrl; } } return null; } @Override public String drawInviteQrCode(String url, Long uid, String portrait, int erCodePostionX, int erCodePostionY, int erCodeSize) throws IOException { if (!StringUtil.isNullOrEmpty(url)) { String targetPath = FileUtil.getCacheDir() + "/share_" + uid + "_" + System.currentTimeMillis() + ".jpg"; String erCodeTempPath = FileUtil.getCacheDir() + "/" + uid + "_" + System.currentTimeMillis() + ".jpg"; String erCode = HttpUtil.getShortLink("http://" + Constant.wxGZConfig.getLoginHost() + "/" + Constant.systemCommonConfig.getProjectName() + "/client/threeShareNew?uid=" + uid); // 生成 try { QRCodeUtil.getInstance(250).encode(erCode, erCodeTempPath); } catch (Exception e) { e.printStackTrace(); } InputStream urlInputStream = HttpUtil.getAsInputStream(url); // 背景 InputStream portraitInputStream = HttpUtil.getAsInputStream(portrait); // 头像 InputStream erCodeInputStream = new FileInputStream(new File(erCodeTempPath)); // 二维码 // 开始根据 url(背景图), qrCode(二维码), portrait(头像) 生成图片 ImageUtil.inviteFriendImg(urlInputStream, portraitInputStream, erCodeInputStream, targetPath, erCodePostionX, erCodePostionY, erCodeSize); if (new File(targetPath).exists() && new File(targetPath).length() > 0) { // 上传文件 // 将url 转为 md5 让到下面中添加 在比较md5的值 int index = url.lastIndexOf("/"); String newUrl = url.substring(index + 1); String urlMd5 = newUrl.substring(0, newUrl.lastIndexOf(".")); String imgUrl = COSManager.getInstance() .uploadFile(new File(targetPath), "ercode/" + "ercode_" + uid + "_" + urlMd5 + ".jpg").getUrl(); if (new File(erCodeTempPath).exists()) new File(erCodeTempPath).delete(); if (new File(targetPath).exists()) new File(targetPath).delete(); return imgUrl; } } return null; } }