- 開源項目 jlatexmath
- 引入依賴
<dependency>
<groupId>org.scilab.forge</groupId>
<artifactId>jlatexmath</artifactId>
<version>1.0.7</version>
</dependency>
- 編碼測試
package test;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.scilab.forge.jlatexmath.TeXConstants;
import org.scilab.forge.jlatexmath.TeXFormula;
public class Test {
public static Image image(String latex) throws Exception {
int style = TeXConstants.STYLE_DISPLAY; // 樣式 符號以最大的尺寸呈現(xiàn)
float size = 24; // 生成公式圖片的字體大小
Color fg = Color.BLACK; // 字體顏色敏晤,黑色
Color bg = null; // 圖片背景色,默認(rèn)為透明北京
return TeXFormula.createBufferedImage(latex, style, size, fg, bg);
}
public static void main(String[] args) throws Exception {
String latex = "\\sqrt{{\\mathrm{a}}^{2}+{\\mathrm}^{2}}";
File file = new File("d:/test.png"); // 保存到文件
BufferedImage image = (BufferedImage)image(latex);
ImageIO.write(image, "png", file);
}
}
-
測試結(jié)果
test.png