1.读取本地图片,转换为urlencode
2.获取baidu token,需要先申请自己的API_KEY和SECRET_KEY,赋给类参数
3.调用增值税发票的API
4.获取返回信息
CLASS zwft_invoice_orc DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
TYPES:
BEGIN OF words_result ,
invoicetypeorg TYPE string,
invoicecode TYPE string,
invoicenum TYPE string,
invoicedate TYPE string,
purchasername TYPE string,
purchaserregisternum TYPE string,
purchaseraddress TYPE string,
purchaserbank TYPE string,
province TYPE string,
city TYPE string,
sheetnum TYPE string,
commodityunit TYPE string,
sellername TYPE string,
sellerregisternum TYPE string,
selleraddress TYPE string,
sellerbank TYPE string,
totalamount TYPE string,
totaltax TYPE string,
amountinwords TYPE string,
remarks TYPE string,
END OF words_result .
TYPES:
BEGIN OF ty_invoice,
words_result_num TYPE string,
words_result TYPE words_result,
END OF ty_invoice .
DATA api_key TYPE string VALUE '' ##自己的API_KEY.
DATA secret_key TYPE string VALUE '' ##自己的密钥.
DATA image_xstring TYPE xstring .
METHODS constructor .
METHODS get_invoice
EXPORTING
!invoice TYPE ty_invoice .
PROTECTED SECTION.
PRIVATE SECTION.
TYPES:
BEGIN OF ty_token ,
refresh_token TYPE string,
access_token TYPE string,
END OF ty_token .
DATA token_url TYPE string VALUE 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials' ##NO_TEXT.
DATA api_url TYPE string VALUE 'https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice' ##NO_TEXT.
METHODS get_image
RETURNING
VALUE(image_urlencoded) TYPE string .
METHODS get_token
RETURNING
VALUE(token) TYPE string .
ENDCLASS.
CLASS ZWFT_INVOICE_ORC IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZWFT_INVOICE_ORC->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD constructor.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZWFT_INVOICE_ORC->GET_IMAGE
* +-------------------------------------------------------------------------------------------------+
* | [<-()] IMAGE_URLENCODED TYPE STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD get_image.
* image_xstring = zwft_common=>file_upload_bin( ).
* gui_upload获取文件信息
* 通过SCMS_BINARY_TO_XSTRING转换为XSTRING
CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
EXPORTING
input = image_xstring
IMPORTING
output = image_urlencoded.
image_urlencoded = cl_http_utility=>escape_url( image_urlencoded ).
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZWFT_INVOICE_ORC->GET_INVOICE
* +-------------------------------------------------------------------------------------------------+
* | [<---] INVOICE TYPE TY_INVOICE
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD get_invoice.
DATA:lo_http_client TYPE REF TO if_http_client.
DATA(image_data) = get_image( ).
CHECK image_data IS NOT INITIAL.
image_data = |image={ image_data }|.
DATA(token) = get_token( ).
CHECK token IS NOT INITIAL.
api_url = |{ api_url }?access_token={ token }|.
cl_http_client=>create_by_url( EXPORTING url = api_url IMPORTING client = lo_http_client ).
lo_http_client->request->set_method( if_http_request=>co_request_method_post ).
lo_http_client->request->set_header_field( name = if_http_header_fields=>content_type value = 'application/x-www-form-urlencoded' ).
lo_http_client->request->set_cdata( data = image_data length = strlen( image_data ) ).
lo_http_client->send( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 ).
lo_http_client->receive( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 ).
lo_http_client->response->get_status( IMPORTING code = DATA(lv_subrc) reason = DATA(return) ).
IF lv_subrc = 200.
DATA(cdata) = lo_http_client->response->get_cdata( ).
cl_fdt_json=>json_to_data( EXPORTING iv_json = cdata CHANGING ca_data = invoice ).
ELSE.
MESSAGE 'invoice get error' TYPE 'E'.
ENDIF.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Private Method ZWFT_INVOICE_ORC->GET_TOKEN
* +-------------------------------------------------------------------------------------------------+
* | [<-()] TOKEN TYPE STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD get_token.
DATA:lo_http_client TYPE REF TO if_http_client.
DATA ls_token TYPE ty_token.
token_url = |{ token_url }&client_id={ api_key }&client_secret={ secret_key }|.
cl_http_client=>create_by_url( EXPORTING url = token_url IMPORTING client = lo_http_client ).
lo_http_client->request->set_header_field( name = if_http_header_fields=>content_type value = 'application/json' ).
lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).
lo_http_client->request->set_method( if_http_request=>co_request_method_get ).
lo_http_client->send( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 ).
lo_http_client->receive( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 ).
lo_http_client->response->get_status( IMPORTING code = DATA(lv_subrc) reason = DATA(return) ).
IF lv_subrc = 200.
cl_fdt_json=>json_to_data( EXPORTING iv_json = lo_http_client->response->get_cdata( ) CHANGING ca_data = ls_token ).
token = ls_token-access_token.
ELSE.
MESSAGE 'token get error' TYPE 'E'.
ENDIF.
ENDMETHOD.
ENDCLASS.
可以额外将xstring保存到表字段中或者WWWDATA中,调用HTML预览,下面为示例方法:
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZWFT_HTML=>SHOW_PICTURE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IMAGE_XSTRING TYPE XSTRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD show_picture.
DATA image_html TYPE cl_abap_browser=>html_table.
DATA mime TYPE lvc_t_mime.
DATA url(255) TYPE c.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = image_xstring
TABLES
binary_tab = mime.
CALL FUNCTION 'DP_CREATE_URL'
EXPORTING
type = 'IMAGE'
subtype = 'PNG'
TABLES
data = mime
CHANGING
url = url
EXCEPTIONS
dp_invalid_parameter = 1
dp_error_put_table = 2
dp_error_general = 3
OTHERS = 4.
APPEND |<img CLASS="image" src="{ url }" alt="picture"</img>| TO image_html.
DATA error_list TYPE cl_abap_browser=>html_table.
cl_abap_browser=>show_html(
EXPORTING
html = image_html
* html_string = html_str
printing = abap_true
context_menu = abap_true
dialog = 'X'
IMPORTING
html_errors = error_list ).
ENDMETHOD.