Java后台生成图片的完整步骤

时间:2021-11-13 11:11:59

前言

由于从事的是支付行业,最近对接的一些第三方支付有时候需要我们这边提供用户的资料和交易信息,并且以图片的形式发送给他们,由于这是后台程序自动处理,然后直接发送给第三方支付公司,因此需要后台查出用户的相关信息,然后生成图片,调用第三方提供的接口,将图片链接以附件的形式发给他们,当用户发起dispute操作的时候,触发我方后台的自动处理机制。

Java后台生成图片

1. 依赖的jar包(maven依赖)

?
1
2
3
4
5
<dependency>
   <groupId>com.github.xuwei-k</groupId>
   <artifactId>html2image</artifactId>
   <version>0.1.0</version>
</dependency>

2. 代码

?
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
125
126
127
128
129
130
131
132
133
134
135
136
package com.generatePic;
 
import gui.ava.html.image.generator.HtmlImageGenerator;
 
public class TestGeneratePic {
 
    public static void main(String[] args) {
        generatePic();
    }
 
    public static void generatePic() {
        HtmlImageGenerator htmlImageGenerator = new HtmlImageGenerator();
 
        //format 表示html里的元素,比如表格
        String format = "<table>\n" +
                "  <tbody>\n" +
                "  <tr>\n" +
                "\t<img src="https://static.msport.com/public/up/pocket/operator/msportlogo.png" />\n" +
                "  </tr>\n" +
                "  </tbody>\n" +
                "</table>\n" +
                "\n" +
                "\n" +
                "<table border =1>\n" +
                "  <tbody>\n" +
                "    <tr>\n" +
                "      <td>User Account(user account number)</td>\n" +
                "      <td>%s</td>\n" +
                "    </tr>\n" +
                "   <tr>\n" +
                "      <td>user's email</td>\n" +
                "      <td>%s</td>\n" +
                "    </tr>\n" +
                "\t   <tr>\n" +
                "      <td>user's name</td>\n" +
                "      <td>%s</td>\n" +
                "    </tr>\n" +
                "  </tbody>\n" +
                "</table>\n" +
                "</br>\n" +
                "\n" +
                "<table>\n" +
                "  <tbody>\n" +
                "  <tr>\n" +
                "  <font size = "4">\n" +
                "  <b>PAYMENT INFOMATION</b> \n" +
                "  </font>\n" +
                "  </tr>\n" +
                "  </tbody>\n" +
                "</table>\n" +
                "\n" +
                "<table border =1>\n" +
                " <tbody>\n" +
                "  <tr>\n" +
                "      <td>Transaction Reference</td>\n" +
                "      <td>%s</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "      <td>Transaction Amount</td>\n" +
                "      <td>%s</td>\n" +
                "    </tr>\n" +
                "\t<tr>\n" +
                "      <td>Currency</td>\n" +
                "      <td>%s</td>\n" +
                "    </tr>\n" +
                "\t<tr>\n" +
                "      <td>Transaction Status</td>\n" +
                "      <td>%s</td>\n" +
                "    </tr>\n" +
                "\t<tr>\n" +
                "      <td>Transaction Date</td>\n" +
                "      <td>%s</td>\n" +
                "    </tr>\n" +
                "\t<tr>\n" +
                "      <td>Payment Type</td>\n" +
                "      <td>%s</td>\n" +
                "    </tr>\n" +
                "\t<tr>\n" +
                "      <td>Card Number</td>\n" +
                "      <td>%s</td>\n" +
                "    </tr>\n" +
                "\t<tr>\n" +
                "      <td>Service Details</td>\n" +
                "      <td>%s</td>\n" +
                "    </tr>\n" +
                "  </tbody> \n" +
                "</table>\n" +
                "<br/>\n" +
                "\n" +
                "<table>\n" +
                "  <tbody>\n" +
                "  <tr>\n" +
                "  <font size = "4">\n" +
                "  <b>SERVICE HISTORY</b> \n" +
                "  </font>\n" +
                "  </tr>\n" +
                "  </tbody>\n" +
                "</table>\n" +
                "\n" +
                "<table border =1>\n" +
                "<thead>\n" +
                "    <tr>\n" +
                "      <th>DATE</th>\n" +
                "      <th>DESCRIPTION</th>\n" +
                "      <th>AMOUNT</th>\n" +
                "      <th>BALANCE</th>\n" +
                "    </tr>\n" +
                "  </thead>\n" +
                "  \n" +
                "  <tbody>\n" +
                "    <tr>\n" +
                "      <td>%s</td>\n" +
                "      <td>%s</td>\n" +
                "      <td>%s</td>\n" +
                "      <td>%s</td>\n" +
                "    </tr>\n" +
                "   <tr>\n" +
                "      <td>%s</td>\n" +
                "      <td>%s</td>\n" +
                "      <td>%s</td>\n" +
                "      <td>%s</td>\n" +
                "    </tr>\n" +
                "  </tbody>\n" +
                "</table>\n" +
                "\n" +
                "\n";
        String finalHtml = "<html></body>" + format + "</body></html>";
        htmlImageGenerator.loadHtml(finalHtml);
 
        //图片名
        String fileName = "1" + ".png";
        
        htmlImageGenerator.saveAsImage(fileName);
 
    }
}

3. 生成的图片

Java后台生成图片的完整步骤

生成的图片路径默认在项目路径下:

Java后台生成图片的完整步骤

4. 实际应用的建议

可以通过   String.format() 函数, 拼接实际的数据,比如3中的%s就可以替换为实际的数据,最后生成自己想要的数据,如果你的前端技术足够好,那么你可以在后台画出更优美的图片。

总结

到此这篇关于Java后台生成图片的文章就介绍到这了,更多相关Java后台生成图片内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/6991081058011709447