Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite0c914b

Browse files
authored
Merge pull request#289 from Sean-LL/dev_residence_permit
feat: 港澳台居民居住证 Yidao Adaptor
2 parentsce01782 +f7e701e commite0c914b

File tree

6 files changed

+275
-15
lines changed

6 files changed

+275
-15
lines changed

‎api/server/src/main/java/com/ke/bella/openapi/endpoints/OcrController.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
importcom.ke.bella.openapi.protocol.ocr.OcrIdcardAdaptor;
1919
importcom.ke.bella.openapi.protocol.ocr.OcrProperty;
2020
importcom.ke.bella.openapi.protocol.ocr.OcrRequest;
21+
importcom.ke.bella.openapi.protocol.ocr.residence_permit.ResidencePermitAdaptor;
2122
importcom.ke.bella.openapi.service.EndpointDataService;
2223
importcom.ke.bella.openapi.tables.pojos.ChannelDB;
2324
importcom.ke.bella.openapi.utils.JacksonUtils;
@@ -106,6 +107,39 @@ public Object bankcard(@RequestBody OcrRequest request) {
106107
returnadaptor.bankcard(request,url,property);
107108
}
108109

110+
@SuppressWarnings({"rawtypes","unchecked" })
111+
@PostMapping("/hmt-residence-permit")
112+
publicObjecthmtResidencePermit(@RequestBodyOcrRequestrequest) {
113+
// 1. 设置请求上下文
114+
Stringendpoint =EndpointContext.getRequest().getRequestURI();
115+
Stringmodel =request.getModel();
116+
endpointDataService.setEndpointData(endpoint,model,request);
117+
EndpointProcessDataprocessData =EndpointContext.getProcessData();
118+
119+
// 2. 参数校验
120+
validateRequest(request);
121+
122+
// 3. 渠道路由选择
123+
ChannelDBchannel =router.route(endpoint,model,EndpointContext.getApikey(),processData.isMock());
124+
endpointDataService.setChannel(channel);
125+
126+
// 4. 并发限制管理
127+
if(!EndpointContext.getProcessData().isPrivate()) {
128+
limiterManager.incrementConcurrentCount(EndpointContext.getProcessData().getAkCode(),model);
129+
}
130+
131+
// 5. 获取协议适配器
132+
Stringprotocol =processData.getProtocol();
133+
Stringurl =processData.getForwardUrl();
134+
StringchannelInfo =channel.getChannelInfo();
135+
ResidencePermitAdaptoradaptor =adaptorManager.getProtocolAdaptor(endpoint,protocol,ResidencePermitAdaptor.class);
136+
OcrPropertyproperty = (OcrProperty)JacksonUtils.deserialize(channelInfo,adaptor.getPropertyClass());
137+
EndpointContext.setEncodingType(property.getEncodingType());
138+
139+
// 6. 调用适配器处理
140+
returnadaptor.hmtResidencePermit(request,url,property);
141+
}
142+
109143
privatevoidvalidateRequest(OcrRequestrequest) {
110144
// 校验模型参数
111145
Assert.hasText(request.getModel(),"model参数不能为空");

‎api/server/src/main/java/com/ke/bella/openapi/protocol/cost/CostCalculator.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
importcom.ke.bella.openapi.protocol.images.ImagesEditsPriceInfo;
2020
importcom.ke.bella.openapi.protocol.images.ImagesPriceInfo;
2121
importcom.ke.bella.openapi.protocol.images.ImagesResponse;
22+
importcom.ke.bella.openapi.protocol.ocr.OcrPriceInfo;
2223
importcom.ke.bella.openapi.protocol.realtime.RealTimePriceInfo;
2324
importcom.ke.bella.openapi.protocol.speaker.SpeakerEmbeddingLogHandler;
2425
importcom.ke.bella.openapi.protocol.speaker.SpeakerEmbeddingPriceInfo;
@@ -77,7 +78,8 @@ enum CostCalculators {
7778
IMAGES_VARIATIONS("/v*/images/variations",images),
7879
WEB_SEARCH("/v*/web/search",webSearch),
7980
WEB_CRAWL("/v*/web/crawl",webCrawl),
80-
WEB_EXTRACT("/v*/web/extract",webExtract)
81+
WEB_EXTRACT("/v*/web/extract",webExtract),
82+
OCR("/v*/ocr/*",ocr),
8183
;
8284
finalStringendpoint;
8385
finalEndpointCostCalculatorcalculator;
@@ -434,6 +436,20 @@ public boolean checkPriceInfo(String priceInfo) {
434436
price.getAdvancedExtractionPrice() !=null;
435437
}
436438
};
439+
staticEndpointCostCalculatorocr =newEndpointCostCalculator() {
440+
@Override
441+
publicBigDecimalcalculate(StringpriceInfo,Objectusage) {
442+
OcrPriceInfoprice =JacksonUtils.deserialize(priceInfo,OcrPriceInfo.class);
443+
inttimes =Integer.parseInt(usage.toString());
444+
returnprice.getPricePerRequest().multiply(BigDecimal.valueOf(times));
445+
}
446+
447+
@Override
448+
publicbooleancheckPriceInfo(StringpriceInfo) {
449+
OcrPriceInfoprice =JacksonUtils.deserialize(priceInfo,OcrPriceInfo.class);
450+
returnprice !=null &&price.getPricePerRequest() !=null;
451+
}
452+
};
437453

438454
interfaceEndpointCostCalculator {
439455
BigDecimalcalculate(StringpriceInfo,Objectusage);

‎api/server/src/main/java/com/ke/bella/openapi/protocol/ocr/OcrResidencePermitAdaptor.java‎

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
packagecom.ke.bella.openapi.protocol.ocr.residence_permit;
2+
3+
importcom.ke.bella.openapi.protocol.IProtocolAdaptor;
4+
importcom.ke.bella.openapi.protocol.ocr.OcrProperty;
5+
importcom.ke.bella.openapi.protocol.ocr.OcrRequest;
6+
importcom.ke.bella.openapi.protocol.ocr.residencepermit.OcrResidencePermitResponse;
7+
8+
publicinterfaceResidencePermitAdaptor<TextendsOcrProperty>extendsIProtocolAdaptor {
9+
10+
OcrResidencePermitResponsehmtResidencePermit(OcrRequestrequest,Stringurl,Tproperty);
11+
12+
@Override
13+
defaultStringendpoint() {
14+
return"/v1/ocr/hmt-residence-permit";
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
packagecom.ke.bella.openapi.protocol.ocr.residence_permit;
2+
3+
importorg.springframework.stereotype.Component;
4+
5+
importcom.ke.bella.openapi.protocol.ocr.OcrLogHandler;
6+
7+
/**
8+
* OCR港澳台居民居住证日志处理器
9+
*/
10+
@Component
11+
publicclassResidencePermitLogHandlerextendsOcrLogHandler {
12+
13+
@Override
14+
publicStringendpoint() {
15+
return"/v1/ocr/hmt-residence-permit";
16+
}
17+
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
packagecom.ke.bella.openapi.protocol.ocr.residence_permit;
2+
3+
importorg.springframework.beans.factory.annotation.Autowired;
4+
importorg.springframework.http.HttpStatus;
5+
importorg.springframework.stereotype.Component;
6+
importorg.springframework.util.StringUtils;
7+
8+
importcom.ke.bella.openapi.protocol.OpenapiResponse;
9+
importcom.ke.bella.openapi.protocol.ocr.ImageRetrievalService;
10+
importcom.ke.bella.openapi.protocol.ocr.OcrRequest;
11+
importcom.ke.bella.openapi.protocol.ocr.YidaoOcrProperty;
12+
importcom.ke.bella.openapi.protocol.ocr.YidaoRequest;
13+
importcom.ke.bella.openapi.protocol.ocr.residencepermit.OcrResidencePermitResponse;
14+
importcom.ke.bella.openapi.protocol.ocr.util.ImageConverter;
15+
importcom.ke.bella.openapi.utils.HttpUtils;
16+
17+
importlombok.extern.slf4j.Slf4j;
18+
importokhttp3.FormBody;
19+
importokhttp3.MediaType;
20+
importokhttp3.MultipartBody;
21+
importokhttp3.Request;
22+
importokhttp3.RequestBody;
23+
24+
@Slf4j
25+
@Component("yidaoResidencePermit")
26+
publicclassYidaoAdaptorimplementsResidencePermitAdaptor<YidaoOcrProperty> {
27+
28+
privatestaticfinalStringYIDAO_OCR_ERROR_TYPE ="YIDAO_OCR_ERROR";
29+
privatestaticfinalintSUCCESS_CODE =0;
30+
privatestaticfinalStringVALID_DATE_SEPARATOR ="-";// U+002D,Hyphen-Minus
31+
32+
@Autowired
33+
privateImageRetrievalServiceimageRetrievalService;
34+
35+
@Override
36+
publicStringgetDescription() {
37+
return"易道OCR港澳台居民居住证识别协议";
38+
}
39+
40+
@Override
41+
publicClass<YidaoOcrProperty>getPropertyClass() {
42+
returnYidaoOcrProperty.class;
43+
}
44+
45+
@Override
46+
publicOcrResidencePermitResponsehmtResidencePermit(OcrRequestrequest,Stringurl,YidaoOcrPropertyproperty) {
47+
YidaoRequestyidaoRequest =requestConvert(request,property);
48+
RequesthttpRequest =buildRequest(yidaoRequest,url);
49+
clearLargeData(request,yidaoRequest);
50+
YidaoResponseyidaoResponse =HttpUtils.httpRequest(httpRequest,YidaoResponse.class);
51+
returnresponseConvert(yidaoResponse);
52+
}
53+
54+
privateYidaoRequestrequestConvert(OcrRequestrequest,YidaoOcrPropertyproperty) {
55+
try {
56+
YidaoRequest.YidaoRequestBuilderbuilder =YidaoRequest.builder()
57+
.appKey(property.getAuth().getApiKey())
58+
.appSecret(property.getAuth().getSecret());
59+
60+
if(StringUtils.hasText(request.getImageBase64())) {
61+
StringimageBase64 =ImageConverter.cleanBase64DataHeader(request.getImageBase64());
62+
builder.imageBase64(imageBase64);
63+
}elseif(StringUtils.hasText(request.getImageUrl())) {
64+
builder.imageUrl(request.getImageUrl());
65+
}else {
66+
byte[]imageData =imageRetrievalService.getImageFromFileId(request.getFileId());
67+
builder.imageBinary(imageData);
68+
}
69+
70+
returnbuilder.build();
71+
}catch (Exceptione) {
72+
log.error("Failed to retrieve image data",e);
73+
thrownewIllegalStateException("Failed to retrieve image data",e);
74+
}
75+
}
76+
77+
privateRequestbuildRequest(YidaoRequestrequest,Stringurl) {
78+
if(request.getImageBinary() !=null) {
79+
MultipartBody.BuilderbodyBuilder =newMultipartBody.Builder()
80+
.setType(MultipartBody.FORM)
81+
.addFormDataPart("app_key",request.getAppKey())
82+
.addFormDataPart("app_secret",request.getAppSecret())
83+
.addFormDataPart("image_binary","image.jpg",
84+
RequestBody.create(MediaType.parse("image/jpeg"),request.getImageBinary()));
85+
86+
returnnewRequest.Builder()
87+
.url(url)
88+
.post(bodyBuilder.build())
89+
.build();
90+
}else {
91+
FormBody.BuilderbodyBuilder =newFormBody.Builder()
92+
.add("app_key",request.getAppKey())
93+
.add("app_secret",request.getAppSecret());
94+
95+
if(StringUtils.hasText(request.getImageBase64())) {
96+
bodyBuilder.add("image_base64",request.getImageBase64());
97+
}elseif(StringUtils.hasText(request.getImageUrl())) {
98+
bodyBuilder.add("image_url",request.getImageUrl());
99+
}
100+
101+
returnnewRequest.Builder()
102+
.url(url)
103+
.post(bodyBuilder.build())
104+
.build();
105+
}
106+
}
107+
108+
privateOcrResidencePermitResponseresponseConvert(YidaoResponseyidaoResponse) {
109+
if(yidaoResponse.getErrorCode() ==null ||yidaoResponse.getErrorCode() !=SUCCESS_CODE) {
110+
returnbuildErrorResponse(yidaoResponse);
111+
}
112+
113+
YidaoResponse.ResultDataresult =yidaoResponse.getResult();
114+
if(result ==null) {
115+
returnbuildErrorResponse(yidaoResponse);
116+
}
117+
118+
OcrResidencePermitResponse.ResidencePermitSideside =determineSide(result);
119+
Objectdata =side ==OcrResidencePermitResponse.ResidencePermitSide.PORTRAIT
120+
?extractPortraitData(result)
121+
:extractNationalEmblemData(result);
122+
123+
returnOcrResidencePermitResponse.builder()
124+
.request_id(yidaoResponse.getRequestId())
125+
.side(side)
126+
.data(data)
127+
.build();
128+
}
129+
130+
privateOcrResidencePermitResponse.ResidencePermitSidedetermineSide(YidaoResponse.ResultDataresult) {
131+
if(result.getName() !=null ||result.getGender() !=null ||result.getBirthdate() !=null) {
132+
returnOcrResidencePermitResponse.ResidencePermitSide.PORTRAIT;
133+
}
134+
returnOcrResidencePermitResponse.ResidencePermitSide.NATIONAL_EMBLEM;
135+
}
136+
137+
privateOcrResidencePermitResponse.PortraitDataextractPortraitData(YidaoResponse.ResultDataresult) {
138+
returnOcrResidencePermitResponse.PortraitData.builder()
139+
.name(getFieldWords(result.getName()))
140+
.sex(getFieldWords(result.getGender()))
141+
.birth_date(getFieldWords(result.getBirthdate()))
142+
.address(getFieldWords(result.getAddress()))
143+
.idcard_number(getFieldWords(result.getIdno()))
144+
.build();
145+
}
146+
147+
privateOcrResidencePermitResponse.NationalEmblemDataextractNationalEmblemData(YidaoResponse.ResultDataresult) {
148+
StringvalidDate =getFieldWords(result.getValid());
149+
String[]dates =parseValidDate(validDate);
150+
151+
returnOcrResidencePermitResponse.NationalEmblemData.builder()
152+
.issue_authority(getFieldWords(result.getIssued()))
153+
.valid_date_start(dates[0])
154+
.valid_date_end(dates[1])
155+
.issue_times(getFieldWords(result.getIssuedTimes()))
156+
.eep_number(getFieldWords(result.getPassNo()))
157+
.build();
158+
}
159+
160+
privateStringgetFieldWords(YidaoResponse.FieldDatafieldData) {
161+
returnfieldData !=null ?fieldData.getWords() :null;
162+
}
163+
164+
privateString[]parseValidDate(StringvalidDate) {
165+
if(validDate ==null) {
166+
returnnewString[] {null,null };
167+
}
168+
169+
String[]parts =validDate.split(VALID_DATE_SEPARATOR);
170+
if(parts.length ==2) {
171+
returnnewString[] {parts[0].trim(),parts[1].trim() };
172+
}
173+
174+
returnnewString[] {validDate,null };
175+
}
176+
177+
privateOcrResidencePermitResponsebuildErrorResponse(YidaoResponseyidaoResponse) {
178+
StringerrorMessage =yidaoResponse.getDescription() !=null
179+
?yidaoResponse.getDescription()
180+
:"Unknown error";
181+
182+
OpenapiResponse.OpenapiErrorerror =OpenapiResponse.OpenapiError.builder()
183+
.code(String.valueOf(yidaoResponse.getErrorCode()))
184+
.message(errorMessage)
185+
.type(YIDAO_OCR_ERROR_TYPE)
186+
.httpCode(HttpStatus.BAD_REQUEST.value())
187+
.build();
188+
189+
returnOcrResidencePermitResponse.builder().error(error).build();
190+
}
191+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp