百度云文字识别接口

本文档详细介绍了如何利用百度云的文字识别接口进行操作,重点讨论了在项目中添加pom文件的相关依赖配置。
摘要由CSDN通过智能技术生成

根据百度云文字识别相关文档整理

pom文件相关依赖

<!-- 百度云依赖   -->
<dependency>
    <groupId>com.baidu.aip</groupId>
    <artifactId>java-sdk</artifactId>
    <version>4.8.0</version>
</dependency>
import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.util.Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;

@RestController
@RequestMapping("/baiducloud/word")
@Api(tags = "百度云 文字识别")
public class Demo {

    public static final String APP_ID = "";
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";
    public static  AipOcr CLIENT;

    @PostMapping(value ="getAipOcr")
    @ApiOperation("获取accesstoken")
    public void getAipOcr(){
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        CLIENT =client;
    }

    @PostMapping(value ="getByFilePath")
    @ApiOperation("通用文字识别(普通版) 参数为本地图片路径")
    public String getByFilePath(AipOcr client,String image) throws IOException {
        client =CLIENT;
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");

        // 参数为本地图片路径
        //String image = "card_2.png";
        JSONObject res = client.basicGeneral(image, options);
        System.out.println(res.toString(2));
        return res.toString(2);
    }

    @PostMapping(value ="getByFileBytes")
    @ApiOperation("通用文字识别(普通版) 参数为本地图片二进制数组")
    public String getByFileBytes(AipOcr client,byte[] file) throws IOException {
        client =CLIENT;
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");
        // 参数为本地图片二进制数组
        //byte[] file = Util.readFileByBytes(image);
        JSONObject res = client.basicGeneral(file, options);
        System.out.println(res.toString(2));
        return res.toString(2);
    }

    @PostMapping(value ="getByUrl")
    @ApiOperation("通用文字识别(普通版) 参数为远程url图片")
    public String getByUrl(AipOcr client,String url){
        client =CLIENT;
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");
        // 通用文字识别, 图片参数为远程url图片
        JSONObject res = client.basicGeneralUrl(url, options);
        System.out.println(res.toString(2));
        return res.toString(2);
    }

    @PostMapping(value = "getAccuracyByFilePath")
    @ApiOperation("通用文字识别(高精度版) 参数为本地图片路径")
    public String getAccuracyByFilePath(AipOcr client,String image){
        client =CLIENT;
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");
        options.put("probability", "true");
        JSONObject res = client.basicAccurateGeneral(image, options);
        System.out.println(res.toString(2));
        return res.toString(2);
    }

    @PostMapping(value ="getAccuracyByFileBytes")
    @ApiOperation("通用文字识别(高精度版) 参数为本地图片二进制数组")
    public String getAccuracyByFileBytes(AipOcr client,byte[] file) throws IOException {
        client =CLIENT;
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");
        options.put("probability", "true");
        //byte[] file = Util.readFileByBytes(image);
        JSONObject res = client.basicAccurateGeneral(file, options);
        System.out.println(res.toString(2));
        return res.toString(2);
    }

    @PostMapping(value ="getByFilePathIncludeLocation")
    @ApiOperation("通用文字识别(含位置信息版)参数为本地图片路径 ")
    public String getByFilePathIncludeLocation(AipOcr client,String image){
        client =CLIENT;
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("recognize_granularity", "big");
        options.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值