• ABAP调用百度OCR识别身份证信息


      1 *&---------------------------------------------------------------------*
      2 *& Report Z3426IDCARD
      3 *&---------------------------------------------------------------------*
      4 *&
      5 *&---------------------------------------------------------------------*
      6 REPORT z3426idcard.
      7 
      8 PERFORM frm_ocr_test.
      9 
     10 
     11 FORM frm_ocr_test.
     12   DATA:request_url TYPE string,
     13        token       TYPE string.
     14   DATA:request_str TYPE string,
     15        image_str   TYPE string.
     16   PERFORM frm_get_url CHANGING image_str.
     17   PERFORM frm_get_token CHANGING token.
     18   request_url = `https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=` && token.
     19   request_str = `id_card_side=front&image=` && image_str.
     20 
     21   cl_http_client=>create_by_url(
     22     EXPORTING
     23           url = request_url
     24     IMPORTING
     25       client = DATA(lo_http_client)
     26     EXCEPTIONS
     27       argument_not_found = 1
     28       plugin_not_active  = 2
     29       internal_error     = 3
     30       OTHERS             = 4 ).
     31 
     32   lo_http_client->request->set_header_field( name  = '~request_method' value = 'POST' ).
     33   lo_http_client->request->set_header_field( name  = '~server_protocol' value = 'HTTP/1.1' ).
     34   lo_http_client->request->set_header_field( name  = '~Content-Type' value = 'application/x-www-form-urlencoded' ).
     35   lo_http_client->request->set_header_field( name  = '~Accept-Encoding' value = 'gzip, deflate, br' ).
     36   lo_http_client->request->set_cdata( request_str ).
     37   lo_http_client->send( EXPORTING timeout = 200 EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2  ).
     38   lo_http_client->receive( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 ).
     39   lo_http_client->response->get_status( IMPORTING code = DATA(lv_code) reason = DATA(lv_message) ).
     40   lo_http_client->get_last_error( IMPORTING message = DATA(lv_error_msg) ).
     41   DATA(lv_response) = lo_http_client->response->get_cdata( ).
     42   lo_http_client->close( ).
     43   CHECK 200 = lv_code.
     44   DATA:BEGIN OF ls_response,
     45          log_id     TYPE string,
     46          error_code TYPE string,
     47          error_msg  TYPE string,
     48        END OF ls_response.
     49 
     50   CALL METHOD /ui2/cl_json=>deserialize
     51     EXPORTING
     52       json        = lv_response
     53       pretty_name = 'Y'
     54     CHANGING
     55       data        = ls_response.
     56 
     57   WRITE:ls_response-error_code,ls_response-error_msg,/.
     58   IF ls_response-error_code IS INITIAL.
     59     CALL TRANSFORMATION sjson2html SOURCE XML lv_response
     60                                     RESULT XML DATA(lv_html).
     61     DATA(lv_convert) = cl_abap_codepage=>convert_from( lv_html ).
     62     cl_abap_browser=>show_html( html_string = lv_convert ).
     63   ENDIF.
     64 ENDFORM.
     65 
     66 FORM frm_get_url CHANGING pv_str TYPE string.
     67   DATA:lv_filename TYPE string,
     68        lv_count    TYPE i,
     69        lt_data     TYPE STANDARD TABLE OF raw255,
     70        lv_xstr     TYPE xstring.
     71   lv_filename = 'C:\Users\3426\Desktop\PDFTEST\A.jpg'.
     72   CALL METHOD cl_gui_frontend_services=>gui_upload
     73     EXPORTING
     74       filename   = lv_filename
     75       filetype   = 'BIN'
     76     IMPORTING
     77       filelength = lv_count
     78     CHANGING
     79       data_tab   = lt_data.
     80 
     81   CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
     82     EXPORTING
     83       input_length = lv_count
     84     IMPORTING
     85       buffer       = lv_xstr
     86     TABLES
     87       binary_tab   = lt_data.
     88 
     89   CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
     90     EXPORTING
     91       input  = lv_xstr
     92     IMPORTING
     93       output = pv_str.
     94   pv_str = cl_http_utility=>escape_url( pv_str ).
     95 
     96 ENDFORM.
     97 
     98 FORM frm_get_token CHANGING pv_token TYPE string.
     99   DATA:request_url TYPE string.
    100   DATA:api_key    TYPE string,
    101        secret_key TYPE string.
    102   "https://console.bce.baidu.com/ai/?fromai=1#/ai/ocr/app/list
    103   api_key = `HNmH7Lm9ppAlS4achWAiGo5K`.
    104   secret_key = `xSQDPGlexzX0wXAnhKoPS2mcdGRO2W7a`.
    105   request_url = `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=` && api_key && `&client_secret=` && secret_key.
    106   cl_http_client=>create_by_url(
    107     EXPORTING
    108           url = request_url
    109     IMPORTING
    110       client = DATA(lo_http_client)
    111     EXCEPTIONS
    112       argument_not_found = 1
    113       plugin_not_active  = 2
    114       internal_error     = 3
    115       OTHERS             = 4 ).
    116 
    117   lo_http_client->request->set_header_field( name  = '~request_method' value = 'POST' ).
    118   lo_http_client->request->set_header_field( name  = '~server_protocol' value = 'HTTP/1.1' ).
    119   lo_http_client->request->set_header_field( name  = '~Accept-Encoding' value = 'gzip, deflate, br' ).
    120   lo_http_client->send( EXPORTING timeout = 200 EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2  ).
    121   lo_http_client->receive( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 ).
    122   lo_http_client->response->get_status( IMPORTING code = DATA(lv_code) reason = DATA(lv_message) ).
    123   lo_http_client->get_last_error( IMPORTING message = DATA(lv_error_msg) ).
    124   DATA(lv_response) = lo_http_client->response->get_cdata( ).
    125   lo_http_client->close( ).
    126   CHECK 200 = lv_code.
    127   DATA:BEGIN OF ls_response,
    128          refresh_token  TYPE string,
    129          expires_in     TYPE string,
    130          session_key    TYPE string,
    131          access_token   TYPE string,
    132          scope          TYPE string,
    133          session_secret TYPE string,
    134        END OF ls_response.
    135 
    136   CALL METHOD /ui2/cl_json=>deserialize
    137     EXPORTING
    138       json        = lv_response
    139       pretty_name = 'Y'
    140     CHANGING
    141       data        = ls_response.
    142 
    143   pv_token = ls_response-access_token.
    144 ENDFORM.
    View Code

    1.先到百度智能云注册和登录账号(目前是免费的):https://login.bce.baidu.com/

    2.注册号账号以后,点击左侧导航栏的产品服务,搜索"识别",选择文字识别

    3.点击创建应用

    4.选择具体需要使用的功能

    5.选择个人,输入描述后,点击立即创建按钮。

     

    6.创建完毕

     7.查看应用详情,保存API KEY和Secret Key,后面开发需要用到这两个Key

    8.查看开发文档:  https://cloud.baidu.com/doc/OCR/s/rk3h7xzck

    9.返回到ABAP端复制上述代码。执行时会报错,提示HTTP 500【证书问题】

    10.安装证书

    复制调试获取token FORM部分url到浏览器,点击小锁复制证书到本地

    SAP事务码:STRUST

     

    再重新执行ABAP程序:识别成功

  • 相关阅读:
    MySQL常用函数介绍
    SQL语法基础之DROP语句
    MySQL常见报错汇总
    SQL语法基础之SELECT
    SQL语法基础之ALTER语句
    OpenStack技术栈-OpenStack的基础原理概述
    体验Hadoop3.0生态圈-CDH6.1时代的来临
    Windows下强制删除文件或文件夹(解除文件占用/Unlock)
    foreach Transform 同时chils.setParent引起的bug
    CharacterController平滑移动到某点
  • 原文地址:https://www.cnblogs.com/1187163927ch/p/16117582.html
Copyright © 2020-2023  润新知