图片转换为 latex 公式,识别图片中Latex公式,支持数学公式,化学公式,物理公式和生物公式,附Java代码和测试效果

本文展示了如何使用Java编写代码,通过接口调用识别图片中的LaTeX公式,实例演示了如何处理Latex识别,并提供了测试Demo和源码下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目  录

1.编写Java代码实现识别图片中Latex公式

2.测试结果

​3.源码下载


1.编写Java代码实现识别图片中Latex公式

直接上代码:

    public static String serverUrl = "接口地址";
    public static String accessToken = "***";
    public static String appKey = "您的appKey";
    public static String appSecret ="您的appSecret";
    public static Map<String,String> getLatex(String img){
        Map<String,String> addMap = new HashMap<String,String>();
        try {
            Img2LatexRequest request=new Img2LatexRequest();
            request.setImg(img);
            ZrscClient client = new DefaultZrscClient(serverUrl, accessToken, appKey,
                    appSecret);
            Img2LatexResponse response= client.execute(request);
            addMap.put("img",img);
            addMap.put("latex",response.getData());
            return addMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return addMap;

    }
Img2LatexRequest
public class Img2LatexRequest extends AbstractRequest implements ZrscRequest<Img2LatexResponse> {
    private String img;

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public Img2LatexRequest() {

    }
    public String getApiMethod() {
        return "img2latex.get";
    }

    public String getAppJsonParams() throws IOException {
        Map<String, Object> pmap = new TreeMap();
        pmap.put("img",this.img);
        return JsonUtil.toJson(pmap);
    }

    @Override
    public Class<Img2LatexResponse> getResponseClass() {
        return Img2LatexResponse.class;
    }

}
Img2LatexResponse
public class Img2LatexResponse extends AbstractResponse {
    private static final long serialVersionUID = -1029647126543204295L;
    public Img2LatexResponse(){}
    private String data;
    @JsonProperty("data")
    public String getData() {
        return data;
    }
    @JsonProperty("data")
    public void setData(String data) {
        this.data = data;
    }
}

2.测试结果

测试demo.html文件

<!DOCTYPE html>
<html>
<head>
<title>Latex测试图片</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1">

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>



</head>
<body>

<blockquote>

<h1>Latex测试图片</h1>


<style type="text/css">
table {
	border-collapse: collapse;
	margin: 0 auto;
	text-align: center;
}
table td, table th {
	border: 2px solid blue;
	color: red;
	height: 30px;
}
table thead th {
	background-color: blue;
	width: 100px;
}
table tr:nth-child(odd) {
	background: #fff;
}
table tr:nth-child(even) {
	background: #F5FAFA;
}
</style>
<table>
<thead>
<tr>
<th>编号</th>
<th>科目</th>
<th>原图</th>
<th>识别结果</th>
</tr>
</thead>
<tr>
<td>1</td>
<td>生物</td>
<td><img src="http://open.zrscsoft.com:81/static/images/img2latex/biology1.png"/></td>
<td>$$\text{C}^{{1}{8}}\text{O}_{{2}}\longrightarrow\text{H}_{{2}}^{{1}{8}}\text{O}\longrightarrow^{{1}{8}}\text{O}_{{2}}$$</td>
</tr>

<tr>
<td>2</td>
<td>数学</td>
<td><img src="http://open.zrscsoft.com:81/static/images/img2latex/math2-2.png"/></td>
<td>$${x}=\frac{-{b}\pm\sqrt{{b}^{{2}}-{4}{a}{c}}}{{2}{a}}$$</td>

</tr>

<tr>
<td>3</td>
<td>化学</td>
<td><img src="http://open.zrscsoft.com:81/static/images/img2latex/chemistry1.png"/></td>
<td>$${2}\text{H}\text{C}\text{O}_{{3}}^{-}+\text{C}\text{a}^{{2}+}+{2}\text{O}\text{H}^{-}\overset{\quad}{\underset{\quad}{=}}\text{C}\text{a}\text{C}\text{O}_{{3}}\downarrow+\text{C}\text{O}_{{3}}^{{2}-}+{2}\text{H}_{{2}}\text{O}$$</td>

</tr>

<tr>
<td>4</td>
<td>物理</td>
<td><img src="http://open.zrscsoft.com:81/static/images/img2latex/physics2-7.png"/></td>
<td>$${q}=\overline{{I}}{t}=\frac{\overline{{E}}}{{R}}{t}=\frac{\Delta\Phi}{{R}}$$</td>

</tr>



</table>



</blockquote>

</body>
</html>

demo.html运行后效果

3.源码下载

源码地址:https://download.csdn.net/download/jlq_diligence/22256057

源码中自带appkey和appsecret。

源码项目目录:

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋9

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值