Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[ABAP] Generate a QR-Code

There are many blogs describing how to create a QR-Code in the context of SAPscript or Smartforms (e.g. here, here, here and here). But I was looking for a way to generate a QR-Code and only receive the graphical data stream from it, without the need for any manual steps such a creation via SE73. During my search, I found these two notes, which contained all the information I needed:

  • 2790500: mentions the class CL_RSTX_BARCODE_RENDERER with method QR_CODE
  • 2030263: describes the parameters for a QC-Code creation

Following my little test report:

PARAMETERS p_text TYPE string DEFAULT 'My QR-Code Content'.

DATA: lv_action   TYPE i.
DATA: lv_filename TYPE string.
DATA: lv_fullpath TYPE string.
DATA: lv_path     TYPE string.

TRY.
    cl_rstx_barcode_renderer=>qr_code( EXPORTING i_module_size      = 25                " Size of smallest module (in pixel, max: 32000)
                                                 i_barcode_text     = p_text            " Barcode text
*                                                 i_mode             = 'A'               " Mode ('N', 'A', 'L', 'B', 'K', 'U', '1', '2'; note 2030263)
*                                                 i_error_correction = 'H'               " Error correction ('L', 'M', 'Q', 'H')
*                                                 i_rotation         = 0                 " Rotation (0, 90, 180 or 270)
                                       IMPORTING e_bitmap           = DATA(e_bitmap) ). " Bitmap in BMP format

    DATA(lt_raw_data) = cl_bcs_convert=>xstring_to_solix( e_bitmap ).

    " Save-Dialog
    cl_gui_frontend_services=>file_save_dialog( EXPORTING default_file_name = 'QR-Code'
                                                          default_extension = 'bmp'
                                                          file_filter       = |{ cl_gui_frontend_services=>filetype_all }|
                                                CHANGING  filename          = lv_filename
                                                          path              = lv_path
                                                          fullpath          = lv_fullpath
                                                          user_action       = lv_action ).

    IF lv_action EQ cl_gui_frontend_services=>action_ok.

      " Download file to disk
      cl_gui_frontend_services=>gui_download( EXPORTING filename     = lv_fullpath
                                                        filetype     = 'BIN'
                                                        bin_filesize = xstrlen( e_bitmap )
                                              CHANGING  data_tab     = lt_raw_data ).
    ENDIF.

  CATCH cx_rstx_barcode_renderer INTO DATA(lo_exp).
    MESSAGE lo_exp->get_text( ) TYPE 'I'.
ENDTRY.