Create QR Codes in Oracle APEX Using APEX_BARCODE.GET_QRCODE_PNG Function

The GET_QRCODE_PNG function is a powerful tool within the APEX_BARCODE package for generating Quick Response (QR) codes.

This function transforms a text string or URL into a 2D matrix barcode that is easily scannable by mobile devices.

It provides extensive customization options, including error correction levels and quiet zone adjustments, to ensure readability.

The resulting output is a Binary Large Object (BLOB) formatted as a PNG image, ready for display or download.

Displaying QR Code in Oracle APEX.

APEX_BARCODE.GET_QRCODE_PNG Syntax

The following PL/SQL syntax outlines how to invoke the GET_QRCODE_PNG function in your application logic.

APEX_BARCODE.GET_QRCODE_PNG (
    p_value             IN VARCHAR2,
    p_scale             IN NUMBER         DEFAULT c_default_scale,
    p_quiet             IN NUMBER         DEFAULT c_default_quiet,
    p_eclevel           IN t_eclevel_type DEFAULT c_default_eclevel,
    p_foreground_color  IN VARCHAR2       DEFAULT c_default_foreground_color,
    p_background_color  IN VARCHAR2       DEFAULT NULL )
    RETURN BLOB;

Parameters Configuration

p_value

The p_value parameter is the core data string that will be encoded into the QR pattern.

Common uses include website URLs, Wi-Fi credentials, or vCard contact information.

p_scale

Use the p_scale parameter to increase the resolution of the generated PNG image.

It accepts an integer between 1 and 10, with the default being 1.

p_quiet

The p_quiet parameter defines the "quiet zone" or blank margin surrounding the QR code.

A sufficient quiet zone is critical for scanners to distinguish the code from unrelated background elements.

p_eclevel

This parameter sets the Error Correction Level, determining how much damage the code can sustain while remaining readable.

Higher levels add more redundancy to the code, making the image denser but more robust.

The available constants for this parameter are:

  • c_eclevel_type_low: Restores approximately 7% of data bytes.
  • c_eclevel_type_medium: Restores approximately 15% of data bytes.
  • c_eclevel_type_quartile: Restores approximately 25% of data bytes.
  • c_eclevel_type_high: Restores approximately 30% of data bytes (Default).

p_foreground_color

This parameter specifies the color of the QR code's data modules (the dark squares).

It expects a hexadecimal color string, defaulting to standard black (#000000).

p_background_color

This parameter sets the color of the background behind the QR code modules.

Providing a NULL value renders the background transparent, which is useful for overlaying on colored regions.

Return Value

The function returns a single PNG image BLOB that encodes the specified value with the chosen settings.

APEX_BARCODE.GET_QRCODE_PNG Usage Example

The following PL/SQL block generates a QR code for a "mailto" link with a high error correction level and custom colors.

This example creates a larger image (scale 3) with a dark blue foreground and a white background for high contrast.

DECLARE
  l_qr_image blob;
BEGIN
  l_qr_image := apex_barcode.get_qrcode_png(
                  p_value            => 'mailto:support@example.com?subject=Help',
                  p_scale            => 3,
                  p_quiet            => 2,
                  p_eclevel          => apex_barcode.c_eclevel_type_high,
                  p_foreground_color => '#003366', -- Dark Blue
                  p_background_color => '#FFFFFF'  -- White
                );
END;

Displaying the QR Code in the Oracle APEX Region

To render this QR code dynamically on an APEX page, you must stream the BLOB to the browser via an Ajax request.

First, create an Application Process named RENDER_QRCODE with the Point set to "Ajax Callback".

Add the following PL/SQL code to the process to handle the generation and download headers.

DECLARE
  l_blob BLOB;
BEGIN
  -- Generate QR code using the 'x01' request value
  -- Using Medium error correction for a balance of size and reliability
  l_blob := apex_barcode.get_qrcode_png(
              p_value            => apex_application.g_x01,
              p_scale            => 4,
              p_quiet            => 2,
              p_eclevel          => apex_barcode.c_eclevel_type_medium,
              p_foreground_color => '#000000'
            );

  -- Initialize HTTP buffer
  sys.htp.init;

  -- specific MIME type for PNG
  sys.owa_util.mime_header('image/png', FALSE);

  -- Set content length
  sys.htp.p('Content-Length: ' || dbms_lob.getlength(l_blob));

  -- Close headers
  sys.owa_util.http_header_close;

  -- Download the file
  wpg_docload.download_file(l_blob);
END;

Next, add a "Static Content" region to your page where the QR code should appear.

Insert the following HTML code, using the APPLICATION_PROCESS syntax in the image source URL.

<div style="text-align: center; margin-top: 20px;">
  <h3>Scan to Visit Website</h3>
  <!-- Dynamically calls the RENDER_QRCODE process with a URL as the payload -->
  <img src="f?p=&APP_ID.:0:&SESSION.:APPLICATION_PROCESS=RENDER_QRCODE:NO::x01:[https://www.oracle.com](https://www.oracle.com)" 
       alt="Website QR Code" 
       title="Scan me!"
       style="box-shadow: 0 4px 8px rgba(0,0,0,0.1);" />
</div>

Output:

Displaying QR Code in Oracle APEX.

See also: Generating Barcodes in Oracle APEX

Vinish Kapoor
Vinish Kapoor

An Oracle ACE and software veteran with 25+ years of experience, passionate about AI and IT innovation.

guest

0 Comments
Oldest
Newest Most Voted