admin
2019-02-19 9192375a211cff2f91d1f65f932f97612d8dbcdb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package org.fanli.service.user.service.impl.invite;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
 
import javax.annotation.Resource;
 
import org.fanli.facade.user.service.invite.QrCodeService;
import org.fanli.facade.user.service.invite.SpreadUserImgService;
import org.fanli.facade.user.util.InviteImageUtil;
import org.springframework.stereotype.Service;
import org.yeshi.utils.FileUtil;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.QRCodeUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.base.Constant;
 
@Service
public class QrCodeServiceImpl implements QrCodeService {
    @Resource
    private SpreadUserImgService spreadUserImgService;
 
 
    @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(头像) 生成图片
            InviteImageUtil.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(头像) 生成图片
            InviteImageUtil.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;
    }
 
}