SpringBoot+阿里云OCR图片识别
准备条件:阿里云OCR图片识别API购买,初次购买1分钱500次接口调用
一、控制层
@GetMapping("/uploadManual")
@ApiOperation("上传药品说明书图片、扫描后返回文字信息")
@ApiImplicitParam(name="file",value="说明书图片",required=true,paramType="query",dataType = "file")
public R uploadManual(@RequestParam("file") MultipartFile file) {
return R.success(drugManualService.uploadManual(file));
}
二、业务层
@OperationLog(detail = "上传药品说明书图片、扫描后返回文字信息", level = 3, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
public String uploadManual(MultipartFile file) {
StringBuffer imageOCRToText = new StringBuffer();
try {
byte[] bytes = file.getBytes();
String imageBase64Code = new BASE64Encoder().encode(bytes);
imageOCRToText = JSONUtil.imageOCRToText(appcode,host,path,imageBase64Code);
} catch (IOException e) {
throw new BadRequestException(e.getMessage());
}
return imageOCRToText==null?"":imageOCRToText.toString();
}
三、JSONUtil工具类,调用阿里OCR-API接口并处理返回数据
package com.cdyx.utils;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.cdyx.exception.BadRequestException;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @program: knowlage-platform
* @description
* @author: xbwen
* @create: 2021-08-02 09:19
**/
public class JSONUtil {
/**
* @Author: xbwen
* @Description: 将JSON字符串中的word拼接成StringBuffer
* @DateTime: 9:20 2021/8/2
* @Params: * @param JSONStr
* @Return
*/
public static StringBuffer JSONToString(String JSONStr) {
Map<String, String> map = JSONObject.parseObject(JSONStr, new TypeReference<Map<String, String>>() {});
String retStr = map.get("ret");
List<Map<String, String>> retList = JSONObject.parseObject(retStr, new TypeReference<List<Map<String, String>>>() {});
StringBuffer retBuffer = new StringBuffer();
retList.forEach(retMap -> {
retBuffer.append(retMap.get("word")).append("\n");
});
return retBuffer;
}
/**
* @Author: xbwen
* @Description: 调用阿里云服务器的图片识别接口,获取识别出来的文字数据
* @DateTime: 11:42 2021/8/2
* @Params: * @param null
* @Return
*/
public static StringBuffer imageOCRToText(String appcode,String host,String path,String imageBase64Code) {
String method = "POST";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 3d04bb801071452bb6cf7e11396d112f
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/json; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
String bodys = "{\"image\":\""+imageBase64Code+"\",\"configure\":{\"min_size\":16,\"output_prob\":true,\"output_keypoints\":false,\"skip_detection\":false,\"without_predicting_direction\":false}}";
StringBuffer imageOCRToText = new StringBuffer();
try {
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//获取response的body
String JSONStr = EntityUtils.toString(response.getEntity());
imageOCRToText = JSONUtil.JSONToString(JSONStr);
} catch (Exception e) {
throw new BadRequestException(e.getMessage());
}
return imageOCRToText;
}
}
四、POST-MAN测试结果
本文来自博客园,作者:IT波少,转载请注明原文链接:https://www.cnblogs.com/itbs/p/15152601.html