乐趣区

关于sap:用-ABAP-调用-OCR-接口实现出租车发票扫描

百度 AI 提供了一个出租车发票扫描接口:

https://ai.baidu.com/ai-doc/O…

反对辨认全国各大城市出租车票的 16 个关键字段,包含发票号码、代码、车号、日期、总金额、燃油附加费、叫车服务费、省、市、单价、里程、上车工夫、下车工夫等。

咱们首先在 postman 里调用该接口。

url:https://aip.baidubce.com/rest…[这里传入 Access Token]

依照接口文档的阐明,content-type 设置为 application/x-www-form-urlencoded:

在 body 里,类型抉择为 x-www-form-urlencoded:

指定参数的 key 为 image,value 为发票图片二进制数据对应的 base 64 编码的 url encode 值。

下图是一个例子。点击 Send 按钮,会收到下图所示的响应数据,该发票里的要害信息被 API 胜利解析。

这里咱们须要留神一点,在 postman 里,咱们指定参数 image 的值,是没有通过 url encode 的图片 base64 encode 的值,即下图图例 A。

而 postman 在发送该 HTTP 申请时,会主动把该 base 64 的值做一次 url encode 解决,比方 base64 里的符号 “/”, 被解决成了 %2F. 见上图 B 的图例。

也就是说,咱们在编写 ABAP 代码时,须要手动调用 cl_http_utility=>escape_url 执行这个 encode 动作。

上面是 ABAP 代码实现,逻辑不难。

report z.

DATA: http_client TYPE REF TO if_http_client.
DATA: ls_token TYPE string.
DATA: lv_token TYPE string.
DATA: url type string.

url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/taxi_receipt'.
ls_token = '这里须要填写你理论获取的 Access Token'.
CONCATENATE url '?access_token=' ls_token INTO lv_token.

CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url    = lv_token
    IMPORTING
      client = http_client.

http_client->propertytype_logon_popup = http_client->co_enabled .
http_client->propertytype_redirect = http_client->co_disabled .

  CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = '~request_method'
      value = 'POST'.                 "

  CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = '~request_protocol'
      value = 'HTTPS/1.1'.

  CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Type'
      value = 'application/x-www-form-urlencoded; charset=utf-8'.

*---------- 上传图片并转换成 base64 格局 start------------
types: begin of ty_pic,
           pic_data(1024) type x,
         end of ty_pic.
data:       path_string type string.
*parameters: p_file      like rlgrap-filename obligatory.
data:       lv_content  type xstring.
data:       encode_str  type string.
data:       len         type i.
data:       pic_tab     type table of ty_pic." 二进制内表

*  path_string = p_file.
  call function 'GUI_UPLOAD'
    exporting
      "filename   = path_string
      filename   = 'C:\Users\I042416\Documents\1.jpg'
      filetype   = 'BIN'
    importing
      filelength = len
    tables
      data_tab   = pic_tab[].

  call function 'SCMS_BINARY_TO_XSTRING'
    exporting
      input_length = len
    importing
      buffer       = lv_content
    tables
      binary_tab   = pic_tab[]
    exceptions
      failed       = 1
      others       = 2.

  " xstring 转 base64
  CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
  EXPORTING
    input  = lv_content
  IMPORTING
    output = encode_str.
*---------- 上传图片并转换成 base64 格局 end--------------

"set body body 内传 base64 格局图片
data: gv_json_send TYPE string.
data: lv_len       TYPE i.

encode_str = cl_http_utility=>escape_url(encode_str).
CONCATENATE 'image=' encode_str INTO gv_json_send.
  lv_len = strlen(gv_json_send).

  CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA
    EXPORTING
      DATA   = gv_json_send
      OFFSET = 0
      LENGTH = lv_len.

  CALL METHOD http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.

  CALL METHOD http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.

  IF sy-subrc <> 0.
    DATA:l_sysubrc    TYPE sysubrc,
         l_error_text TYPE string.
    l_sysubrc = sy-subrc.
    CALL METHOD http_client->get_last_error
      IMPORTING
        code    = l_sysubrc
        message = l_error_text.
  ENDIF.

"Read HTTP RETURN CODE
DATA:http_status_code TYPE i,
     status_text      TYPE string.
  CALL METHOD http_client->response->get_status
    IMPORTING
      code   = http_status_code
      reason = status_text
      .
  WRITE: / 'HTTP_STATUS_CODE =',
          http_status_code,
         / 'STATUS_TEXT =',
         status_text.

"READ RESPONSE DATA
DATA:w_result TYPE string.
  CALL METHOD http_client->response->get_cdata
    RECEIVING data = w_result .

* 获取返回的数据
  DATA: r_value    TYPE string,
        r_value1   TYPE string,
        r_fields   TYPE tihttpnvp,
        r_h_fields TYPE tihttpnvp.
  http_client->response->get_header_fields(CHANGING fields =  r_h_fields). " 返回表数据 

最初在 ABAP 端接管到胜利解析的后果:

更多 Jerry 的原创文章,尽在:” 汪子熙 ”:

退出移动版