1.修改优化
This commit is contained in:
parent
215008075c
commit
fdccf1b8da
@ -1,5 +1,9 @@
|
||||
package com.heyu.api.controller;
|
||||
|
||||
import com.heyu.api.alibaba.request.ACommonTextRequest;
|
||||
import com.heyu.api.data.constants.ApiConstants;
|
||||
import com.heyu.api.data.utils.ApiR;
|
||||
|
||||
public class BaseController {
|
||||
|
||||
|
||||
@ -13,4 +17,23 @@ public class BaseController {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean hasImage(ACommonTextRequest request) {
|
||||
return request != null && (!isBlank(request.getImageUrl()) || !isBlank(request.getImageBase64()));
|
||||
}
|
||||
|
||||
protected boolean isFaceOrBack(String side) {
|
||||
return ApiConstants.face.equals(side) || ApiConstants.back.equals(side);
|
||||
}
|
||||
|
||||
protected String thirdError(ApiR<?> apiR) {
|
||||
if (apiR != null && !isBlank(apiR.getErrorMsg())) {
|
||||
return apiR.getErrorMsg();
|
||||
}
|
||||
return "识别失败";
|
||||
}
|
||||
|
||||
protected boolean isBlank(String value) {
|
||||
return value == null || value.trim().length() == 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -5,13 +5,15 @@ import com.aliyun.ocr20191230.models.RecognizeDriverLicenseResponseBody;
|
||||
import com.heyu.api.alibaba.handle.common.text.ARecognizeDriverLicenseHandle;
|
||||
import com.heyu.api.alibaba.request.common.text.ARecognizeDriverLicenseRequest;
|
||||
import com.heyu.api.controller.BaseController;
|
||||
import com.heyu.api.resp.car.RecognizeDriverLicenseBackResp;
|
||||
import com.heyu.api.resp.car.RecognizeDriverLicenseFaceResp;
|
||||
import com.heyu.api.data.annotation.NotIntercept;
|
||||
import com.heyu.api.data.constants.ApiConstants;
|
||||
import com.heyu.api.data.utils.ApiR;
|
||||
import com.heyu.api.data.utils.R;
|
||||
import com.heyu.api.resp.car.RecognizeDriverLicenseBackResp;
|
||||
import com.heyu.api.resp.car.RecognizeDriverLicenseFaceResp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@ -43,61 +45,70 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@NotIntercept
|
||||
public class RecognizeDriverLicenseController extends BaseController {
|
||||
|
||||
|
||||
@Autowired
|
||||
private ARecognizeDriverLicenseHandle aRecognizeDriverLicenseHandle;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@RequestMapping("/recognize")
|
||||
@PostMapping("/recognize")
|
||||
public R recognize(ARecognizeDriverLicenseRequest request) {
|
||||
|
||||
RecognizeDriverLicenseFaceResp faceResp = new RecognizeDriverLicenseFaceResp();
|
||||
RecognizeDriverLicenseBackResp backResp = new RecognizeDriverLicenseBackResp();
|
||||
ApiR<RecognizeDriverLicenseResponse> aR = aRecognizeDriverLicenseHandle.handle(request);
|
||||
|
||||
|
||||
if (aR.isSuccess()) {
|
||||
RecognizeDriverLicenseResponse response = aR.getData();
|
||||
if (isSuccessStatusCode(response.getStatusCode())) {
|
||||
RecognizeDriverLicenseResponseBody.RecognizeDriverLicenseResponseBodyData
|
||||
responseBodyData = response.getBody().getData();
|
||||
|
||||
if ("face".equals(request.getSide())) {
|
||||
RecognizeDriverLicenseResponseBody.RecognizeDriverLicenseResponseBodyDataFaceResult
|
||||
faceResult = responseBodyData.getFaceResult();
|
||||
|
||||
faceResp.setVehicleType(faceResult.getVehicleType());
|
||||
faceResp.setIssueDate(faceResult.getIssueDate());
|
||||
faceResp.setEndDate(faceResult.getEndDate());
|
||||
faceResp.setGender(faceResult.getGender());
|
||||
faceResp.setAddress(faceResult.getAddress());
|
||||
faceResp.setStartDate(faceResult.getStartDate());
|
||||
faceResp.setLicenseNumber(faceResult.getLicenseNumber());
|
||||
faceResp.setNationality(faceResult.getNationality());
|
||||
faceResp.setIssueUnit(faceResult.getIssueUnit());
|
||||
faceResp.setNationality(faceResult.getNationality());
|
||||
faceResp.setBirthDate(faceResult.getBirthDate());
|
||||
return R.ok().setData(faceResp);
|
||||
}
|
||||
if ("back".equals(request.getSide())) {
|
||||
RecognizeDriverLicenseResponseBody.RecognizeDriverLicenseResponseBodyDataBackResult
|
||||
backResult = responseBodyData.getBackResult();
|
||||
|
||||
backResp.setArchiveNumber(backResult.getArchiveNumber());
|
||||
backResp.setName(backResult.getName());
|
||||
backResp.setCardNumber(backResult.getCardNumber());
|
||||
backResp.setRecord(backResult.getRecord());
|
||||
return R.ok().setData(backResp);
|
||||
}
|
||||
}
|
||||
if (!hasImage(request)) {
|
||||
return R.error("imageUrl和imageBase64不能同时为空");
|
||||
}
|
||||
if (!isFaceOrBack(request.getSide())) {
|
||||
return R.error("side只能为face或back");
|
||||
}
|
||||
|
||||
ApiR<RecognizeDriverLicenseResponse> aR = aRecognizeDriverLicenseHandle.handle(request);
|
||||
if (!isValidAliResponse(aR)) {
|
||||
return R.error(thirdError(aR));
|
||||
}
|
||||
|
||||
return R.error("识别失败");
|
||||
RecognizeDriverLicenseResponseBody.RecognizeDriverLicenseResponseBodyData data =
|
||||
aR.getData().getBody().getData();
|
||||
if (ApiConstants.face.equals(request.getSide())) {
|
||||
if (data.getFaceResult() == null) {
|
||||
return R.error("未识别到驾驶证正页信息");
|
||||
}
|
||||
return R.ok().setData(toFaceResp(data.getFaceResult()));
|
||||
}
|
||||
if (data.getBackResult() == null) {
|
||||
return R.error("未识别到驾驶证副页信息");
|
||||
}
|
||||
return R.ok().setData(toBackResp(data.getBackResult()));
|
||||
}
|
||||
|
||||
private boolean isValidAliResponse(ApiR<RecognizeDriverLicenseResponse> apiR) {
|
||||
return apiR != null
|
||||
&& apiR.isSuccess()
|
||||
&& apiR.getData() != null
|
||||
&& isSuccessStatusCode(apiR.getData().getStatusCode())
|
||||
&& apiR.getData().getBody() != null
|
||||
&& apiR.getData().getBody().getData() != null;
|
||||
}
|
||||
|
||||
private RecognizeDriverLicenseFaceResp toFaceResp(
|
||||
RecognizeDriverLicenseResponseBody.RecognizeDriverLicenseResponseBodyDataFaceResult faceResult) {
|
||||
RecognizeDriverLicenseFaceResp faceResp = new RecognizeDriverLicenseFaceResp();
|
||||
faceResp.setVehicleType(faceResult.getVehicleType());
|
||||
faceResp.setIssueDate(faceResult.getIssueDate());
|
||||
faceResp.setEndDate(faceResult.getEndDate());
|
||||
faceResp.setGender(faceResult.getGender());
|
||||
faceResp.setAddress(faceResult.getAddress());
|
||||
faceResp.setStartDate(faceResult.getStartDate());
|
||||
faceResp.setLicenseNumber(faceResult.getLicenseNumber());
|
||||
faceResp.setName(faceResult.getName());
|
||||
faceResp.setIssueUnit(faceResult.getIssueUnit());
|
||||
faceResp.setNationality(faceResult.getNationality());
|
||||
faceResp.setBirthDate(faceResult.getBirthDate());
|
||||
return faceResp;
|
||||
}
|
||||
|
||||
private RecognizeDriverLicenseBackResp toBackResp(
|
||||
RecognizeDriverLicenseResponseBody.RecognizeDriverLicenseResponseBodyDataBackResult backResult) {
|
||||
RecognizeDriverLicenseBackResp backResp = new RecognizeDriverLicenseBackResp();
|
||||
backResp.setArchiveNumber(backResult.getArchiveNumber());
|
||||
backResp.setName(backResult.getName());
|
||||
backResp.setCardNumber(backResult.getCardNumber());
|
||||
backResp.setRecord(backResult.getRecord());
|
||||
return backResp;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
package com.heyu.api.controller.car;
|
||||
|
||||
|
||||
import com.aliyun.ocr20191230.models.RecognizeDrivingLicenseResponse;
|
||||
import com.aliyun.ocr20191230.models.RecognizeDrivingLicenseResponseBody;
|
||||
import com.heyu.api.alibaba.handle.common.text.ARecognizeDrivingLicenseHandle;
|
||||
@ -17,6 +16,7 @@ import com.heyu.api.resp.car.RecognizeDrivingLicenseBackResp;
|
||||
import com.heyu.api.resp.car.RecognizeDrivingLicenseFaceResp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@ -41,94 +41,124 @@ import java.util.Map;
|
||||
@NotIntercept
|
||||
public class RecognizeDrivingLicenseController extends BaseController {
|
||||
|
||||
|
||||
@Autowired
|
||||
private ARecognizeDrivingLicenseHandle arRecognizeDrivingLicenseHandle;
|
||||
|
||||
|
||||
@Autowired
|
||||
@Autowired
|
||||
private BVehicleLicenseHandle bVehicleLicenseHandle;
|
||||
|
||||
|
||||
@RequestMapping("/recognize")
|
||||
public R recognize(ARecognizeDrivingLicenseRequest aRecognizeDrivingLicenseRequest) {
|
||||
RecognizeDrivingLicenseBackResp backResp = new RecognizeDrivingLicenseBackResp();
|
||||
RecognizeDrivingLicenseFaceResp faceResp = new RecognizeDrivingLicenseFaceResp();
|
||||
|
||||
BVehicleLicenseRequest bVehicleLicenseRequest = new BVehicleLicenseRequest();
|
||||
bVehicleLicenseRequest.setVehicleLicenseSide(aRecognizeDrivingLicenseRequest.getSide());
|
||||
bVehicleLicenseRequest.setImageUrl(aRecognizeDrivingLicenseRequest.getImageUrl());
|
||||
bVehicleLicenseRequest.setImageBase64(aRecognizeDrivingLicenseRequest.getImageBase64());
|
||||
ApiR<Map> bR = bVehicleLicenseHandle.handle(bVehicleLicenseRequest);
|
||||
|
||||
if (bR.isSuccess()) {
|
||||
Map<String, Object> data = bR.getData();
|
||||
if (ApiConstants.face.equals(aRecognizeDrivingLicenseRequest.getSide())) {
|
||||
faceResp.setIssueDate(MapUtils.getByExpr(data, "words_result.发证日期.words"));
|
||||
faceResp.setModel(MapUtils.getByExpr(data, "words_result.品牌型号.words")); // 品牌型号
|
||||
faceResp.setVehicleType(MapUtils.getByExpr(data, "words_result.车辆类型.words"));// 车辆类型
|
||||
faceResp.setOwner(MapUtils.getByExpr(data, "words_result.所有人.words"));
|
||||
faceResp.setEngineNumber(MapUtils.getByExpr(data, "words_result.发动机号码.words"));
|
||||
faceResp.setPlateNumber(MapUtils.getByExpr(data, "words_result.号牌号码.words"));
|
||||
faceResp.setAddress(MapUtils.getByExpr(data, "words_result.住址.words"));
|
||||
faceResp.setUseCharacter(MapUtils.getByExpr(data, "words_result.使用性质.words"));
|
||||
faceResp.setVin(MapUtils.getByExpr(data, "words_result.车辆识别代号.words"));
|
||||
faceResp.setRegisterDate(MapUtils.getByExpr(data, "words_result.发动机号码.words"));
|
||||
return R.ok().setData(faceResp);
|
||||
}
|
||||
|
||||
if (ApiConstants.back.equals(aRecognizeDrivingLicenseRequest.getSide())) {
|
||||
backResp.setOverallDimension(MapUtils.getByExpr(data, "words_result.外廓尺寸.words"));
|
||||
backResp.setInspectionRecord(MapUtils.getByExpr(data, "words_result.检验记录.words"));
|
||||
backResp.setUnladenMass(MapUtils.getByExpr(data, "words_result.整备质量.words"));
|
||||
backResp.setFileNumber(MapUtils.getByExpr(data, "words_result.档案编号.words"));
|
||||
backResp.setTractionMass(MapUtils.getByExpr(data, "words_result.准牵引总质量.words"));
|
||||
backResp.setGrossMass(MapUtils.getByExpr(data, "words_result.总质量.words"));
|
||||
backResp.setPlateNumber(MapUtils.getByExpr(data, "words_result.号牌号码.words"));
|
||||
backResp.setApprovedPassengerCapacity(MapUtils.getByExpr(data, "words_result.核定载人数.words"));
|
||||
backResp.setEnergyType(MapUtils.getByExpr(data, "words_result.燃油类型.words"));
|
||||
backResp.setApprovedLoad(MapUtils.getByExpr(data, "words_result.核定载质量.words"));
|
||||
return R.ok().setData(backResp);
|
||||
}
|
||||
@PostMapping("/recognize")
|
||||
public R recognize(ARecognizeDrivingLicenseRequest request) {
|
||||
if (!hasImage(request)) {
|
||||
return R.error("imageUrl和imageBase64不能同时为空");
|
||||
}
|
||||
if (!isFaceOrBack(request.getSide())) {
|
||||
return R.error("side只能为face或back");
|
||||
}
|
||||
|
||||
ApiR<RecognizeDrivingLicenseResponse> aR = arRecognizeDrivingLicenseHandle.handle(aRecognizeDrivingLicenseRequest);
|
||||
if (aR.isSuccess() && aR.getData() != null && isSuccessStatusCode(aR.getData().statusCode)) {
|
||||
RecognizeDrivingLicenseResponse response = aR.getData();
|
||||
RecognizeDrivingLicenseResponseBody responseBody = response.getBody();
|
||||
RecognizeDrivingLicenseResponseBody.RecognizeDrivingLicenseResponseBodyData responseBodyData = responseBody.getData();
|
||||
|
||||
RecognizeDrivingLicenseResponseBody.RecognizeDrivingLicenseResponseBodyDataFaceResult faceResult = responseBodyData.getFaceResult();
|
||||
RecognizeDrivingLicenseResponseBody.RecognizeDrivingLicenseResponseBodyDataBackResult backResult = responseBodyData.getBackResult();
|
||||
if (ApiConstants.face.equals(aRecognizeDrivingLicenseRequest.getSide())) {
|
||||
faceResp.setIssueDate(faceResult.getIssueDate());
|
||||
faceResp.setModel(faceResult.getModel());
|
||||
faceResp.setVehicleType(faceResult.getVehicleType());
|
||||
faceResp.setOwner(faceResult.getOwner());
|
||||
faceResp.setEngineNumber(faceResult.getEngineNumber());
|
||||
faceResp.setPlateNumber(faceResult.getPlateNumber());
|
||||
faceResp.setAddress(faceResult.getAddress());
|
||||
faceResp.setUseCharacter(faceResult.getUseCharacter());
|
||||
faceResp.setVin(faceResult.getVin());
|
||||
faceResp.setRegisterDate(faceResult.getRegisterDate());
|
||||
return R.ok().setData(faceResp);
|
||||
}
|
||||
if (ApiConstants.back.equals(aRecognizeDrivingLicenseRequest.getSide())) {
|
||||
backResp.setOverallDimension(backResult.getOverallDimension());
|
||||
backResp.setInspectionRecord(backResult.getInspectionRecord());
|
||||
backResp.setUnladenMass(backResult.getUnladenMass());
|
||||
backResp.setFileNumber(backResult.getFileNumber());
|
||||
backResp.setTractionMass(backResult.getTractionMass());
|
||||
backResp.setGrossMass(backResult.getGrossMass());
|
||||
backResp.setPlateNumber(backResult.getPlateNumber());
|
||||
backResp.setApprovedPassengerCapacity(backResult.getApprovedPassengerCapacity());
|
||||
backResp.setEnergyType(backResult.getEnergyType());
|
||||
backResp.setApprovedLoad(backResult.getApprovedLoad());
|
||||
return R.ok().setData(backResp);
|
||||
ApiR<Map> bR = bVehicleLicenseHandle.handle(toBaiduRequest(request));
|
||||
if (bR != null && bR.isSuccess() && bR.getData() != null) {
|
||||
if (ApiConstants.face.equals(request.getSide())) {
|
||||
return R.ok().setData(toFaceResp(bR.getData()));
|
||||
}
|
||||
return R.ok().setData(toBackResp(bR.getData()));
|
||||
}
|
||||
|
||||
return R.ok();
|
||||
ApiR<RecognizeDrivingLicenseResponse> aR = arRecognizeDrivingLicenseHandle.handle(request);
|
||||
if (!isValidAliResponse(aR)) {
|
||||
return R.error(!isBlank(thirdError(aR)) ? thirdError(aR) : thirdError(bR));
|
||||
}
|
||||
|
||||
RecognizeDrivingLicenseResponseBody.RecognizeDrivingLicenseResponseBodyData data =
|
||||
aR.getData().getBody().getData();
|
||||
if (ApiConstants.face.equals(request.getSide())) {
|
||||
if (data.getFaceResult() == null) {
|
||||
return R.error("未识别到行驶证正页信息");
|
||||
}
|
||||
return R.ok().setData(toFaceResp(data.getFaceResult()));
|
||||
}
|
||||
if (data.getBackResult() == null) {
|
||||
return R.error("未识别到行驶证副页信息");
|
||||
}
|
||||
return R.ok().setData(toBackResp(data.getBackResult()));
|
||||
}
|
||||
|
||||
}
|
||||
private BVehicleLicenseRequest toBaiduRequest(ARecognizeDrivingLicenseRequest request) {
|
||||
BVehicleLicenseRequest bVehicleLicenseRequest = new BVehicleLicenseRequest();
|
||||
bVehicleLicenseRequest.setVehicleLicenseSide(request.getSide());
|
||||
bVehicleLicenseRequest.setImageUrl(request.getImageUrl());
|
||||
bVehicleLicenseRequest.setImageBase64(request.getImageBase64());
|
||||
return bVehicleLicenseRequest;
|
||||
}
|
||||
|
||||
private boolean isValidAliResponse(ApiR<RecognizeDrivingLicenseResponse> apiR) {
|
||||
return apiR != null
|
||||
&& apiR.isSuccess()
|
||||
&& apiR.getData() != null
|
||||
&& isSuccessStatusCode(apiR.getData().getStatusCode())
|
||||
&& apiR.getData().getBody() != null
|
||||
&& apiR.getData().getBody().getData() != null;
|
||||
}
|
||||
|
||||
private RecognizeDrivingLicenseFaceResp toFaceResp(Map<String, Object> data) {
|
||||
RecognizeDrivingLicenseFaceResp faceResp = new RecognizeDrivingLicenseFaceResp();
|
||||
faceResp.setIssueDate(MapUtils.getByExpr(data, "words_result.发证日期.words"));
|
||||
faceResp.setModel(MapUtils.getByExpr(data, "words_result.品牌型号.words"));
|
||||
faceResp.setVehicleType(MapUtils.getByExpr(data, "words_result.车辆类型.words"));
|
||||
faceResp.setOwner(MapUtils.getByExpr(data, "words_result.所有人.words"));
|
||||
faceResp.setEngineNumber(MapUtils.getByExpr(data, "words_result.发动机号码.words"));
|
||||
faceResp.setPlateNumber(MapUtils.getByExpr(data, "words_result.号牌号码.words"));
|
||||
faceResp.setAddress(MapUtils.getByExpr(data, "words_result.住址.words"));
|
||||
faceResp.setUseCharacter(MapUtils.getByExpr(data, "words_result.使用性质.words"));
|
||||
faceResp.setVin(MapUtils.getByExpr(data, "words_result.车辆识别代号.words"));
|
||||
faceResp.setRegisterDate(MapUtils.getByExpr(data, "words_result.注册日期.words"));
|
||||
return faceResp;
|
||||
}
|
||||
|
||||
private RecognizeDrivingLicenseBackResp toBackResp(Map<String, Object> data) {
|
||||
RecognizeDrivingLicenseBackResp backResp = new RecognizeDrivingLicenseBackResp();
|
||||
backResp.setOverallDimension(MapUtils.getByExpr(data, "words_result.外廓尺寸.words"));
|
||||
backResp.setInspectionRecord(MapUtils.getByExpr(data, "words_result.检验记录.words"));
|
||||
backResp.setUnladenMass(MapUtils.getByExpr(data, "words_result.整备质量.words"));
|
||||
backResp.setFileNumber(MapUtils.getByExpr(data, "words_result.档案编号.words"));
|
||||
backResp.setTractionMass(MapUtils.getByExpr(data, "words_result.准牵引总质量.words"));
|
||||
backResp.setGrossMass(MapUtils.getByExpr(data, "words_result.总质量.words"));
|
||||
backResp.setPlateNumber(MapUtils.getByExpr(data, "words_result.号牌号码.words"));
|
||||
backResp.setApprovedPassengerCapacity(MapUtils.getByExpr(data, "words_result.核定载人数.words"));
|
||||
backResp.setEnergyType(MapUtils.getByExpr(data, "words_result.燃油类型.words"));
|
||||
backResp.setApprovedLoad(MapUtils.getByExpr(data, "words_result.核定载质量.words"));
|
||||
return backResp;
|
||||
}
|
||||
|
||||
private RecognizeDrivingLicenseFaceResp toFaceResp(
|
||||
RecognizeDrivingLicenseResponseBody.RecognizeDrivingLicenseResponseBodyDataFaceResult faceResult) {
|
||||
RecognizeDrivingLicenseFaceResp faceResp = new RecognizeDrivingLicenseFaceResp();
|
||||
faceResp.setIssueDate(faceResult.getIssueDate());
|
||||
faceResp.setModel(faceResult.getModel());
|
||||
faceResp.setVehicleType(faceResult.getVehicleType());
|
||||
faceResp.setOwner(faceResult.getOwner());
|
||||
faceResp.setEngineNumber(faceResult.getEngineNumber());
|
||||
faceResp.setPlateNumber(faceResult.getPlateNumber());
|
||||
faceResp.setAddress(faceResult.getAddress());
|
||||
faceResp.setUseCharacter(faceResult.getUseCharacter());
|
||||
faceResp.setVin(faceResult.getVin());
|
||||
faceResp.setRegisterDate(faceResult.getRegisterDate());
|
||||
return faceResp;
|
||||
}
|
||||
|
||||
private RecognizeDrivingLicenseBackResp toBackResp(
|
||||
RecognizeDrivingLicenseResponseBody.RecognizeDrivingLicenseResponseBodyDataBackResult backResult) {
|
||||
RecognizeDrivingLicenseBackResp backResp = new RecognizeDrivingLicenseBackResp();
|
||||
backResp.setOverallDimension(backResult.getOverallDimension());
|
||||
backResp.setInspectionRecord(backResult.getInspectionRecord());
|
||||
backResp.setUnladenMass(backResult.getUnladenMass());
|
||||
backResp.setFileNumber(backResult.getFileNumber());
|
||||
backResp.setTractionMass(backResult.getTractionMass());
|
||||
backResp.setGrossMass(backResult.getGrossMass());
|
||||
backResp.setPlateNumber(backResult.getPlateNumber());
|
||||
backResp.setApprovedPassengerCapacity(backResult.getApprovedPassengerCapacity());
|
||||
backResp.setEnergyType(backResult.getEnergyType());
|
||||
backResp.setApprovedLoad(backResult.getApprovedLoad());
|
||||
return backResp;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,18 +1,18 @@
|
||||
package com.heyu.api.controller.car;
|
||||
|
||||
|
||||
import com.aliyun.ocr20191230.models.RecognizeLicensePlateResponse;
|
||||
import com.aliyun.ocr20191230.models.RecognizeLicensePlateResponseBody;
|
||||
import com.heyu.api.alibaba.handle.common.text.ARecognizeLicensePlateHandle;
|
||||
import com.heyu.api.alibaba.request.common.text.ARecognizeLicensePlateRequest;
|
||||
import com.heyu.api.controller.BaseController;
|
||||
import com.heyu.api.resp.car.RecognizeLicensePlateResp;
|
||||
import com.heyu.api.data.annotation.NotIntercept;
|
||||
import com.heyu.api.data.utils.ApiR;
|
||||
import com.heyu.api.data.utils.R;
|
||||
import com.heyu.api.resp.car.RecognizeLicensePlateResp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@ -43,24 +43,39 @@ public class RecognizeLicensePlateController extends BaseController {
|
||||
@Autowired
|
||||
private ARecognizeLicensePlateHandle aRecognizeLicensePlateHandle;
|
||||
|
||||
@RequestMapping("/recognize")
|
||||
@PostMapping("/recognize")
|
||||
public R recognize(ARecognizeLicensePlateRequest request) {
|
||||
RecognizeLicensePlateResp resp = new RecognizeLicensePlateResp();
|
||||
ApiR<RecognizeLicensePlateResponse> aR = aRecognizeLicensePlateHandle.handle(request);
|
||||
if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
|
||||
RecognizeLicensePlateResponse response = aR.getData();
|
||||
RecognizeLicensePlateResponseBody responseBody = response.getBody();
|
||||
RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyData responseBodyData = responseBody.getData();
|
||||
if (CollectionUtils.isNotEmpty(responseBodyData.getPlates())) {
|
||||
RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyDataPlates responseBodyDataPlates = responseBodyData.getPlates().get(0);
|
||||
resp.setConfidence(responseBodyDataPlates.getConfidence());
|
||||
resp.setPlateTypeConfidence(responseBodyDataPlates.getPlateTypeConfidence());
|
||||
resp.setPlateType(responseBodyDataPlates.getPlateType());
|
||||
resp.setPlateNumber(responseBodyDataPlates.getPlateNumber());
|
||||
return R.ok().setData(resp);
|
||||
}
|
||||
if (!hasImage(request)) {
|
||||
return R.error("imageUrl和imageBase64不能同时为空");
|
||||
}
|
||||
return R.ok();
|
||||
|
||||
ApiR<RecognizeLicensePlateResponse> aR = aRecognizeLicensePlateHandle.handle(request);
|
||||
if (!isValidAliResponse(aR)) {
|
||||
return R.error(thirdError(aR));
|
||||
}
|
||||
|
||||
RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyData data =
|
||||
aR.getData().getBody().getData();
|
||||
if (CollectionUtils.isEmpty(data.getPlates())) {
|
||||
return R.error("未识别到车牌信息");
|
||||
}
|
||||
|
||||
RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyDataPlates plate =
|
||||
data.getPlates().get(0);
|
||||
RecognizeLicensePlateResp resp = new RecognizeLicensePlateResp();
|
||||
resp.setConfidence(plate.getConfidence());
|
||||
resp.setPlateTypeConfidence(plate.getPlateTypeConfidence());
|
||||
resp.setPlateType(plate.getPlateType());
|
||||
resp.setPlateNumber(plate.getPlateNumber());
|
||||
return R.ok().setData(resp);
|
||||
}
|
||||
|
||||
private boolean isValidAliResponse(ApiR<RecognizeLicensePlateResponse> apiR) {
|
||||
return apiR != null
|
||||
&& apiR.isSuccess()
|
||||
&& apiR.getData() != null
|
||||
&& isSuccessStatusCode(apiR.getData().getStatusCode())
|
||||
&& apiR.getData().getBody() != null
|
||||
&& apiR.getData().getBody().getData() != null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,59 +1,200 @@
|
||||
package com.heyu.api.controller.car;
|
||||
|
||||
|
||||
import com.aliyun.ocr20191230.models.RecognizeTaxiInvoiceResponse;
|
||||
import com.aliyun.ocr20191230.models.RecognizeTaxiInvoiceResponseBody;
|
||||
import com.heyu.api.alibaba.handle.common.text.ARecognizeTaxiInvoiceHandle;
|
||||
import com.heyu.api.alibaba.request.common.text.ARecognizeTaxiInvoiceRequest;
|
||||
import com.heyu.api.baidu.handle.financial.BTaxiReceiptHandle;
|
||||
import com.heyu.api.baidu.request.financial.BTaxiReceiptRequest;
|
||||
import com.heyu.api.baidu.response.financial.BTaxiReceiptResp;
|
||||
import com.heyu.api.controller.BaseController;
|
||||
import com.heyu.api.data.annotation.NotIntercept;
|
||||
import com.heyu.api.data.utils.ApiR;
|
||||
import com.heyu.api.data.utils.R;
|
||||
import com.heyu.api.resp.car.RecognizeTaxiInvoiceResp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/***
|
||||
* https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeTaxiInvoice?tab=DEMO&lang=JAVA
|
||||
*
|
||||
*出租车发票识别 RecognizeTaxiInvoice
|
||||
*出租车发票识别能力可以准确识别出全国各大城市出租车发票在图像中的位置,支持出租车发票结构化识别,输出发票号码、代码、车号、日期、时间、金额,共 6 个关键字段信息。
|
||||
*
|
||||
*
|
||||
*
|
||||
* https://console.bce.baidu.com/support/?_=1740575657628×tamp=1740577722254#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E8%B4%A2%E5%8A%A1%E7%A5%A8%E6%8D%AEOCR&api=rest%2F2.0%2Focr%2Fv1%2Ftaxi_receipt&method=post
|
||||
*
|
||||
*/
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/taxi/invoice")
|
||||
@NotIntercept
|
||||
public class RecognizeTaxiInvoiceController extends BaseController {
|
||||
|
||||
|
||||
@Autowired
|
||||
private ARecognizeTaxiInvoiceHandle arRecognizeTaxiInvoiceHandle;
|
||||
|
||||
@Autowired
|
||||
private BTaxiReceiptHandle bTaxiReceiptHandle;
|
||||
|
||||
|
||||
@RequestMapping("/recognize")
|
||||
@PostMapping("/recognize")
|
||||
public R recognize(ARecognizeTaxiInvoiceRequest request) {
|
||||
ApiR<RecognizeTaxiInvoiceResponse> aR = arRecognizeTaxiInvoiceHandle.handle(request);
|
||||
|
||||
if(aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode()) ){
|
||||
RecognizeTaxiInvoiceResponse recognizeTaxiInvoiceResponse = aR.getData();
|
||||
|
||||
RecognizeTaxiInvoiceResponseBody responseBody = recognizeTaxiInvoiceResponse.getBody();
|
||||
RecognizeTaxiInvoiceResponseBody.RecognizeTaxiInvoiceResponseBodyData responseBodyData = responseBody.getData();
|
||||
|
||||
|
||||
if (!hasImage(request)) {
|
||||
return R.error("imageUrl和imageBase64不能同时为空");
|
||||
}
|
||||
|
||||
return R.error();
|
||||
ApiR<RecognizeTaxiInvoiceResponse> aR = arRecognizeTaxiInvoiceHandle.handle(request);
|
||||
if (isValidAliResponse(aR)) {
|
||||
RecognizeTaxiInvoiceResp resp = toResp(aR.getData().getBody().getData());
|
||||
if (CollectionUtils.isNotEmpty(resp.getRawItems())) {
|
||||
return R.ok().setData(resp);
|
||||
}
|
||||
}
|
||||
|
||||
ApiR<BTaxiReceiptResp> bR = bTaxiReceiptHandle.handle(toBaiduRequest(request));
|
||||
if (bR != null && bR.isSuccess() && bR.getData() != null && bR.getData().getWordsResult() != null) {
|
||||
return R.ok().setData(toResp(bR.getData().getWordsResult()));
|
||||
}
|
||||
|
||||
return R.error(!isBlank(thirdError(aR)) ? thirdError(aR) : thirdError(bR));
|
||||
}
|
||||
|
||||
private boolean isValidAliResponse(ApiR<RecognizeTaxiInvoiceResponse> apiR) {
|
||||
return apiR != null
|
||||
&& apiR.isSuccess()
|
||||
&& apiR.getData() != null
|
||||
&& isSuccessStatusCode(apiR.getData().getStatusCode())
|
||||
&& apiR.getData().getBody() != null
|
||||
&& apiR.getData().getBody().getData() != null;
|
||||
}
|
||||
|
||||
private BTaxiReceiptRequest toBaiduRequest(ARecognizeTaxiInvoiceRequest request) {
|
||||
BTaxiReceiptRequest bTaxiReceiptRequest = new BTaxiReceiptRequest();
|
||||
bTaxiReceiptRequest.setImageUrl(request.getImageUrl());
|
||||
bTaxiReceiptRequest.setImageBase64(request.getImageBase64());
|
||||
return bTaxiReceiptRequest;
|
||||
}
|
||||
|
||||
private RecognizeTaxiInvoiceResp toResp(
|
||||
RecognizeTaxiInvoiceResponseBody.RecognizeTaxiInvoiceResponseBodyData data) {
|
||||
RecognizeTaxiInvoiceResp resp = new RecognizeTaxiInvoiceResp();
|
||||
List<String> rawItems = new ArrayList<>();
|
||||
if (data == null || CollectionUtils.isEmpty(data.getInvoices())) {
|
||||
resp.setRawItems(rawItems);
|
||||
return resp;
|
||||
}
|
||||
|
||||
RecognizeTaxiInvoiceResponseBody.RecognizeTaxiInvoiceResponseBodyDataInvoices invoice =
|
||||
data.getInvoices().get(0);
|
||||
if (invoice == null || CollectionUtils.isEmpty(invoice.getItems())) {
|
||||
resp.setRawItems(rawItems);
|
||||
return resp;
|
||||
}
|
||||
|
||||
for (RecognizeTaxiInvoiceResponseBody.RecognizeTaxiInvoiceResponseBodyDataInvoicesItems item
|
||||
: invoice.getItems()) {
|
||||
if (item == null || isBlank(item.getText())) {
|
||||
continue;
|
||||
}
|
||||
String text = item.getText().trim();
|
||||
rawItems.add(text);
|
||||
applyAliItem(resp, text);
|
||||
}
|
||||
resp.setRawItems(rawItems);
|
||||
return resp;
|
||||
}
|
||||
|
||||
private void applyAliItem(RecognizeTaxiInvoiceResp resp, String text) {
|
||||
String value = parseValue(text);
|
||||
if (text.contains("发票代码")) {
|
||||
setInvoiceCode(resp, value);
|
||||
return;
|
||||
}
|
||||
if (text.contains("发票号码") || text.contains("发票号")) {
|
||||
setInvoiceNum(resp, value);
|
||||
return;
|
||||
}
|
||||
if (text.contains("车号") || text.contains("车牌")) {
|
||||
setTaxiNum(resp, value);
|
||||
return;
|
||||
}
|
||||
if (text.contains("日期")) {
|
||||
setDate(resp, value);
|
||||
return;
|
||||
}
|
||||
if (text.contains("上车")) {
|
||||
resp.setPickupTime(value);
|
||||
return;
|
||||
}
|
||||
if (text.contains("下车")) {
|
||||
resp.setDropoffTime(value);
|
||||
return;
|
||||
}
|
||||
if (text.contains("时间")) {
|
||||
resp.setTime(value);
|
||||
return;
|
||||
}
|
||||
if (text.contains("金额") || text.contains("合计") || text.contains("票价")) {
|
||||
setFare(resp, value);
|
||||
}
|
||||
}
|
||||
|
||||
private String parseValue(String text) {
|
||||
int colonIndex = Math.max(text.lastIndexOf(':'), text.lastIndexOf(':'));
|
||||
if (colonIndex >= 0 && colonIndex + 1 < text.length()) {
|
||||
return text.substring(colonIndex + 1).trim();
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
private RecognizeTaxiInvoiceResp toResp(BTaxiReceiptResp.WordsResultDTO wordsResult) {
|
||||
RecognizeTaxiInvoiceResp resp = new RecognizeTaxiInvoiceResp();
|
||||
resp.setDate(wordsResult.getDate());
|
||||
resp.setFare(wordsResult.getFare());
|
||||
resp.setLocation(wordsResult.getLocation());
|
||||
resp.setInvoiceCode(wordsResult.getInvoiceCode());
|
||||
resp.setInvoiceNum(wordsResult.getInvoiceNum());
|
||||
resp.setTaxiNum(wordsResult.getTaxiNum());
|
||||
resp.setTime(wordsResult.getTime());
|
||||
resp.setPickupTime(wordsResult.getPickupTime());
|
||||
resp.setDropoffTime(wordsResult.getDropoffTime());
|
||||
resp.setFuelOilSurcharge(wordsResult.getFuelOilSurcharge());
|
||||
resp.setCallServiceSurcharge(wordsResult.getCallServiceSurcharge());
|
||||
resp.setTotalFare(wordsResult.getTotalFare());
|
||||
resp.setProvince(wordsResult.getProvince());
|
||||
resp.setCity(wordsResult.getCity());
|
||||
resp.setPricePerkm(wordsResult.getPricePerkm());
|
||||
resp.setDistance(wordsResult.getDistance());
|
||||
return resp;
|
||||
}
|
||||
|
||||
private void setInvoiceCode(RecognizeTaxiInvoiceResp resp, String value) {
|
||||
if (isBlank(resp.getInvoiceCode())) {
|
||||
resp.setInvoiceCode(value);
|
||||
}
|
||||
}
|
||||
|
||||
private void setInvoiceNum(RecognizeTaxiInvoiceResp resp, String value) {
|
||||
if (isBlank(resp.getInvoiceNum())) {
|
||||
resp.setInvoiceNum(value);
|
||||
}
|
||||
}
|
||||
|
||||
private void setTaxiNum(RecognizeTaxiInvoiceResp resp, String value) {
|
||||
if (isBlank(resp.getTaxiNum())) {
|
||||
resp.setTaxiNum(value);
|
||||
}
|
||||
}
|
||||
|
||||
private void setDate(RecognizeTaxiInvoiceResp resp, String value) {
|
||||
if (isBlank(resp.getDate())) {
|
||||
resp.setDate(value);
|
||||
}
|
||||
}
|
||||
|
||||
private void setFare(RecognizeTaxiInvoiceResp resp, String value) {
|
||||
if (isBlank(resp.getFare())) {
|
||||
resp.setFare(value);
|
||||
}
|
||||
if (isBlank(resp.getTotalFare())) {
|
||||
resp.setTotalFare(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,12 +5,13 @@ import com.aliyun.ocr20191230.models.RecognizeTrainTicketResponseBody;
|
||||
import com.heyu.api.alibaba.handle.common.text.ARecognizeTrainTicketHandle;
|
||||
import com.heyu.api.alibaba.request.common.text.ARecognizeTrainTicketRequest;
|
||||
import com.heyu.api.controller.BaseController;
|
||||
import com.heyu.api.resp.car.RecognizeTrainTicketResp;
|
||||
import com.heyu.api.data.annotation.NotIntercept;
|
||||
import com.heyu.api.data.utils.ApiR;
|
||||
import com.heyu.api.data.utils.R;
|
||||
import com.heyu.api.resp.car.RecognizeTrainTicketResp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@ -28,31 +29,43 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@NotIntercept
|
||||
public class RecognizeTrainTicketController extends BaseController {
|
||||
|
||||
|
||||
@Autowired
|
||||
private ARecognizeTrainTicketHandle aRecognizeTrainTicketHandle;
|
||||
|
||||
|
||||
@RequestMapping("/recognize")
|
||||
@PostMapping("/recognize")
|
||||
public R recognize(ARecognizeTrainTicketRequest request) {
|
||||
ApiR<RecognizeTrainTicketResponse> aR = aRecognizeTrainTicketHandle.handle(request);
|
||||
if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
|
||||
RecognizeTrainTicketResponse recognizeTrainTicketResponse = aR.getData();
|
||||
|
||||
RecognizeTrainTicketResponseBody responseBody = recognizeTrainTicketResponse.getBody();
|
||||
RecognizeTrainTicketResponseBody.RecognizeTrainTicketResponseBodyData responseBodyData = responseBody.getData();
|
||||
|
||||
RecognizeTrainTicketResp resp = new RecognizeTrainTicketResp();
|
||||
resp.setPrice(responseBodyData.getPrice());
|
||||
resp.setDestination(responseBodyData.getDestination());
|
||||
resp.setDepartureStation(responseBodyData.getDepartureStation());
|
||||
resp.setDate(responseBodyData.getDate());
|
||||
resp.setNumber(responseBodyData.getNumber());
|
||||
resp.setSeat(responseBodyData.getSeat());
|
||||
resp.setName(responseBodyData.getName());
|
||||
resp.setLevel(responseBodyData.getLevel());
|
||||
return R.ok().setData(resp);
|
||||
if (!hasImage(request)) {
|
||||
return R.error("imageUrl和imageBase64不能同时为空");
|
||||
}
|
||||
return R.error();
|
||||
|
||||
ApiR<RecognizeTrainTicketResponse> aR = aRecognizeTrainTicketHandle.handle(request);
|
||||
if (!isValidAliResponse(aR)) {
|
||||
return R.error(thirdError(aR));
|
||||
}
|
||||
|
||||
return R.ok().setData(toResp(aR.getData().getBody().getData()));
|
||||
}
|
||||
|
||||
private boolean isValidAliResponse(ApiR<RecognizeTrainTicketResponse> apiR) {
|
||||
return apiR != null
|
||||
&& apiR.isSuccess()
|
||||
&& apiR.getData() != null
|
||||
&& isSuccessStatusCode(apiR.getData().getStatusCode())
|
||||
&& apiR.getData().getBody() != null
|
||||
&& apiR.getData().getBody().getData() != null;
|
||||
}
|
||||
|
||||
private RecognizeTrainTicketResp toResp(
|
||||
RecognizeTrainTicketResponseBody.RecognizeTrainTicketResponseBodyData data) {
|
||||
RecognizeTrainTicketResp resp = new RecognizeTrainTicketResp();
|
||||
resp.setPrice(data.getPrice());
|
||||
resp.setDestination(data.getDestination());
|
||||
resp.setDepartureStation(data.getDepartureStation());
|
||||
resp.setDate(data.getDate());
|
||||
resp.setNumber(data.getNumber());
|
||||
resp.setSeat(data.getSeat());
|
||||
resp.setName(data.getName());
|
||||
resp.setLevel(data.getLevel());
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ public class RecognizeLicensePlateResp {
|
||||
* 示例值:
|
||||
* 0.99745339155197144
|
||||
*/
|
||||
private float confidence;
|
||||
private Float confidence;
|
||||
|
||||
|
||||
/***
|
||||
|
||||
@ -1,4 +1,28 @@
|
||||
package com.heyu.api.resp.car;
|
||||
|
||||
public class RecognizeTaxiInvoiceResp {
|
||||
import com.heyu.api.data.dto.BaseResp;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class RecognizeTaxiInvoiceResp extends BaseResp {
|
||||
|
||||
private String date;
|
||||
private String fare;
|
||||
private String location;
|
||||
private String invoiceCode;
|
||||
private String invoiceNum;
|
||||
private String taxiNum;
|
||||
private String time;
|
||||
private String pickupTime;
|
||||
private String dropoffTime;
|
||||
private String fuelOilSurcharge;
|
||||
private String callServiceSurcharge;
|
||||
private String totalFare;
|
||||
private String province;
|
||||
private String city;
|
||||
private String pricePerkm;
|
||||
private String distance;
|
||||
private List<String> rawItems;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user