How to create a QR code and show it in a Smartform

+ -

Before running the code given below what should we do?

 

1. Create a smartform. Follow the steps given below.

 

    Capture.PNG

 

2. Goto Form Interface and add a new variable under the import tab which holds the name
of the qrcode that we are passing from the driver program[W_NAME  type char20]

 

     Capture1.PNG

3. Create a new graphics and as shown below give the name as &W_NAME&. [Dynamic
variable]

 

        Capture2.PNG

 

  4.  Done.

  5. Copy the code below.

  6. Enjoy the emerging technology of qrcode in SAP.

 

 

Note : In the program given below I have used a screen 9000. Please do create it before running and also create acustom control in the screen layout and give the name ‘PICTURECONTROL’ to it.

 

SOURCE CODE

 

 

 

*&---------------------------------------------------------------------*
*& Report  Z03_QRCODE_IMAGE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

 



REPORT  z03_qrcode_image.

*types:  ty_boolean(1) type c.
*
*constants:
*  c_true  type ty_boolean value 'X',
*  c_false type ty_boolean value space.

DATA : bds_description  like bapisignat-prop_value.


* BDS handling
constants:
  c_bds_classname type sbdst_classname value 'DEVC_STXD_BITMAP',
  c_bds_classtype type sbdst_classtype value 'OT',          " others
  c_bds_mimetype  type bds_mimetp      value 'application/octet-stream',
  c_bds_original  type sbdst_doc_var_tg value 'OR'.



* Graphic handling
constants:
      c_stdtext  like thead-tdobject value 'TEXT',
      c_graphics like thead-tdobject value 'GRAPHICS',
      c_bmon     like thead-tdid     value 'BMON',
      c_bcol     like thead-tdid     value 'BCOL'.


DATA: gi_filename    type rlgrap-filename,
      gi_name        type stxbitmaps-tdname,
      gi_object      type stxbitmaps-tdobject,
      gi_id          type stxbitmaps-tdid,
      gi_btype       type stxbitmaps-tdbtype,
      gi_resident    type stxbitmaps-resident,
      gi_autoheight  type stxbitmaps-autoheight,
      gi_bmcomp      type stxbitmaps-bmcomp,
      gi_resolution  type stxbitmaps-resolution,

      l_extension type rlgrap-filename,
      l_docid     type stxbitmaps-docid.

"Picture Control
DATA: picture_container TYPE REF TO cl_gui_custom_container,
      picture_control   TYPE REF TO cl_gui_picture.


DATA: l_img_url     TYPE w3url.
DATA :l_img_subtype TYPE w3param-cont_type.
DATA : l_str_length TYPE i.
DATA : url TYPE string.
DATA l_content_length TYPE  i.


DATA mime TYPE  w3mimetabtype.
DATA: blob TYPE w3mimetabtype,
            blob_size TYPE w3param-cont_len,
            blob_type TYPE w3param-cont_type.

DATA : i_igs_image_converter TYPE REF TO cl_igs_image_converter.
DATA: content TYPE xstring.
DATA : http_client TYPE REF TO if_http_client.


TYPES : BEGIN OF ty_binary,
                    binary_field(1000) TYPE c,
               END OF ty_binary.

DATA : hex_tab1 TYPE TABLE OF ty_binary WITH HEADER LINE.


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS : qr_text TYPE char100 OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.



SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
  PARAMETERS : width TYPE int3,
               height TYPE int3.
SELECTION-SCREEN END OF BLOCK b2.



PARAMETERS : p_rad1 RADIOBUTTON GROUP rd1 DEFAULT 'X',
             p_rad2 RADIOBUTTON GROUP rd1.


START-OF-SELECTION.

  PERFORM download_qrcode.
  PERFORM convert_image.

*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD_QRCODE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*

FORM download_qrcode .

  CONCATENATE 'http://chart.apis.google.com/chart?chs=200x200&cht=qr&chld=|1&chl=' qr_text '/chart.png' INTO url.

  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = url
    IMPORTING
      client             = http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.


  IF sy-subrc = 0.

    http_client->send( ).

    http_client->receive( ).

    content = http_client->response->get_data( ).

    http_client->close( ).

    l_str_length = xstrlen( content ).

    CALL FUNCTION 'RSFO_XSTRING_TO_MIME'
      EXPORTING
        c_xstring = content
        i_length  = l_str_length
      TABLES
        c_t_mime  = mime.

  ENDIF.

ENDFORM.                    " DOWNLOAD_QRCODE

*&---------------------------------------------------------------------*
*&      Form  CONVERT_IMAGE

*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*

FORM convert_image .

  CREATE OBJECT i_igs_image_converter .

  i_igs_image_converter->input = 'image/png'.
  i_igs_image_converter->output = 'image/bmp'.
  i_igs_image_converter->width = width.
  i_igs_image_converter->height = height.

  CALL METHOD i_igs_image_converter->set_image
    EXPORTING
      blob      = mime
      blob_size = l_content_length.


  CALL METHOD i_igs_image_converter->execute
    EXCEPTIONS
      communication_error = 1
      internal_error      = 2
      external_error      = 3
      OTHERS              = 4.

  IF sy-subrc = 0.

    CALL METHOD i_igs_image_converter->get_image
      IMPORTING
        blob      = blob
        blob_size = blob_size
        blob_type = blob_type.

  ENDIF.

  IF sy-subrc = 0.


    IF  p_rad1 = 'X'.

      CALL SCREEN '9000'"Calling the screen for qrcode display

    ELSE.

      PERFORM show_smart_form. "calling the smartform for qrcode display

    ENDIF.

  ENDIF.

ENDFORM.                    " CONVERT_IMAGE


*&---------------------------------------------------------------------*
*&      Module  STATUS_9000  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*

MODULE status_9000 OUTPUT.

  SET PF-STATUS 'PF'.

"Creating the object for the container

  CREATE OBJECT picture_container
    EXPORTING
      container_name = 'PICTURECONTROL'.
  CREATE OBJECT picture_control
    EXPORTING
      parent = picture_container.

  "Calling the screen

  PERFORM call_screen.

ENDMODULE.                 " STATUS_9000  OUTPUT

*&---------------------------------------------------------------------*
*&      Form  CALL_SCREEN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*

FORM call_screen .

  "Creating the url of the image for the display in the container in the screen
  SPLIT blob_type AT '/' INTO blob_type l_img_subtype.

  CALL FUNCTION 'DP_CREATE_URL'
    EXPORTING
      type     = blob_type
      subtype  = l_img_subtype
      size     = blob_size
      lifetime = cndp_lifetime_transaction
    TABLES
      data     = blob
    CHANGING
      url      = l_img_url
    EXCEPTIONS
      OTHERS   = 1.


  IF sy-subrc IS INITIAL.
    CALL METHOD picture_control->load_picture_from_url
      EXPORTING
        url = l_img_url.
  ENDIF.

ENDFORM.                    " CALL_SCREEN

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.

  IF sy-ucomm = 'BACK'.


    LEAVE TO SCREEN 0.

  ENDIF.

ENDMODULE.                 " USER_COMMAND_9000  INPUT

*&---------------------------------------------------------------------*
*&      Form  SHOW_SMART_FORM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*

FORM show_smart_form .

  gi_name = 'QRCODE10'.         "name of the qrcode will be in se78 after one time running this program
  gi_object = 'GRAPHICS'.
  gi_id = 'BMAP'.
  gi_btype = 'BCOL'. "If u want black and white pass BMON
  gi_resident = ' '.
  gi_autoheight 'X'.
  gi_bmcomp = 'X'.
  l_extension = 'BMP'.

  "importing the image into se78 before displaying it in the smartform.

  perform import_bitmap_bds    using blob
                                   gi_name
                                   gi_object
                                   gi_id
                                   gi_btype
                                   l_extension
                                   ' '
                                   gi_resident
                                   gi_autoheight
                                   gi_bmcomp
                          changing l_docid
                                   gi_resolution.


IF sy-subrc = 0.

  DATA:fname TYPE rs38l_fnam.

  "gettingt the name FM of the smartform
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
   EXPORTING
     formname                 = 'ZQR_TEST'
*   VARIANT                  = ' '
*   DIRECT_CALL              = ' '
  IMPORTING
    fm_name                  = fname
* EXCEPTIONS
*   NO_FORM                  = 1
*   NO_FUNCTION_MODULE       = 2
*   OTHERS                   = 3  .

"Calling the FM of the smartform for display
  CALL FUNCTION fname
    EXPORTING
*    ARCHIVE_INDEX              =
*    ARCHIVE_INDEX_TAB          =
*    ARCHIVE_PARAMETERS         =
*    CONTROL_PARAMETERS         =
*    MAIL_APPL_OBJ              =
*    MAIL_RECIPIENT             =
*    MAIL_SENDER                =
*    OUTPUT_OPTIONS             =
*    USER_SETTINGS              = 'X'
      w_name                     = 'QRCODE10'
*  IMPORTING
*    DOCUMENT_OUTPUT_INFO       =
*    JOB_OUTPUT_INFO            =
*    JOB_OUTPUT_OPTIONS         =
*  EXCEPTIONS
*    FORMATTING_ERROR           = 1
*    INTERNAL_ERROR             = 2
*    SEND_ERROR                 = 3
*    USER_CANCELED              = 4
*    OTHERS                     = 5            .

  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

ENDIF.

ENDFORM.                    " SHOW_SMART_FORM

*&---------------------------------------------------------------------*
*&      Form  IMPORT_BITMAP_BDS (Copied from standard program and modified it as per the requirement)
*&---------------------------------------------------------------------*
form import_bitmap_bds
        using    p_blob       type w3mimetabtype
                 p_name           type stxbitmaps-tdname
                 p_object         type stxbitmaps-tdobject
                 p_id             type stxbitmaps-tdid
                 p_btype          type stxbitmaps-tdbtype
                 p_format         type c
                 p_title          like bds_description
                 p_resident       type stxbitmaps-resident
                 p_autoheight     type stxbitmaps-autoheight
                 p_bmcomp         type stxbitmaps-bmcomp
        changing p_docid          type stxbitmaps-docid
                 p_resolution     type stxbitmaps-resolution.


data: l_object_key type sbdst_object_key.
data: l_tab        type ddobjname.


data: begin of l_bitmap occurs 0,
        l(64) type x,
      end of l_bitmap.

data: l_filename        type string,
      l_bytecount       type i,
      l_bds_bytecount   type i.
data: l_color(1)        type c,

      l_width_tw        type stxbitmaps-widthtw,
      l_height_tw       type stxbitmaps-heighttw,
      l_width_pix       type stxbitmaps-widthpix,
      l_height_pix      type stxbitmaps-heightpix.
data: l_bds_object      type ref to cl_bds_document_set,
      l_bds_content     type sbdst_content,
      l_bds_components  type sbdst_components,
      wa_bds_components type line of sbdst_components,
      l_bds_signature   type sbdst_signature,
      wa_bds_signature  type line of sbdst_signature,
      l_bds_properties  type sbdst_properties,
      wa_bds_properties type line of sbdst_properties.


data  wa_stxbitmaps type stxbitmaps.



* Enqueue
  perform enqueue_graphic using p_object
                                p_name
                                p_id
                                p_btype.


* Bitmap conversion
  call function 'SAPSCRIPT_CONVERT_BITMAP_BDS'
       exporting
            color                    = 'X'
            format                   = p_format
            resident                 = p_resident
            bitmap_bytecount         = l_bytecount
            compress_bitmap          = p_bmcomp
       importing
            width_tw                 = l_width_tw
            height_tw                = l_height_tw
            width_pix                = l_width_pix
            height_pix               = l_height_pix
            dpi                      = p_resolution
            bds_bytecount            = l_bds_bytecount
       tables
            bitmap_file              = p_blob
            bitmap_file_bds          = l_bds_content
       exceptions
            format_not_supported     = 1
            no_bmp_file              = 2
            bmperr_invalid_format    = 3
            bmperr_no_colortable     = 4
            bmperr_unsup_compression = 5
            bmperr_corrupt_rle_data  = 6
            others                   = 7.

  if sy-subrc <> 0.

    perform dequeue_graphic using p_object
                                  p_name
                                  p_id
                                  p_btype.
    message id sy-msgid type sy-msgty number sy-msgno
            with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
    raising conversion_failed.

  endif.



* Save bitmap in BDS
  create object l_bds_object.

  wa_bds_components-doc_count  = '1'.
  wa_bds_components-comp_count = '1'.
  wa_bds_components-mimetype   = c_bds_mimetype.
  wa_bds_components-comp_size  = l_bds_bytecount.
  append wa_bds_components to l_bds_components.

  if p_docid is initial.          " graphic is new

    wa_bds_signature-doc_count = '1'.
    append wa_bds_signature to l_bds_signature.


    call method l_bds_object->create_with_table
         exporting
              classname  = c_bds_classname
              classtype  = c_bds_classtype
              components = l_bds_components
              content    = l_bds_content
         changing
              signature  = l_bds_signature
              object_key = l_object_key
         exceptions
              others     = 1.

    if sy-subrc <> 0.

      perform dequeue_graphic using p_object
                                    p_name
                                    p_id
                                    p_btype.
*      message e285 with p_name  'BDS'.

    endif.

    read table l_bds_signature index 1 into wa_bds_signature
    transporting doc_id.

    if sy-subrc = 0.

      p_docid = wa_bds_signature-doc_id.

    else.

      perform dequeue_graphic using p_object
                                    p_name
                                    p_id
                                    p_btype.
*      message e285 with p_name 'BDS'.

    endif.

  else.                " graphic already exists

********* read object_key for faster access *****
   clear l_object_key.
   select single * from stxbitmaps into wa_stxbitmaps
       where tdobject = p_object
         and tdid     = p_id
         and tdname   = p_name
         and tdbtype  = p_btype.

   select single tabname from bds_locl into l_tab
      where classname = c_bds_classname
         and classtype = c_bds_classtype.


   if sy-subrc = 0.

     select single object_key from (l_tab) into l_object_key
       where loio_id = wa_stxbitmaps-docid+10(32)
         and classname = c_bds_classname
           and classtype = c_bds_classtype.

   endif.

******** read object_key end ********************

    call method l_bds_object->update_with_table
         exporting
              classname  = c_bds_classname
              classtype  = c_bds_classtype
              object_key =