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

前言

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

Java后台生成图片

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


   com.github.xuwei-k
   html2image
   0.1.0

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 = "\n" +
                "  \n" +
                "  \n" +
                "\t\n" +
                "  \n" +
                "  \n" +
                "
\n" + "\n" + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "\t \n" + " \n" + " \n" + " \n" + " \n" + "
User Account(user account number)%s
user's email%s
user's name%s
\n" + "
\n" + "\n" + "\n" + " \n" + " \n" + " \n" + " PAYMENT INFOMATION \n" + " \n" + " \n" + " \n" + "
\n" + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "\t\n" + " \n" + " \n" + " \n" + "\t\n" + " \n" + " \n" + " \n" + "\t\n" + " \n" + " \n" + " \n" + "\t\n" + " \n" + " \n" + " \n" + "\t\n" + " \n" + " \n" + " \n" + "\t\n" + " \n" + " \n" + " \n" + " \n" + "
Transaction Reference%s
Transaction Amount%s
Currency%s
Transaction Status%s
Transaction Date%s
Payment Type%s
Card Number%s
Service Details%s
\n" + "
\n" + "\n" + "\n" + " \n" + " \n" + " \n" + " SERVICE HISTORY \n" + " \n" + " \n" + " \n" + "
\n" + "\n" + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
DATEDESCRIPTIONAMOUNTBALANCE
%s%s%s%s
%s%s%s%s
\n" + "\n" + "\n"; String finalHtml = "" + format + ""; htmlImageGenerator.loadHtml(finalHtml); //图片名 String fileName = "1" + ".png"; htmlImageGenerator.saveAsImage(fileName); } }

3. 生成的图片

Java后台生成图片的完整步骤_第1张图片

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

Java后台生成图片的完整步骤_第2张图片

4. 实际应用的建议

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

总结

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

你可能感兴趣的:(Java后台生成图片的完整步骤)