1.完成car下面的所有接口,使用的百度云接口
This commit is contained in:
parent
001fe17de3
commit
7d8d4d293d
@ -0,0 +1,34 @@
|
|||||||
|
package com.heyu.api.baidu.handle.financial;
|
||||||
|
|
||||||
|
import com.heyu.api.data.annotation.CustomPath;
|
||||||
|
import com.heyu.api.baidu.BaiduBaseHandle;
|
||||||
|
import com.heyu.api.baidu.request.financial.BTrainTicketRequest;
|
||||||
|
import com.heyu.api.baidu.response.financial.BTrainTicketResp;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://console.bce.baidu.com/support/#/api?product=AI&project=文字识别&parent=财务票据OCR&api=rest%2F2.0%2Focr%2Fv1%2Ftrain_ticket&method=post
|
||||||
|
*
|
||||||
|
* 火车票识别
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
@CustomPath("trainTicket")
|
||||||
|
public class BTrainTicketHandle extends BaiduBaseHandle<BTrainTicketRequest, BTrainTicketResp> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUri() {
|
||||||
|
return "/rest/2.0/ocr/v1/train_ticket";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String check(BTrainTicketRequest request) {
|
||||||
|
return checkImageUri(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getContent(BTrainTicketRequest request) {
|
||||||
|
return getImageContent(request).toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package com.heyu.api.baidu.request.financial;
|
||||||
|
|
||||||
|
import com.heyu.api.baidu.request.BaiduImageUrlRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://console.bce.baidu.com/support/#/api?product=AI&project=文字识别&parent=财务票据OCR&api=rest%2F2.0%2Focr%2Fv1%2Ftrain_ticket&method=post
|
||||||
|
*
|
||||||
|
* 火车票识别
|
||||||
|
*/
|
||||||
|
public class BTrainTicketRequest extends BaiduImageUrlRequest {
|
||||||
|
|
||||||
|
}
|
||||||
@ -35,7 +35,7 @@ public class ANumbersResp extends BBaseResp {
|
|||||||
|
|
||||||
|
|
||||||
@JsonProperty("log_id")
|
@JsonProperty("log_id")
|
||||||
private Integer logId;
|
private Long logId;
|
||||||
@JsonProperty("words_result")
|
@JsonProperty("words_result")
|
||||||
private List<WordsResultDTO> wordsResult;
|
private List<WordsResultDTO> wordsResult;
|
||||||
@JsonProperty("words_result_num")
|
@JsonProperty("words_result_num")
|
||||||
|
|||||||
@ -18,7 +18,7 @@ public class BDocConvertResp extends BBaseResp {
|
|||||||
@JsonProperty("success")
|
@JsonProperty("success")
|
||||||
private Boolean success;
|
private Boolean success;
|
||||||
@JsonProperty("log_id")
|
@JsonProperty("log_id")
|
||||||
private Integer logId;
|
private Long logId;
|
||||||
@JsonProperty("result")
|
@JsonProperty("result")
|
||||||
private ResultDTO result;
|
private ResultDTO result;
|
||||||
@JsonProperty("code")
|
@JsonProperty("code")
|
||||||
|
|||||||
@ -18,7 +18,7 @@ public class BFerryTicketResp {
|
|||||||
|
|
||||||
|
|
||||||
@JsonProperty("log_id")
|
@JsonProperty("log_id")
|
||||||
private Integer logId;
|
private Long logId;
|
||||||
@JsonProperty("words_result_num")
|
@JsonProperty("words_result_num")
|
||||||
private Integer wordsResultNum;
|
private Integer wordsResultNum;
|
||||||
@JsonProperty("words_result")
|
@JsonProperty("words_result")
|
||||||
|
|||||||
@ -17,7 +17,7 @@ public class BTaxiReceiptResp extends BBaseResp {
|
|||||||
|
|
||||||
|
|
||||||
@JsonProperty("log_id")
|
@JsonProperty("log_id")
|
||||||
private Integer logId;
|
private Long logId;
|
||||||
@JsonProperty("words_result_num")
|
@JsonProperty("words_result_num")
|
||||||
private Integer wordsResultNum;
|
private Integer wordsResultNum;
|
||||||
@JsonProperty("words_result")
|
@JsonProperty("words_result")
|
||||||
|
|||||||
@ -19,7 +19,7 @@ public class BVinCodeResp extends BBaseResp {
|
|||||||
|
|
||||||
|
|
||||||
@JsonProperty("log_id")
|
@JsonProperty("log_id")
|
||||||
private Integer logId;
|
private Long logId;
|
||||||
@JsonProperty("words_result")
|
@JsonProperty("words_result")
|
||||||
private List<WordsResultDTO> wordsResult;
|
private List<WordsResultDTO> wordsResult;
|
||||||
@JsonProperty("words_result_num")
|
@JsonProperty("words_result_num")
|
||||||
|
|||||||
@ -1,9 +1,5 @@
|
|||||||
package com.heyu.api.controller.car;
|
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.handle.financial.BTaxiReceiptHandle;
|
||||||
import com.heyu.api.baidu.request.financial.BTaxiReceiptRequest;
|
import com.heyu.api.baidu.request.financial.BTaxiReceiptRequest;
|
||||||
import com.heyu.api.baidu.response.financial.BTaxiReceiptResp;
|
import com.heyu.api.baidu.response.financial.BTaxiReceiptResp;
|
||||||
@ -11,137 +7,69 @@ import com.heyu.api.controller.BaseController;
|
|||||||
import com.heyu.api.data.annotation.NotIntercept;
|
import com.heyu.api.data.annotation.NotIntercept;
|
||||||
import com.heyu.api.data.utils.ApiR;
|
import com.heyu.api.data.utils.ApiR;
|
||||||
import com.heyu.api.data.utils.R;
|
import com.heyu.api.data.utils.R;
|
||||||
|
import com.heyu.api.request.car.TaxiInvoiceRecognizeRequest;
|
||||||
import com.heyu.api.resp.car.RecognizeTaxiInvoiceResp;
|
import com.heyu.api.resp.car.RecognizeTaxiInvoiceResp;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.collections.CollectionUtils;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.List;
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://console.bce.baidu.com/support/#/api?product=AI&project=文字识别&parent=财务票据OCR&api=rest%2F2.0%2Focr%2Fv1%2Ftaxi_receipt&method=post
|
||||||
|
*
|
||||||
|
* 出租车发票识别(百度OCR)
|
||||||
|
*
|
||||||
|
* 支持识别全国各大城市的出租车票的全部字段,包括发票号码、发票代码、电话、日期、金额、
|
||||||
|
* 上下车时间、里程等。
|
||||||
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/taxi/invoice")
|
@RequestMapping("/taxi/invoice")
|
||||||
@NotIntercept
|
@NotIntercept
|
||||||
public class RecognizeTaxiInvoiceController extends BaseController {
|
public class RecognizeTaxiInvoiceController extends BaseController {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ARecognizeTaxiInvoiceHandle arRecognizeTaxiInvoiceHandle;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private BTaxiReceiptHandle bTaxiReceiptHandle;
|
private BTaxiReceiptHandle bTaxiReceiptHandle;
|
||||||
|
|
||||||
@PostMapping("/recognize")
|
@PostMapping("/recognize")
|
||||||
public R recognize(ARecognizeTaxiInvoiceRequest request) {
|
public R recognize(@RequestBody TaxiInvoiceRecognizeRequest request) {
|
||||||
if (!hasImage(request)) {
|
if (request == null) {
|
||||||
return R.error("imageUrl和imageBase64不能同时为空");
|
return R.error("请求参数不能为空");
|
||||||
|
}
|
||||||
|
if (isBlank(request.getImageBase64()) && isBlank(request.getImageUrl())) {
|
||||||
|
return R.error("imageBase64和imageUrl不能同时为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiR<RecognizeTaxiInvoiceResponse> aR = arRecognizeTaxiInvoiceHandle.handle(request);
|
BTaxiReceiptRequest bRequest = toBaiduRequest(request);
|
||||||
if (isValidAliResponse(aR)) {
|
|
||||||
RecognizeTaxiInvoiceResp resp = toResp(aR.getData().getBody().getData());
|
ApiR<BTaxiReceiptResp> apiR = bTaxiReceiptHandle.handle(bRequest);
|
||||||
if (CollectionUtils.isNotEmpty(resp.getRawItems())) {
|
if (apiR == null || !apiR.isSuccess() || apiR.getData() == null) {
|
||||||
return R.ok().setData(resp);
|
return R.error(thirdError(apiR));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiR<BTaxiReceiptResp> bR = bTaxiReceiptHandle.handle(toBaiduRequest(request));
|
BTaxiReceiptResp bResp = apiR.getData();
|
||||||
if (bR != null && bR.isSuccess() && bR.getData() != null && bR.getData().getWordsResult() != null) {
|
BTaxiReceiptResp.WordsResultDTO wordsResult = bResp.getWordsResult();
|
||||||
return R.ok().setData(toResp(bR.getData().getWordsResult()));
|
if (wordsResult == null) {
|
||||||
|
return R.error("未识别到出租车发票信息");
|
||||||
}
|
}
|
||||||
|
|
||||||
return R.error(!isBlank(thirdError(aR)) ? thirdError(aR) : thirdError(bR));
|
return R.ok().setData(toResp(wordsResult));
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidAliResponse(ApiR<RecognizeTaxiInvoiceResponse> apiR) {
|
private BTaxiReceiptRequest toBaiduRequest(TaxiInvoiceRecognizeRequest request) {
|
||||||
return apiR != null
|
BTaxiReceiptRequest bRequest = new BTaxiReceiptRequest();
|
||||||
&& apiR.isSuccess()
|
if (isNotBlank(request.getImageBase64())) {
|
||||||
&& apiR.getData() != null
|
bRequest.setImageBase64(encodeParam(request.getImageBase64()));
|
||||||
&& 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;
|
|
||||||
}
|
}
|
||||||
|
if (isNotBlank(request.getImageUrl())) {
|
||||||
RecognizeTaxiInvoiceResponseBody.RecognizeTaxiInvoiceResponseBodyDataInvoices invoice =
|
bRequest.setImageUrl(encodeParam(request.getImageUrl()));
|
||||||
data.getInvoices().get(0);
|
|
||||||
if (invoice == null || CollectionUtils.isEmpty(invoice.getItems())) {
|
|
||||||
resp.setRawItems(rawItems);
|
|
||||||
return resp;
|
|
||||||
}
|
}
|
||||||
|
return bRequest;
|
||||||
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) {
|
private RecognizeTaxiInvoiceResp toResp(BTaxiReceiptResp.WordsResultDTO wordsResult) {
|
||||||
@ -165,36 +93,16 @@ public class RecognizeTaxiInvoiceController extends BaseController {
|
|||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setInvoiceCode(RecognizeTaxiInvoiceResp resp, String value) {
|
private String encodeParam(String value) {
|
||||||
if (isBlank(resp.getInvoiceCode())) {
|
try {
|
||||||
resp.setInvoiceCode(value);
|
return URLEncoder.encode(value, "UTF-8");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
log.error("URL编码失败:{}", value, e);
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setInvoiceNum(RecognizeTaxiInvoiceResp resp, String value) {
|
private boolean isNotBlank(String value) {
|
||||||
if (isBlank(resp.getInvoiceNum())) {
|
return value != null && value.trim().length() > 0;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,27 +1,31 @@
|
|||||||
package com.heyu.api.controller.car;
|
package com.heyu.api.controller.car;
|
||||||
|
|
||||||
import com.aliyun.ocr20191230.models.RecognizeTrainTicketResponse;
|
import com.heyu.api.baidu.handle.financial.BTrainTicketHandle;
|
||||||
import com.aliyun.ocr20191230.models.RecognizeTrainTicketResponseBody;
|
import com.heyu.api.baidu.request.financial.BTrainTicketRequest;
|
||||||
import com.heyu.api.alibaba.handle.common.text.ARecognizeTrainTicketHandle;
|
import com.heyu.api.baidu.response.financial.BTrainTicketResp;
|
||||||
import com.heyu.api.alibaba.request.common.text.ARecognizeTrainTicketRequest;
|
|
||||||
import com.heyu.api.controller.BaseController;
|
import com.heyu.api.controller.BaseController;
|
||||||
import com.heyu.api.data.annotation.NotIntercept;
|
import com.heyu.api.data.annotation.NotIntercept;
|
||||||
import com.heyu.api.data.utils.ApiR;
|
import com.heyu.api.data.utils.ApiR;
|
||||||
import com.heyu.api.data.utils.R;
|
import com.heyu.api.data.utils.R;
|
||||||
|
import com.heyu.api.request.car.TrainTicketRecognizeRequest;
|
||||||
import com.heyu.api.resp.car.RecognizeTrainTicketResp;
|
import com.heyu.api.resp.car.RecognizeTrainTicketResp;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
/***
|
import java.io.UnsupportedEncodingException;
|
||||||
* https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeTrainTicket?tab=DOC&lang=JAVA
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://console.bce.baidu.com/support/#/api?product=AI&project=文字识别&parent=财务票据OCR&api=rest%2F2.0%2Focr%2Fv1%2Ftrain_ticket&method=post
|
||||||
*
|
*
|
||||||
* 火车票识别
|
* 火车票识别(百度OCR)
|
||||||
*
|
|
||||||
* RecognizeTrainTicket
|
|
||||||
*
|
*
|
||||||
|
* 支持识别各类火车票的全部字段,包括车票号码、乘车日期时间、出发站、到达站、车次号、席别、座位号、
|
||||||
|
* 乘车人姓名、票价等信息。
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
@ -30,42 +34,90 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
public class RecognizeTrainTicketController extends BaseController {
|
public class RecognizeTrainTicketController extends BaseController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ARecognizeTrainTicketHandle aRecognizeTrainTicketHandle;
|
private BTrainTicketHandle bTrainTicketHandle;
|
||||||
|
|
||||||
@PostMapping("/recognize")
|
@PostMapping("/recognize")
|
||||||
public R recognize(ARecognizeTrainTicketRequest request) {
|
public R recognize(@RequestBody TrainTicketRecognizeRequest request) {
|
||||||
if (!hasImage(request)) {
|
if (request == null) {
|
||||||
return R.error("imageUrl和imageBase64不能同时为空");
|
return R.error("请求参数不能为空");
|
||||||
|
}
|
||||||
|
if (isBlank(request.getImageBase64()) && isBlank(request.getImageUrl())) {
|
||||||
|
return R.error("imageBase64和imageUrl不能同时为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiR<RecognizeTrainTicketResponse> aR = aRecognizeTrainTicketHandle.handle(request);
|
BTrainTicketRequest bRequest = toBaiduRequest(request);
|
||||||
if (!isValidAliResponse(aR)) {
|
|
||||||
return R.error(thirdError(aR));
|
ApiR<BTrainTicketResp> apiR = bTrainTicketHandle.handle(bRequest);
|
||||||
|
if (apiR == null || !apiR.isSuccess() || apiR.getData() == null) {
|
||||||
|
return R.error(thirdError(apiR));
|
||||||
}
|
}
|
||||||
|
|
||||||
return R.ok().setData(toResp(aR.getData().getBody().getData()));
|
BTrainTicketResp bResp = apiR.getData();
|
||||||
|
BTrainTicketResp.WordsResultDTO wordsResult = bResp.getWordsResult();
|
||||||
|
if (wordsResult == null) {
|
||||||
|
return R.error("未识别到火车票信息");
|
||||||
|
}
|
||||||
|
|
||||||
|
return R.ok().setData(toResp(wordsResult));
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidAliResponse(ApiR<RecognizeTrainTicketResponse> apiR) {
|
private BTrainTicketRequest toBaiduRequest(TrainTicketRecognizeRequest request) {
|
||||||
return apiR != null
|
BTrainTicketRequest bRequest = new BTrainTicketRequest();
|
||||||
&& apiR.isSuccess()
|
if (isNotBlank(request.getImageBase64())) {
|
||||||
&& apiR.getData() != null
|
bRequest.setImageBase64(encodeParam(request.getImageBase64()));
|
||||||
&& isSuccessStatusCode(apiR.getData().getStatusCode())
|
}
|
||||||
&& apiR.getData().getBody() != null
|
if (isNotBlank(request.getImageUrl())) {
|
||||||
&& apiR.getData().getBody().getData() != null;
|
bRequest.setImageUrl(encodeParam(request.getImageUrl()));
|
||||||
|
}
|
||||||
|
return bRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
private RecognizeTrainTicketResp toResp(
|
private RecognizeTrainTicketResp toResp(BTrainTicketResp.WordsResultDTO words) {
|
||||||
RecognizeTrainTicketResponseBody.RecognizeTrainTicketResponseBodyData data) {
|
|
||||||
RecognizeTrainTicketResp resp = new RecognizeTrainTicketResp();
|
RecognizeTrainTicketResp resp = new RecognizeTrainTicketResp();
|
||||||
resp.setPrice(data.getPrice());
|
|
||||||
resp.setDestination(data.getDestination());
|
resp.setName(words.getName());
|
||||||
resp.setDepartureStation(data.getDepartureStation());
|
resp.setNumber(words.getTrainNum());
|
||||||
resp.setDate(data.getDate());
|
resp.setDepartureStation(words.getStartingStation());
|
||||||
resp.setNumber(data.getNumber());
|
resp.setDestination(words.getDestinationStation());
|
||||||
resp.setSeat(data.getSeat());
|
resp.setLevel(words.getSeatCategory());
|
||||||
resp.setName(data.getName());
|
resp.setSeat(words.getSeatNum());
|
||||||
resp.setLevel(data.getLevel());
|
|
||||||
|
// 日期和时间合并:百度返回独立date和time字段,合并为"2017年08月05日 22:09开"格式
|
||||||
|
StringBuilder dateTime = new StringBuilder();
|
||||||
|
if (isNotBlank(words.getDate())) {
|
||||||
|
dateTime.append(words.getDate());
|
||||||
|
}
|
||||||
|
if (isNotBlank(words.getTime())) {
|
||||||
|
if (dateTime.length() > 0) {
|
||||||
|
dateTime.append(" ");
|
||||||
|
}
|
||||||
|
dateTime.append(words.getTime());
|
||||||
|
}
|
||||||
|
resp.setDate(dateTime.length() > 0 ? dateTime.toString() : null);
|
||||||
|
|
||||||
|
// 票价:百度返回字符串,转为Float
|
||||||
|
if (isNotBlank(words.getTicketRates())) {
|
||||||
|
try {
|
||||||
|
resp.setPrice(Float.parseFloat(words.getTicketRates()));
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
log.warn("票价解析失败:{}", words.getTicketRates());
|
||||||
|
resp.setPrice(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String encodeParam(String value) {
|
||||||
|
try {
|
||||||
|
return URLEncoder.encode(value, "UTF-8");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
log.error("URL编码失败:{}", value, e);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isNotBlank(String value) {
|
||||||
|
return value != null && value.trim().length() > 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,29 @@
|
|||||||
|
package com.heyu.api.request.car;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 出租车发票识别请求参数(百度OCR)
|
||||||
|
*
|
||||||
|
* 支持识别全国各大城市的出租车票的全部字段,包括发票号码、发票代码、电话、日期、金额、
|
||||||
|
* 上下车时间、里程等。
|
||||||
|
*
|
||||||
|
* 百度文档:https://console.bce.baidu.com/support/#/api?product=AI&project=文字识别&parent=财务票据OCR&api=rest%2F2.0%2Focr%2Fv1%2Ftaxi_receipt&method=post
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class TaxiInvoiceRecognizeRequest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M
|
||||||
|
* 支持jpg/jpeg/png/bmp格式
|
||||||
|
* 和url二选一
|
||||||
|
*/
|
||||||
|
private String imageBase64;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图片完整URL,URL长度不超过1024字节
|
||||||
|
* 和imageBase64二选一
|
||||||
|
*/
|
||||||
|
private String imageUrl;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
package com.heyu.api.request.car;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 火车票识别请求参数(百度OCR)
|
||||||
|
*
|
||||||
|
* 支持识别各类火车票的全部字段,包括车票号码、乘车日期时间、出发站、到达站、车次号、席别、座位号、
|
||||||
|
* 乘车人姓名、票价等信息。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class TrainTicketRecognizeRequest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M
|
||||||
|
* 支持jpg/jpeg/png/bmp格式
|
||||||
|
* 和url二选一
|
||||||
|
*/
|
||||||
|
private String imageBase64;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图片完整URL,URL长度不超过1024字节
|
||||||
|
* 和imageBase64二选一
|
||||||
|
*/
|
||||||
|
private String imageUrl;
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,19 +1,13 @@
|
|||||||
package com.heyu.api.resp.car;
|
package com.heyu.api.resp.car;
|
||||||
|
|
||||||
|
|
||||||
import com.aliyun.tea.NameInMap;
|
|
||||||
import com.heyu.api.data.dto.BaseResp;
|
import com.heyu.api.data.dto.BaseResp;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
/***
|
/**
|
||||||
*
|
* 火车票识别响应
|
||||||
*
|
|
||||||
* https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeTrainTicket?useCommon=true
|
|
||||||
* 火车票识别
|
|
||||||
*
|
|
||||||
* RecognizeTrainTicket
|
|
||||||
*
|
|
||||||
*
|
*
|
||||||
|
* 百度OCR文档:https://console.bce.baidu.com/support/#/api?product=AI&project=文字识别&parent=财务票据OCR&api=rest%2F2.0%2Focr%2Fv1%2Ftrain_ticket&method=post
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class RecognizeTrainTicketResp extends BaseResp {
|
public class RecognizeTrainTicketResp extends BaseResp {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user