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 map = JSONObject.parseObject(JSONStr, new TypeReference
四、POST-MAN测试结果