Class WQrCode

  • Direct Known Subclasses:
    TotpQrCode

    public class WQrCode
    extends WPaintedWidget
    A widget representing a QR code.

    This widget allows to generate QR code from a string.

    If you wish to alter the look of the QR code (to add a logo in the middle for instance), you can override paintEvent. However, you will still need to call the paintEvent() to generate the QR code itself.

    The content that is encoded is taken as-is. If the purpose of your QR code requires special formatting, the formatted string needs to be supplied to the constructor.

    By default, the generated QR code will use the lowest possible error correction, and a square size of 5 pixels.

    Usage example:

    
     String website = "https://webtoolkit.eu";
     WApplication app = WApplication.getInstance();
     app.getRoot().addWidget(new WQrCode(website));
    
     
    • Method Detail

      • setErrorCorrectionLevel

        public void setErrorCorrectionLevel​(WQrCode.ErrorCorrectionLevel ecl)
        Set the error correction level of the QR code.

        Increases the amount of information that can be lost before altering the encoded message. A higher level of error correcting code, makes the QR code more robust to changes.

        Increasing the error correction level also increases the amount of data needed to encode the message, resulting in a visually bigger QR code than one with a lower error correcting level.

        By default, LOW is used.

        See Also:
        setMessage(String message)
      • setMessage

        public void setMessage​(java.lang.String message)
        Set the message of the QR code.

        This sets the message carried by the QR code. There is a limit to the size of the message which depends on many factors. The most important one is the error correction level. Higher error correction level diminish the maximum size the message can have. Roughly speaking when using the highest level of error correction, the maximum number of allowed content is in the area of 1Kb, whereas for the lowest level of error correction, this is around 3Kb.

        The longer the message is, the more data needs to be encoded, and thus the bigger the resulting QR code becomes.

        If the message is to long, the QR code will not be generated.

        See Also:
        setErrorCorrectionLevel(WQrCode.ErrorCorrectionLevel ecl), isError()
      • setSquareSize

        public void setSquareSize​(double size)
        Set the size of the dots composing the QR code.

        Sets the size (in pixels) that each dot of the QR code will have. A single square of the QR code can be seen in any corner of the QR code. There a reference visual is always displayed that contains exactly seven dots in width and height.

        This allows the application to correctly resize the QR code.

      • getSquareSize

        public double getSquareSize()
        Returns the size of the squares composing the QR code.

        See Also:
        setSquareSize(double size)
      • isError

        public boolean isError()
        Returns whether an error stopped the generation of the QR code.

        Returns true if the QR code could not be generated due to an error.

        In case the QR code was not generated, it's size will be set to 0 and the QR code will not be painted.

      • setBrush

        public void setBrush​(WBrush brush)
        Sets the brush with which the QR code is painted.

        This allows for the color of the QR code to be changed.

      • paintEvent

        protected void paintEvent​(WPaintDevice paintDevice)
        Description copied from class: WPaintedWidget
        Paints the widget.

        You should reimplement this method to paint the contents of the widget, using the given paintDevice.

        Specified by:
        paintEvent in class WPaintedWidget