背景需求,
官方SDK,在SpringBoot项目中过于臃肿,需要引入的Jar包过多,
在SpringBoot中,
本文使用SpringBoot中的RestTemplate对象进行请求接口文章来源:https://www.toymoban.com/news/detail-616902.html
案例代码如下文章来源地址https://www.toymoban.com/news/detail-616902.html
package com.example.demo2.Test;
import com.example.demo2.Test.Ocr.OcrResponse;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
public class OcrV3Demo2 {
private static final String YOUDAO_URL = "https://openapi.youdao.com/ocrapi";
private static final String APP_KEY = "AppID";
private static final String APP_SECRET = "密钥";
public static void main(String[] args) throws IOException {
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
String q = loadAsBase64("图片地址....");
String salt = String.valueOf(System.currentTimeMillis());
String detectType = "10012";
String imageType = "1";
String langType = "auto";
params.add("detectType", detectType);
params.add("imageType", imageType);
params.add("langType", langType);
params.add("img", q);
params.add("docType", "json");
params.add("signType", "v3");
String curtime = String.valueOf(System.currentTimeMillis() / 1000);
params.add("curtime", curtime);
String signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET;
String sign = getDigest(signStr);
params.add("appKey", APP_KEY);
params.add("salt", salt);
params.add("sign", sign);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<OcrResponse> responseEntity = restTemplate.exchange(YOUDAO_URL, HttpMethod.POST, requestEntity, OcrResponse.class);
System.out.println("输出....");
System.out.println(responseEntity.getBody());
}
public static String getDigest(String string) {
if (string == null) {
return null;
}
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
byte[] btInput = string.getBytes();
try {
MessageDigest mdInst = MessageDigest.getInstance("SHA-256");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (byte byte0 : md) {
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
public static String loadAsBase64(String imgFile) {
File file = new File(imgFile);
if (!file.exists()) {
System.out.println("文件不存在");
return null;
}
try (InputStream in = new FileInputStream(imgFile)) {
byte[] data = new byte[in.available()];
in.read(data);
return Base64.getEncoder().encodeToString(data);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String truncate(String q) {
if (q == null) {
return null;
}
int len = q.length();
return len <= 20 ? q : (q.substring(0, 10) + len + q.substring(len - 10, len));
}
}
到了这里,关于有道OCR图文识别整合SpringBoot的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!