Cómo guardar capturas de pantalla en SAP ABAP

2020-10-19       

Esta breve publicación de blog contiene un fragmento de ABAP que permite guardar una captura de pantalla en el disco duro del usuario de SAP ERP, usando la conocida clase CL_GUI_FRONTEND_SERVICES.

La clase ABAP CL_GUI_FRONTEND_SERVICES es una herramienta bastante versátil que todo desarrollador de SAP ABAP debería conocer. Contiene muchas herramientas extremadamente útiles que ayudan a lidiar con los requisitos básicos de la interfaz de usuario, como mostrar cuadros de diálogo de confirmación, descargar datos o, obviamente, tomar capturas de pantalla.

Aquí hay un pequeño fragmento de ABAP que demuestra cómo guardar una captura de pantalla en ABAP. Si bien el método cl_gui_frontend_services => get_screenshot parece bastante obvio, saber cómo convertir los datos de la imagen sin procesar al formato SOLIX es algo que puede requerir ayuda. Esta pieza de código demuestra cómo hacerlo.

Programa ABAP: cómo tomar y descargar una captura de pantalla en ABAP

DATA lv_imagebytes TYPE xstring.
DATA lv_mimetype TYPE string.
DATA lv_user_action TYPE i.
DATA lv_file_name TYPE string.
DATA lv_full_path TYPE string.
DATA lv_filepath TYPE string.

* Wrap everything in a try/catch block to capture exceptions
TRY.
    * Capture a screenshot, using CL_GUI_FRONTEND_SERVICES
    * After this operation, lv_mimetype will contain the MIME type of the image, such as "image/jpg"
    cl_gui_frontend_services=>get_screenshot(
        IMPORTING
            mime_type_str = lv_mimetype
            image         = lv_imagebytes
    ).

    * Show the default "save file" dialog
    cl_gui_frontend_services=>file_save_dialog( 
        EXPORTING
            default_file_name = 'screenshot.jpg'
            default_extension = 'jpg'
            file_filter       = '(*.jpg)|*.jpg|'
        CHANGING
            filename          = lv_file_name
            path              = lv_filepath
            fullpath          = lv_full_path
            user_action       = lv_user_action
    ).

    * Check if the user confirmed the dialog.
    * TODO: implement the cancel action.
    IF lv_user_action = cl_gui_frontend_services=>action_ok.

        * Create an internal table, converting the image bytestring to SOLIX.
        DATA(it_solix_data) = cl_bcs_convert=>xstring_to_solix(
            EXPORTING
                iv_xstring = lv_image_bytes
        ).

        * Download the screenshot to the user's file system.
        cl_gui_frontend_services=>gui_download(
            EXPORTING
                filename     = lv_full_path
                filetype     = 'BIN'
                bin_filesize = xstrlen(lv_image_bytes)
            CHANGING
                data_tab     = it_solix_data
        ).

    ENDIF.

    CATCH cx_root INTO DATA(e_error_message).
ENDTRY.