[ABAP] QR-Codes als Bitmap erzeugen

* Docking-Container für Einbettung cl_gui_picture
DATA: o_dock TYPE REF TO cl_gui_docking_container.

* Text für QR-Code
PARAMETERS: p_text TYPE string DEFAULT 'https://google.de' LOWER CASE.

AT SELECTION-SCREEN OUTPUT.

  TRY.
      IF NOT o_dock IS BOUND.
* Dockingcontainer erzeugen
        o_dock = NEW #( repid = sy-repid
                        dynnr = sy-dynnr
                        side  = cl_gui_docking_container=>dock_at_right
                        ratio = 50 ).
      ENDIF.

      DATA: lv_xstr_bmp TYPE xstring.

* Siehe https://me.sap.com/notes/2790500
* Siehe https://me.sap.com/notes/2030263
* i_module_size      - Größe des Barcodes in 1-32000 Pixel, wobei 1 Pixel == ein 1/600 Zoll großer Punkt
* i_barcode_text     - Textinhalt
* i_mode             - A - AUTOMATIC
*                      N - NUMERIC
*                      L - ALPHANUMERIC
*                      B - BYTE_LATIN1
*                      K - KANJI
*                      U - BYTE_UTF8
*                      1 - FNC1_POS1 (FNC1 an erster Position)
*                      2 - FNC1_POS2 (FNC1 an zweiter Position)
* i_error_correction - Fehlerkorrekturwert (L - 7%, M - 15%, Q - 25%, H - 30%)
* i_rotation         - Barcode-Ausrichtung: 0 - NORMAL, 90 - ROTATED, 180 - INVERTED, 270 - BOTTOMUP
* Process QR Code barcode
      cl_rstx_barcode_renderer=>qr_code( EXPORTING i_module_size  = 50
                                                   i_barcode_text = p_text
*                                                 i_mode             = 'A'
*                                                 i_error_correction = 'H'
*                                                 i_rotation         = 0
                                         IMPORTING e_bitmap       = lv_xstr_bmp ).

      DATA(it_bin_bmp) = cl_bcs_convert=>xstring_to_solix( lv_xstr_bmp ).

      DATA: lv_url TYPE swk_url.

* temporäre URL auf das Bild erzeugen
      CALL FUNCTION 'DP_CREATE_URL'
        EXPORTING
          type                 = 'image/bmp' " https://wiki.selfhtml.org/wiki/MIME-Type/%C3%9Cbersicht
          subtype              = 'bmp'
        TABLES
          data                 = it_bin_bmp
        CHANGING
          url                  = lv_url
        EXCEPTIONS
          dp_invalid_parameter = 1
          dp_error_put_table   = 2
          dp_error_general     = 3
          OTHERS               = 4.

* Bild über die URL laden und anzeigen
      DATA(o_pic) = NEW cl_gui_picture( parent = o_dock ).
      o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).
      o_pic->load_picture_from_url_async( lv_url ).

* Flush ist wichtig für die Abarbeitung des GUI-Queues, sonst gibt es einen Core-Dump -> "SYSTEM_POINTER_PENDING"
      cl_gui_cfw=>flush( ).

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

[ABAP] Code128-Barcode als Bitmap erzeugen

* Docking-Container für Einbettung cl_gui_picture
DATA: o_dock TYPE REF TO cl_gui_docking_container.

* Text für Barcode
PARAMETERS: p_text TYPE string DEFAULT '1234567890' LOWER CASE.

AT SELECTION-SCREEN OUTPUT.

  TRY.
      IF NOT o_dock IS BOUND.
* Dockingcontainer erzeugen
        o_dock = NEW #( repid = sy-repid
                        dynnr = sy-dynnr
                        side  = cl_gui_docking_container=>dock_at_right
                        ratio = 50 ).
      ENDIF.

      DATA: lv_xstr_bmp TYPE xstring.

* Siehe: https://me.sap.com/notes/2790500
* Siehe: https://me.sap.com/notes/645158
* i_narrow_module_width - 1 ... 10, Breite für das schmalste Modul eines Barcodes in Pixeln
* i_height              - 1 ... 9999, Höhe der Balken des Barcodes in Pixeln (600 px == 1 Zoll)
* i_mode                - N (None), A (Automatic), U (UCC case mode)
* i_rotation            - 0 - Normal, 90 -  Rotated, 180 - Inverted, 270 - Bottomup
* Process Code 128 barcode
      cl_rstx_barcode_renderer=>code_128( EXPORTING i_narrow_module_width = 1
                                                    i_height              = 50
*                                                    i_mode                = 'A'
*                                                    i_rotation            = 0
                                                    i_barcode_text        = p_text
                                          IMPORTING e_bitmap              = lv_xstr_bmp ).

      DATA(it_bin_bmp) = cl_bcs_convert=>xstring_to_solix( lv_xstr_bmp ).

      DATA: lv_url TYPE swk_url.

* temporäre URL auf das Bild erzeugen
      CALL FUNCTION 'DP_CREATE_URL'
        EXPORTING
          type                 = 'image/bmp' " https://wiki.selfhtml.org/wiki/MIME-Type/%C3%9Cbersicht
          subtype              = 'bmp'
        TABLES
          data                 = it_bin_bmp
        CHANGING
          url                  = lv_url
        EXCEPTIONS
          dp_invalid_parameter = 1
          dp_error_put_table   = 2
          dp_error_general     = 3
          OTHERS               = 4.

* Bild über die URL laden und anzeigen
      DATA(o_pic) = NEW cl_gui_picture( parent = o_dock ).
      o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_fit_center ).
      o_pic->load_picture_from_url_async( lv_url ).

* Flush ist wichtig für die Abarbeitung des GUI-Queues, sonst gibt es einen Core-Dump -> "SYSTEM_POINTER_PENDING"
      cl_gui_cfw=>flush( ).

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