一、controller層代碼
package com.wy.gcserver.ocr.controller;
import com.wy.gcserver.ocr.service.AliyunApiOcrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 阿裡雲API調用OCR識别Controller層.
*
*/
@Api(tags = "AliyunApiOcrController", description = "阿裡雲API調用OCR識别")
@RestController
@RequestMapping(value = "/aliyun/api/ocr")
public class AliyunApiOcrController {
@Autowired
private AliyunApiOcrService aliyunApiOcrService;
/**
* 擷取身份證OCR識别資訊.
*
* @param job
* @param image
* @return
*/
@ApiOperation("擷取身份證OCR識别資訊")
@RequestMapping(value = "/getIdcardOcr",method = RequestMethod.POST)
public String getIdcardOcr(@RequestParam(value = "job") @ApiParam("正反面辨別:face-人臉面/back-國徽面") String job,
@RequestParam(value = "image") @ApiParam("圖檔base64") String image){
String result = aliyunApiOcrService.getIdcardOcr(job, image);
return result;
}
/**
* 擷取戶口本OCR識别資訊.
*
* @param image
* @return
*/
@ApiOperation("擷取戶口本OCR識别資訊")
@RequestMapping(value = "/getHouseholdOcr",method = RequestMethod.POST)
public String getHouseholdOcr(@RequestParam(value = "image") @ApiParam("圖檔base64") String image){
String result = aliyunApiOcrService.getHouseholdOcr(image);
return result;
}
}
二、sevice層代碼
package com.wy.gcserver.ocr.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wy.gcserver.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class AliyunApiOcrService {
private static Logger logger = LoggerFactory.getLogger(AliyunApiOcrService.class);
@Value("${ocr.appCode}")
private String appCode;
// @Value("${ocr.host}")
// private String host;
//
// @Value("${ocr.path}")
// private String path;
private static final String FIELD_OCR_HOST_IDCARD = "http://dm-51.data.aliyun.com";
private static final String FIELD_OCR_PATH_IDCARD = "/rest/160601/ocr/ocr_idcard.json";
private static final String FIELD_OCR_HOST_HOUSEHOLD = "https://household.market.alicloudapi.com";
private static final String FIELD_OCR_PATH_HOUSEHOLD = "/api/predict/ocr_household_register";
/**
* 擷取header資訊.
*
* @return
*/
private Map<String, String> getHeaders(){
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "APPCODE " + appCode);
//根據API的要求,定義相對應的Content-Type
headers.put("Content-Type", "application/json; charset=UTF-8");
return headers;
}
/**
* 公共請求方法.
*
* @param host
* @param path
* @param bodys
* @return
*/
private String generalMethod(String host, String path, String bodys){
String result = "";
Map<String, String> headers = getHeaders();
Map<String, String> querys = new HashMap<String, String>();
try {
/**
* 重要提示如下:
* HttpUtils請從
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下載下傳
*
* 相應的依賴請參照
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
HttpResponse response = HttpUtils.doPost(host, path, "POST", headers, querys, bodys);
int stat = response.getStatusLine().getStatusCode();
if (stat != 200) {
System.out.println("Http code: " + stat);
System.out.println("http header error msg: " + response.getFirstHeader("X-Ca-Error-Message"));
System.out.println("Http body error msg:" + EntityUtils.toString(response.getEntity()));
return null;
}
String res = EntityUtils.toString(response.getEntity());
JSONObject res_obj = JSON.parseObject(res);
System.out.println(res_obj.toJSONString());
result = res_obj.toJSONString();
} catch (Exception e) {
logger.info(e.getMessage());
}
return result;
}
/**
* 身份證識别.
*
* @param side
* @param imgBase64
* @return
*/
public String getIdcardOcr(String side, String imgBase64) {
Map<String, String> headers = getHeaders();
Map<String, String> querys = new HashMap<String, String>();
//configure配置
JSONObject configObj = new JSONObject();
configObj.put("side", side);
String config_str = configObj.toString();
// 拼裝請求body的json字元串
JSONObject requestObj = new JSONObject();
requestObj.put("image", imgBase64);
if (configObj.size() > 0) {
requestObj.put("configure", config_str);
}
String bodys = requestObj.toString();
String result = generalMethod(FIELD_OCR_HOST_IDCARD, FIELD_OCR_PATH_IDCARD, bodys);
return result;
}
/**
* 戶口本識别.
*
* @param imgBase64
* @return
*/
public String getHouseholdOcr(String imgBase64){
// 拼裝請求body的json字元串
JSONObject requestObj = new JSONObject();
requestObj.put("image", imgBase64);
String bodys = requestObj.toString();
String result = generalMethod(FIELD_OCR_HOST_HOUSEHOLD, FIELD_OCR_PATH_HOUSEHOLD, bodys);
return result;
}
}
三、附件