您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Java后臺(tái)生成圖片的完整過(guò)程”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
由于從事的是支付行業(yè),最近對(duì)接的一些第三方支付有時(shí)候需要我們這邊提供用戶(hù)的資料和交易信息,并且以圖片的形式發(fā)送給他們,由于這是后臺(tái)程序自動(dòng)處理,然后直接發(fā)送給第三方支付公司,因此需要后臺(tái)查出用戶(hù)的相關(guān)信息,然后生成圖片,調(diào)用第三方提供的接口,將圖片鏈接以附件的形式發(fā)給他們,當(dāng)用戶(hù)發(fā)起dispute操作的時(shí)候,觸發(fā)我方后臺(tái)的自動(dòng)處理機(jī)制。
1. 依賴(lài)的jar包(maven依賴(lài))
<dependency> <groupId>com.github.xuwei-k</groupId> <artifactId>html2image</artifactId> <version>0.1.0</version> </dependency>
2. 代碼
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. 生成的圖片
生成的圖片路徑默認(rèn)在項(xiàng)目路徑下:
4. 實(shí)際應(yīng)用的建議
可以通過(guò) String.format() 函數(shù), 拼接實(shí)際的數(shù)據(jù),比如3中的%s就可以替換為實(shí)際的數(shù)據(jù),最后生成自己想要的數(shù)據(jù),如果你的前端技術(shù)足夠好,那么你可以在后臺(tái)畫(huà)出更優(yōu)美的圖片。
“Java后臺(tái)生成圖片的完整過(guò)程”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。