基于Java的MathML转图片的方法(示例代码)
作者:布禾 时间:2023-11-25 14:29:39
Maven依赖:
<dependency>
<groupId>de.rototor.jeuclid</groupId>
<artifactId>jeuclid-core</artifactId>
<version>3.1.14</version>
</dependency>
示例:
@Test
public void testMathMlToImg() throws IOException {
//MathML字符串
String mathStr = "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n" +
" <mi>f</mi>\n" +
" <mo>(</mo>\n" +
" <mi>x</mi>\n" +
" <mo>)</mo>\n" +
" <mo>></mo>\n" +
" <mfrac>\n" +
" <msqrt>\n" +
" <mn>2</mn>\n" +
" </msqrt>\n" +
" <mn>8</mn>\n" +
" </mfrac>\n" +
" <mo>+</mo>\n" +
" <mn>1</mn>\n" +
" </math>";
//创建转换器
Converter converter = Converter.getInstance();
//创建layoutContext并设置相关参数
//参数设置可以参考net.sourceforge.jeuclid.context.Parameter类
//参数默认值可以参考LayoutContextImpl类构造方法
LayoutContextImpl layoutContext = (LayoutContextImpl) LayoutContextImpl.getDefaultLayoutContext();
//设置公式字体大小,默认12.0pt
layoutContext.setParameter(Parameter.MATHSIZE, 30);
//设置公式颜色,默认黑色
layoutContext.setParameter(Parameter.MATHCOLOR, Color.RED);
//转换
Dimension dimension = converter.convert(mathStr, new FileOutputStream("f:/1.png"), "image/png", layoutContext);
if(dimension != null){
System.out.println("图片转换成功!高:" + dimension.getHeight() + " 宽:" + dimension.getWidth());
}
}
输出结果:
图片转换成功!高:70.0 宽:149.0
转换结果如下:
关于outFileType参数的取值,支持以下几种:
image/png
image/vnd.wap.wbmp
image/x-png
image/jpeg
image/tiff
image/bmp
image/gif
来源:https://www.cnblogs.com/seve/archive/2021/06/23/14906931.html
标签:Java,MathML,转图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入解析C#编程中struct所定义的结构
2022-03-01 12:43:08
C# 如何解析获取Url参数值
2022-07-03 01:00:52
java 中堆内存和栈内存理解
2023-04-23 19:24:05
c#实现简单控制台udp异步通信程序示例
2022-06-13 18:54:38
Java8新特性之默认方法和静态方法
2021-08-29 13:34:26
![](https://img.aspxhome.com/file/2023/4/89954_0s.jpg)
Android studio点击跳转WebView详解
2022-12-12 05:11:17
Java枚举类使用Lombok方式
2022-06-28 22:07:02
Android 沉浸式改变小米魅族状态栏颜色的实例代码
2023-11-18 15:13:16
![](https://img.aspxhome.com/file/2023/5/125395_0s.png)
springboot中的springSession的存储和获取实现
2023-11-04 10:23:14
Android APK使用Debug签名重新打包 Eclipse更改默认Debug签名
2022-03-07 13:27:12
Android中Fragment与Activity的生命周期对比
2021-12-12 06:28:01
![](https://img.aspxhome.com/file/2023/4/137524_0s.jpg)
IntelliJ IDEA安装插件阿里巴巴Java开发手册(Alibaba Java Coding Guidelines)
2023-10-17 11:30:02
![](https://img.aspxhome.com/file/2023/3/113443_0s.png)
springmvc处理模型数据Map过程解析
2022-04-24 01:06:01
![](https://img.aspxhome.com/file/2023/8/129278_0s.png)
java poi sax方式处理大数据量excel文件
2021-09-19 19:28:48
Spring实战之使用注解实现声明式事务操作示例
2021-08-19 07:16:07
Android Data Binding 在 library module 中遇到错误及解决办法
2021-08-31 12:16:31
用Java连接sqlserver数据库时候几个jar包的区别分析
2021-11-07 02:18:01
Java编程复用类代码详解
2021-09-13 10:06:34
Flutter 实现网易云音乐字幕的代码
2023-04-01 11:50:45
![](https://img.aspxhome.com/file/2023/4/138084_0s.gif)
Jmeter如何添加循环控制器
2021-06-26 20:09:52
![](https://img.aspxhome.com/file/2023/8/76508_0s.png)