0% found this document useful (0 votes)
319 views14 pages

Function Module QR Code

This document contains code for a FUNCTION MODULE in ABAP that generates QR codes from URLs. The module accepts URL and QR code parameters, calls functions to retrieve the QR code image from the URL, convert it to BMP format, and import it into the SAP system as a document object. It returns status and message outputs.

Uploaded by

nstomar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
319 views14 pages

Function Module QR Code

This document contains code for a FUNCTION MODULE in ABAP that generates QR codes from URLs. The module accepts URL and QR code parameters, calls functions to retrieve the QR code image from the URL, convert it to BMP format, and import it into the SAP system as a document object. It returns status and message outputs.

Uploaded by

nstomar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

FUNCTION MODULE

QR CODE
https://belajarabap.wordpress.com/

Function Module
FUNCTION ZFM_QRCODE.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(FI_URL) TYPE STRING OPTIONAL
*" VALUE(FI_QRCODE) TYPE ZDE_QRCOD OPTIONAL
*" VALUE(FI_WIDTH) TYPE I OPTIONAL
*" VALUE(FI_HEIGHT) TYPE I OPTIONAL
*" VALUE(FI_IMAGENAME) TYPE TDOBNAME OPTIONAL
*" EXPORTING
*" VALUE(FE_STATS) TYPE CHAR1
*" VALUE(FE_MESSG) TYPE STRING
*" EXCEPTIONS
*" ENQUEUE_FAILED
*"----------------------------------------------------------------------
DATA: ld_content LIKE d_xstring,
ld_strlen TYPE i,
ld_blob TYPE w3mimetabtype,
ld_object TYPE stxbitmaps-tdobject,
ld_id TYPE stxbitmaps-tdid,
ld_btype TYPE stxbitmaps-tdbtype,
ld_resident TYPE stxbitmaps-resident, https://belajarabap.wordpress.com
ld_autoheight TYPE stxbitmaps-autoheight,
ld_bmcomp TYPE stxbitmaps-bmcomp,
ld_resolution TYPE stxbitmaps-resolution,
ld_extension TYPE rlgrap-filename,
ld_docid TYPE stxbitmaps-docid,
ld_data TYPE string,
ld_quote(1) VALUE '"'.

CONCATENATE '{' ld_quote 'url' ld_quote ':' ld_quote fi_qrcode ld_quote '}'
INTO ld_data.

1
PERFORM f_get_qrcode_from_url USING fi_url
ld_data
CHANGING ld_content
ld_strlen
fe_stats
fe_messg.

CHECK fe_stats IS INITIAL.

PERFORM f_convert_image USING ld_content


ld_strlen
fi_width
fi_height
CHANGING ld_blob
fe_stats
fe_messg.

CHECK fe_stats IS INITIAL.

ld_object = 'GRAPHICS'.
ld_id = 'BMAP'.
ld_btype = 'BCOL'. "If u want black and white pass BMON
ld_resident = ' '.
ld_autoheight = 'X'.
ld_bmcomp = 'X'.
ld_extension = 'BMP'.

PERFORM f_import_image USING ld_blob


fi_imagename
ld_object
ld_id
ld_btype
ld_extension
' ' https://belajarabap.wordpress.com
ld_resident
ld_autoheight
ld_bmcomp
CHANGING ld_docid
ld_resolution
fe_stats
fe_messg.

ENDFUNCTION.

2
Include TOP
type-pools: SBDST.
data: d_xstring TYPE xstring.

Include F01
*&---------------------------------------------------------------------*
*& Form F_GET_QRCODE_FROM_URL
*&---------------------------------------------------------------------*
FORM f_get_qrcode_from_url USING fu_url TYPE string
fu_data TYPE string
CHANGING fc_content LIKE d_xstring
fc_strlen TYPE i
fc_stats
fc_messg TYPE string.

DATA: http_client TYPE REF TO if_http_client,


ld_length TYPE i,
ld_xdata TYPE xstring,
ld_content TYPE string.

CALL METHOD cl_http_client=>create_by_url


EXPORTING
url = fu_url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc NE 0.
fc_stats = 'E'.
CASE sy-subrc. https://belajarabap.wordpress.com
WHEN 1. fc_messg = 'Error when creating url: Argument no found'.
WHEN 2. fc_messg = 'Error when creating url: Plugin not active'.
WHEN OTHERS. fc_messg = 'Error when creating url'.
ENDCASE.
RETURN.
ENDIF.

3
* set http_client header parameters
CALL METHOD http_client->request->set_header_field
EXPORTING
name = '~request_method'
value = 'POST'.

CALL METHOD http_client->request->set_header_field


EXPORTING
name = '~server_protocol'
value = 'HTTP/1.1'.

CALL METHOD http_client->request->set_header_field


EXPORTING
name = 'Content-Type'
value = 'application/json'.

* set data to be sent


ld_length = STRLEN( fu_data ).
CALL METHOD http_client->request->set_cdata
EXPORTING
data = fu_data
offset = 0
length = ld_length.

* send data
CALL METHOD http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_invalid_timeout = 3.
IF sy-subrc NE 0.
fc_stats = 'E'.
CALL METHOD http_client->get_last_error
IMPORTING
message = fc_messg. https://belajarabap.wordpress.com
RETURN.
ENDIF.

* receive data
CALL METHOD http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
IF sy-subrc NE 0.

4
fc_stats = 'E'.
CALL METHOD http_client->get_last_error
IMPORTING
message = fc_messg.
RETURN.
ENDIF.

ld_content = http_client->response->get_cdata( ).
fc_strlen = STRLEN( ld_content ).

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'


EXPORTING
text = ld_content
mimetype = 'X'
IMPORTING
buffer = fc_content
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
fc_stats = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO fc_messg.
RETURN.
ENDIF.

fc_strlen = XSTRLEN( fc_content ).

ENDFORM. " F_GET_QRCODE_FROM_URL


*&---------------------------------------------------------------------*
*& Form F_CONVERT_IMAGE
*&---------------------------------------------------------------------*
FORM f_convert_image USING fu_content LIKE d_xstring
fu_strlen TYPE i https://belajarabap.wordpress.com
fu_width TYPE i
fu_height TYPE i
CHANGING fc_blob TYPE w3mimetabtype
fc_stats
fc_messg TYPE string.

DATA: lcl_img_conv TYPE REF TO cl_igs_image_converter.

DATA: lt_mime TYPE w3mimetabtype,


ld_bloblen TYPE i,

5
ld_blob_size TYPE w3param-cont_len,
ld_blob_type TYPE w3param-cont_type.

* convert xstring to mime


CALL FUNCTION 'RSFO_XSTRING_TO_MIME'
EXPORTING
c_xstring = fu_content
i_length = fu_strlen
TABLES
c_t_mime = lt_mime.

CREATE OBJECT lcl_img_conv.

lcl_img_conv->input = 'image/png'.
lcl_img_conv->output = 'image/bmp'.
lcl_img_conv->width = fu_width.
lcl_img_conv->height = fu_height.

CALL METHOD lcl_img_conv->set_image


EXPORTING
blob = lt_mime
blob_size = ld_bloblen.

CALL METHOD lcl_img_conv->execute


EXCEPTIONS
communication_error = 1
internal_error = 2
external_error = 3
OTHERS = 4.

IF sy-subrc = 0.
CALL METHOD lcl_img_conv->get_image
IMPORTING
blob = fc_blob
blob_size = ld_blob_size https://belajarabap.wordpress.com
blob_type = ld_blob_type.
ELSE.
fc_stats = 'E'.
fc_messg = 'Error converting image'.
RETURN.
ENDIF.

ENDFORM. " F_CONVERT_IMAGE

6
*&---------------------------------------------------------------------*
*& Form F_IMPORT_IMAGE
*&---------------------------------------------------------------------*
FORM f_import_image
USING fu_blob TYPE w3mimetabtype
fu_name TYPE stxbitmaps-tdname
fu_object TYPE stxbitmaps-tdobject
fu_id TYPE stxbitmaps-tdid
fu_btype TYPE stxbitmaps-tdbtype
fu_format TYPE c
fu_title TYPE bds_propva
fu_resident TYPE stxbitmaps-resident
fu_autoheight TYPE stxbitmaps-autoheight
fu_bmcomp TYPE stxbitmaps-bmcomp
CHANGING fc_docid TYPE stxbitmaps-docid
fc_resolution TYPE stxbitmaps-resolution
fc_stats
fc_messg.

DATA: lcl_bds_object TYPE REF TO cl_bds_document_set.

DATA: ld_bytecount TYPE i,


ld_width_tw TYPE stxbitmaps-widthtw,
ld_height_tw TYPE stxbitmaps-heighttw,
ld_width_pix TYPE stxbitmaps-widthpix,
ld_height_pix TYPE stxbitmaps-heightpix,
ld_bds_bytecount TYPE i,
ld_object_key TYPE sbdst_object_key,
ld_tabname TYPE ddobjname.

DATA: lt_bds_content TYPE sbdst_content,


lt_bds_components TYPE sbdst_components,
lt_bds_properties TYPE sbdst_properties,
lt_bds_signature TYPE sbdst_signature.
https://belajarabap.wordpress.com
DATA: lx_bds_components TYPE LINE OF sbdst_components,
lx_bds_signature TYPE LINE OF sbdst_signature,
lx_stxbitmaps TYPE stxbitmaps,
lx_bds_properties TYPE LINE OF sbdst_properties.

* BDS handling
CONSTANTS:
lc_bds_classname TYPE sbdst_classname VALUE 'DEVC_STXD_BITMAP',
lc_bds_classtype TYPE sbdst_classtype VALUE 'OT', "others
lc_bds_mimetype TYPE bds_mimetp VALUE 'application/octet-stream',

7
lc_bds_original TYPE sbdst_doc_var_tg VALUE 'OR'.

* Enqueue
PERFORM enqueue_graphic USING fu_object
fu_name
fu_id
fu_btype
CHANGING fc_stats
fc_messg.
IF fc_stats = 'E'.
RETURN.
ENDIF.

* Bitmap conversion
CALL FUNCTION 'SAPSCRIPT_CONVERT_BITMAP_BDS'
EXPORTING
color = 'X'
format = fu_format
resident = fu_resident
bitmap_bytecount = ld_bytecount
compress_bitmap = fu_bmcomp
IMPORTING
width_tw = ld_width_tw
height_tw = ld_height_tw
width_pix = ld_width_pix
height_pix = ld_height_pix
dpi = fc_resolution
bds_bytecount = ld_bds_bytecount
TABLES
bitmap_file = fu_blob
bitmap_file_bds = lt_bds_content
EXCEPTIONS
format_not_supported = 1
no_bmp_file = 2
bmperr_invalid_format = 3 https://belajarabap.wordpress.com
bmperr_no_colortable = 4
bmperr_unsup_compression = 5
bmperr_corrupt_rle_data = 6
OTHERS = 7.

IF sy-subrc <> 0.
fc_stats = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO fc_messg.

8
PERFORM dequeue_graphic USING fu_object
fu_name
fu_id
fu_btype.
RETURN.
ENDIF.

* Save bitmap in BDS


CREATE OBJECT lcl_bds_object.

lx_bds_components-doc_count = '1'.
lx_bds_components-comp_count = '1'.
lx_bds_components-mimetype = lc_bds_mimetype.
lx_bds_components-comp_size = ld_bds_bytecount.
APPEND lx_bds_components TO lt_bds_components.

IF fc_docid IS INITIAL. "graphic is new

lx_bds_signature-doc_count = '1'.
APPEND lx_bds_signature TO lt_bds_signature.

CALL METHOD lcl_bds_object->create_with_table


EXPORTING
classname = lc_bds_classname
classtype = lc_bds_classtype
components = lt_bds_components
content = lt_bds_content
CHANGING
signature = lt_bds_signature
object_key = ld_object_key
EXCEPTIONS
OTHERS = 1.

IF sy-subrc <> 0.
fc_stats = 'E'. https://belajarabap.wordpress.com
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO fc_messg.
PERFORM dequeue_graphic USING fu_object
fu_name
fu_id
fu_btype.
RETURN.
ENDIF.

9
READ TABLE lt_bds_signature INDEX 1 INTO lx_bds_signature
TRANSPORTING doc_id.

IF sy-subrc = 0.
fc_docid = lx_bds_signature-doc_id.
ELSE.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO fc_messg.
fc_stats = 'E'.
PERFORM dequeue_graphic USING fu_object
fu_name
fu_id
fu_btype.
RETURN.
ENDIF.

ELSE. " graphic already exists

*read object_key for faster access


CLEAR ld_object_key.
SELECT SINGLE * FROM stxbitmaps INTO lx_stxbitmaps
WHERE tdobject = fu_object
AND tdid = fu_id
AND tdname = fu_name
AND tdbtype = fu_btype.

SELECT SINGLE tabname FROM bds_locl INTO ld_tabname


WHERE classname = lc_bds_classname
AND classtype = lc_bds_classtype.

IF sy-subrc = 0.
SELECT SINGLE object_key FROM (ld_tabname) INTO ld_object_key
WHERE loio_id = lx_stxbitmaps-docid+10(32)
AND classname = lc_bds_classname https://belajarabap.wordpress.com
AND classtype = lc_bds_classtype.
ENDIF.

10
*read object_key end
CALL METHOD lcl_bds_object->update_with_table
EXPORTING
classname = lc_bds_classname
classtype = lc_bds_classtype
object_key = ld_object_key
doc_id = fc_docid
doc_ver_no = '1'
doc_var_id = '1'
CHANGING
components = lt_bds_components
content = lt_bds_content
EXCEPTIONS
nothing_found = 1
OTHERS = 2.

IF sy-subrc = 1. " inconsistency STXBITMAPS # BDS; repeat check in

lx_bds_signature-doc_count = '1'.
APPEND lx_bds_signature TO lt_bds_signature.

CALL METHOD lcl_bds_object->create_with_table


EXPORTING
classname = lc_bds_classname
classtype = lc_bds_classtype
components = lt_bds_components
content = lt_bds_content
CHANGING
signature = lt_bds_signature
object_key = ld_object_key
EXCEPTIONS
OTHERS = 1.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno https://belajarabap.wordpress.com
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO fc_messg.
fc_stats = 'E'.
PERFORM dequeue_graphic USING fu_object
fu_name
fu_id
fu_btype.
RETURN.
ENDIF.

11
READ TABLE lt_bds_signature INDEX 1 INTO lx_bds_signature
TRANSPORTING doc_id.
IF sy-subrc = 0.
fc_docid = lx_bds_signature-doc_id.
ELSE.
fc_stats = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO fc_messg.
PERFORM dequeue_graphic USING fu_object
fu_name
fu_id
fu_btype.
RETURN.
ENDIF.

ELSEIF sy-subrc = 2.
fc_stats = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO fc_messg.
PERFORM dequeue_graphic USING fu_object
fu_name
fu_id
fu_btype.
RETURN.
ENDIF.
ENDIF.

* Save bitmap header in STXBITPMAPS


lx_stxbitmaps-tdname = fu_name.
lx_stxbitmaps-tdobject = fu_object.
lx_stxbitmaps-tdid = fu_id.
lx_stxbitmaps-tdbtype = fu_btype.
lx_stxbitmaps-docid = fc_docid. https://belajarabap.wordpress.com
lx_stxbitmaps-widthpix = ld_width_pix.
lx_stxbitmaps-heightpix = ld_height_pix.
lx_stxbitmaps-widthtw = ld_width_tw.
lx_stxbitmaps-heighttw = ld_height_tw.
lx_stxbitmaps-resolution = fc_resolution.
lx_stxbitmaps-resident = fu_resident.
lx_stxbitmaps-autoheight = fu_autoheight.
lx_stxbitmaps-bmcomp = fu_bmcomp.
INSERT into stxbitmaps values lx_stxbitmaps.

12
IF sy-subrc <> 0.
UPDATE stxbitmaps FROM lx_stxbitmaps.
IF sy-subrc <> 0.
fc_stats = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO fc_messg.
RETURN.
ENDIF.
ENDIF.

* Set description in BDS attributes


lx_bds_properties-prop_name = 'DESCRIPTION'.
lx_bds_properties-prop_value = fu_title.
APPEND lx_bds_properties TO lt_bds_properties.

CALL METHOD lcl_bds_object->change_properties


EXPORTING
classname = lc_bds_classname
classtype = lc_bds_classtype
object_key = ld_object_key
doc_id = fc_docid
doc_ver_no = '1'
doc_var_id = '1'
CHANGING
properties = lt_bds_properties
EXCEPTIONS
OTHERS = 1.

PERFORM dequeue_graphic USING fu_object


fu_name
fu_id
fu_btype.

ENDFORM. " F_IMPORT_IMAGE https://belajarabap.wordpress.com

13
*&---------------------------------------------------------------------*
*& Form enqueue_graphic
*&---------------------------------------------------------------------*
FORM enqueue_graphic USING fu_object
fu_name
fu_id
fu_btype
CHANGING fc_stats
fc_messg.

CALL FUNCTION 'ENQUEUE_ESSGRABDS'


EXPORTING
tdobject = fu_object
tdname = fu_name
tdid = fu_id
tdbtype = fu_btype
EXCEPTIONS
foreign_lock = 1
OTHERS = 2.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO fc_messg.
fc_stats = 'E'.
ENDIF.

ENDFORM. "ENQUEUE_GRAPHIC
*&---------------------------------------------------------------------*
*& Form dequeue_graphic
*&---------------------------------------------------------------------*
FORM dequeue_graphic USING fu_object
fu_name
fu_id
fu_btype. https://belajarabap.wordpress.com

CALL FUNCTION 'DEQUEUE_ESSGRABDS'


EXPORTING
tdobject = fu_object
tdname = fu_name
tdid = fu_id
tdbtype = fu_btype.

ENDFORM. "DEQUEUE_GRAPHIC

14

You might also like