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测试结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值