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.
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]
3. Create a new graphics and as shown below give the name as &W_NAME&. [Dynamic
variable]
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.
源码:
*&---------------------------------------------------------------------*
*& 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
=
l
_object
_key
doc
_id
=
p
_docid
doc
_ver
_no
=
'1'
doc
_var
_id
=
'1'
changing
components
=
l
_bds
_components
content
=
l
_bds
_content
exceptions
nothing
_found
=
1
others
=
2.
if
sy
-subrc
=
1.
" inconsistency STXBITMAPS - BDS; repeat check in
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
.
elseif
sy
-subrc
=
2.
perform
dequeue
_graphic
using
p
_object
p
_name
p
_id
p
_btype
.
* message e285 with p_name 'BDS'.
endif
.
endif
.
* Save bitmap header in STXBITPMAPS
wa
_stxbitmaps
-tdname
=
p
_name
.
wa
_stxbitmaps
-tdobject
=
p
_object
.
wa
_stxbitmaps
-tdid
=
p
_id
.
wa
_stxbitmaps
-tdbtype
=
p
_btype
.
wa
_stxbitmaps
-docid
=
p
_docid
.
wa
_stxbitmaps
-widthpix
=
l
_width
_pix
.
wa
_stxbitmaps
-heightpix
=
l
_height
_pix
.
wa
_stxbitmaps
-widthtw
=
l
_width
_tw
.
wa
_stxbitmaps
-heighttw
=
l
_height
_tw
.
wa
_stxbitmaps
-resolution
=
p
_resolution
.
wa
_stxbitmaps
-resident
=
p
_resident
.
wa
_stxbitmaps
-autoheight
=
p
_autoheight
.
wa
_stxbitmaps
-bmcomp
=
p
_bmcomp
.
insert
into
stxbitmaps
values
wa
_stxbitmaps
.
if
sy
-subrc
<
>
0.
update
stxbitmaps
from
wa
_stxbitmaps
.
if
sy
-subrc
<
>
0.
* message e285 with p_name 'STXBITMAPS'.
endif
.
endif
.
* Set description in BDS attributes
wa
_bds
_properties
-prop
_name
=
'DESCRIPTION'
.
wa
_bds
_properties
-prop
_value
=
p
_title
.
append
wa
_bds
_properties
to
l
_bds
_properties
.
call method
l
_bds
_object
-
>change
_properties
exporting
classname
=
c
_bds
_classname
classtype
=
c
_bds
_classtype
object
_key
=
l
_object
_key
doc
_id
=
p
_docid
doc
_ver
_no
=
'1'
doc
_var
_id
=
'1'
changing
properties
=
l
_bds
_properties
exceptions
others
=
1.
perform
dequeue
_graphic
using
p
_object
p
_name
p
_id
p
_btype
.
endform
.
*&---------------------------------------------------------------------*
*& Form ENQUEUE_GRAPHIC
*&---------------------------------------------------------------------*
* Enqueue of graphics stored in BDS
*----------------------------------------------------------------------*
form
enqueue
_graphic
using
p
_object
p
_name
p
_id
p
_btype
.
call function
'ENQUEUE_ESSGRABDS'
exporting
* MODE_STXBITMAPS = 'E'
tdobject
=
p
_object
tdname
=
p
_name
tdid
=
p
_id
tdbtype
=
p
_btype
* X_TDOBJECT = ' '
* X_TDNAME = ' '
* X_TDID = ' '
* X_TDBTYPE = ' '
* _SCOPE = '2'
* _WAIT = ' '
* _COLLECT = ' '
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
raising
enqueue
_failed
.
endif
.
endform
.
" ENQUEUE_GRAPHIC
*&---------------------------------------------------------------------*
*& Form DEQUEUE_GRAPHIC
*&---------------------------------------------------------------------*
* Dequeue of graphics stored in BDS
*----------------------------------------------------------------------*
form
dequeue
_graphic
using
p
_object
p
_name
p
_id
p
_btype
.
call function
'DEQUEUE_ESSGRABDS'
exporting
* MODE_STXBITMAPS = 'E'
* X_TDOBJECT = ' '
* X_TDNAME = ' '
* X_TDID = ' '
* X_TDBTYPE = ' '
* _SCOPE = '3'
* _SYNCHRON = ' '
* _COLLECT = ' '
tdobject
=
p
_object
tdname
=
p
_name
tdid
=
p
_id
tdbtype
=
p
_btype
.
endform
.
" DEQUEUE_GRAPHIC