LatexUtils.java
3.97 KB
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 com.subsidy.util;
import org.scilab.forge.jlatexmath.TeXConstants;
import org.scilab.forge.jlatexmath.TeXFormula;
import org.scilab.forge.jlatexmath.TeXIcon;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* @author: tuyp
* @create: 2020-08-08 21:19
*/
public class LatexUtils {
/**
* @Description: 将base64编码字符串转换为图片
* @Author:
* @CreateTime:
* @param imgStr
* base64编码字符串
* @param path
* 图片路径-具体到文件
* @return
*/
public static boolean generateImage(String imgStr, String path) {
if (imgStr == null) {
return false;
}
// BASE64Decoder decoder = new BASE64Decoder();
try {
// byte[] b = decoder.decodeBuffer(imgStr);
// for (int i = 0; i < b.length; i++) {
// if (b[i] < 0) {
// b[i] += 256;
// }
// }
// OutputStream out = new FileOutputStream(path);
// out.write(b);
// out.flush();
// out.close();
return true;
} catch (Exception e) {
// TODO: handle exception
return false;
}
}
/**
* @Description: 根据图片地址转换为base64编码字符串
* @Author:
* @CreateTime:
* @return
*/
// public static String getImageStr(String imgFile) {
// InputStream inputStream = null;
// byte[] data = null;
// try {
// inputStream = new FileInputStream(imgFile);
// data = new byte[inputStream.available()];
// inputStream.read(data);
// inputStream.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// // 加密
// BASE64Encoder encoder = new BASE64Encoder();
// return encoder.encode(data);
// }
// latex 转 imgbase64
public static String latex2Png(String latex) {
try {
TeXFormula formula = new TeXFormula(latex);
// render the formla to an icon of the same size as the formula.
TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);
// insert a border
icon.setInsets(new Insets(1, 1, 1, 1));
// now create an actual image of the rendered equation
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2 = image.createGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
JLabel jl = new JLabel();
jl.setForeground(new Color(0, 0, 0));
icon.paintIcon(jl, g2, 0, 0);
// at this point the image is created, you could also save it with ImageIO
// saveImage(image, "png", "F:\\b.png");
// ImageIO.write(image, "png", new File("F:\\c.png"));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
ImageIO.write(image, "png", outputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
byte[] buffer = outputStream.toByteArray();
// BASE64Encoder encoder = new BASE64Encoder();
return ("data:image/png;base64,");
} catch (Exception e) {
// e.printStackTrace();
// ExceptionUtil.log(log, e);
System.err.println("公式解析有误:\n" + latex);
// e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
String str = latex2Png("\\(\\sqrt[3]{2+x}\\)");
System.out.println(str);
// String omml =Latex_Word.latexToWord(str);
// System.out.println(omml);
}
}