[ABAP] Komplexbeispiel: Binärdaten Screenshot mit GZIP komprimieren, in Base64-String wandeln und zurück (CL_ABAP_GZIP, SSFC_BASE64_ENCODE, SSFC_BASE64_DECODE)

TRY.
    DATA: lv_mime_type TYPE string.
    DATA: lv_image_bytes TYPE xstring.

* Ein paar Bytes holen: Screenshot aufnehmen
    cl_gui_frontend_services=>get_screenshot( IMPORTING
                                                mime_type_str = lv_mime_type
                                                image         = lv_image_bytes ).

    DATA: lv_xstr_gzip TYPE xstring.

* Binärdaten mit GZIP komprimieren
    cl_abap_gzip=>compress_binary( EXPORTING raw_in   = lv_image_bytes
                                   IMPORTING gzip_out = lv_xstr_gzip ).

    DATA(lv_base64_data) = ||.

* Base64 codieren
    CALL FUNCTION 'SSFC_BASE64_ENCODE'
      EXPORTING
        bindata                  = lv_xstr_gzip
      IMPORTING
        b64data                  = lv_base64_data
      EXCEPTIONS
        ssf_krn_error            = 1
        ssf_krn_noop             = 2
        ssf_krn_nomemory         = 3
        ssf_krn_opinv            = 4
        ssf_krn_input_data_error = 5
        ssf_krn_invalid_par      = 6
        ssf_krn_invalid_parlen   = 7
        OTHERS                   = 8.
    IF sy-subrc = 0.

* Base64-String in der Listenausgabe darstellen
      DATA: it_text80 TYPE STANDARD TABLE OF text80 WITH DEFAULT KEY.
* formatierte Ausgabe (80 Zeichen Breite)
      CALL FUNCTION 'SWA_STRING_TO_TABLE'
        EXPORTING
          character_string = lv_base64_data
        IMPORTING
          character_table  = it_text80.

      LOOP AT it_text80 ASSIGNING FIELD-SYMBOL(<t>).
        WRITE: / <t>.
      ENDLOOP.

      DATA: lv_xstr_dec TYPE xstring.

* Base64 decodieren
      CALL FUNCTION 'SSFC_BASE64_DECODE'
        EXPORTING
          b64data                  = lv_base64_data
        IMPORTING
          bindata                  = lv_xstr_dec
        EXCEPTIONS
          ssf_krn_error            = 1
          ssf_krn_noop             = 2
          ssf_krn_nomemory         = 3
          ssf_krn_opinv            = 4
          ssf_krn_input_data_error = 5
          ssf_krn_invalid_par      = 6
          ssf_krn_invalid_parlen   = 7
          OTHERS                   = 8.

      IF sy-subrc = 0.
        DATA: lv_pic_bytes TYPE xstring.

* Binärdaten mit GZIP dekomprimieren
        cl_abap_gzip=>decompress_binary( EXPORTING gzip_in = lv_xstr_dec IMPORTING raw_out = lv_pic_bytes ).

* freier Custom-Container innerhalb der Listenansicht
        DATA(o_cnt) = NEW cl_gui_custom_container( container_name = ''
                                                   repid          = 'SAPMSSY0'
                                                   dynnr          = '0120' ).

* Position des Containers setzen
        o_cnt->set_top( 50 ).
        o_cnt->set_left( 50 ).
        o_cnt->set_width( 640 ).
        o_cnt->set_height( 240 ).

* Bild über die URL laden und anzeigen
        DATA(o_pic) = NEW cl_gui_picture( parent = o_cnt ).
        o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_stretch ).

* xstring -> solix
        DATA(it_bin_data) = cl_bcs_convert=>xstring_to_solix( lv_pic_bytes ).

        DATA: lv_url TYPE swk_url.

* temporäre URL für das Bild erzeugen
        CALL FUNCTION 'DP_CREATE_URL'
          EXPORTING
            type                 = 'image/jpeg' " https://wiki.selfhtml.org/wiki/Referenz:MIME-Typen
            subtype              = ''
          TABLES
            data                 = it_bin_data
          CHANGING
            url                  = lv_url
          EXCEPTIONS
            dp_invalid_parameter = 1
            dp_error_put_table   = 2
            dp_error_general     = 3
            OTHERS               = 4.

* Bild von URL laden
        o_pic->load_picture_from_url_async( lv_url ).

      ENDIF.
    ENDIF.

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Kommentare sind geschlossen.