根据百度云文字识别相关文档整理
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.