1, 二维码简介
二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型。
2, 例子代码
网上看到一个关于二维码的abap程序,重新整理了一下,分享给出来,供大家参考。
注意,这个例子需要能上google.com,不能上就不能显示二维码。。。。
完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 |
REPORT ztest_qr_code. 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 '9000'. "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 |
程序中需要创建一个屏幕9000,流逻辑如下:
1 2 3 4 5 |
PROCESS BEFORE OUTPUT. MODULE STATUS_9000. * PROCESS AFTER INPUT. MODULE USER_COMMAND_9000. |
9000屏幕上创建一个名字为PICTURECONTROL的custom control
还要创建一个status 9000,如下:
最后Tcode smartforms 创建一个供二维码显示的smartform ‘ZQR_TEST'
Form interface中创建一个输入参数 W_NAME
Main window下创建一个Graphic,然后保存。
3, 测试显示
选择画面输入需要生成二维码的内容,二维码的长宽,以及显示的模式,是abap显示,还是smartform显示,然后运行。
abap程序显示如下:
Smartform显示如下:
以上。
740以后的版本就不用这么麻烦了,可以直接在字体里配置二维码.
生成二维码的webAPI让公司里负责web开发的同事写一个就好了,smartform里用到的图片要求是256色bmp格式,要不然,就会出现,像文章中灰底的情况.
以下是我自己写的python的Django框架的webAPI
from django.http import HttpResponse
from django.shortcuts import render
import qrcode
from django.utils.six import BytesIO
import os
from PIL import Image
import json
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
2020-04-22 10:17 上午def generate_code(request):
width = int(request.GET['width'])
height = int(request.GET['height'])
if request.method == 'POST':
postbody = request.body
json_result = json.loads(postbody)
data = json_result['data']
img = qrcode.make(data)
img = img.resize((width, height))
img = img.convert('P')
buf = BytesIO()
img.save(buf, format='bmp')
image_stream = buf.getvalue()
response = HttpResponse(image_stream, content_type='image/bmp')
return response
else:
return HttpResponse('error')
怎么我的帐号也是石头?不管咋样,这个写的不错!感谢一下!
2017-08-30 2:34 上午我怎么感觉我认识你丫的呢。。。别装。。。
2017-08-31 8:19 上午