SpringBoot+阿里雲OCR圖片識別
阿新 • • 發佈:2021-08-17
準備條件:阿里雲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/swpu-wxb/p/15152601.html