Module bus.extra

Class QrConfig

java.lang.Object
org.miaixz.bus.extra.qrcode.QrConfig

public class QrConfig extends Object
QR code configuration.
Since:
Java 17+
Author:
Kimi Liu
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Integer
    Background color, defaults to white, null means transparent.
    protected Charset
    Character encoding.
    protected com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
    Error correction level.
    protected Integer
    Foreground color (QR code color).
    protected com.google.zxing.BarcodeFormat
    Format of the generated code, defaults to QR code.
    protected int
    Height.
    protected Image
    Logo in the QR code.
    protected double
    Corner radius of the QR code logo, 0-1, as a ratio of width/height.
    protected Integer
    Margin, 0-4.
    protected Integer
    Sets the amount of information in the QR code, can be set to an integer from 0-40.
    protected int
    Scaling ratio for the logo in the QR code, e.g., 5 means 1/5 of the smaller dimension (width or height).
    protected com.google.zxing.datamatrix.encoder.SymbolShapeHint
    Symbol shape for DATA_MATRIX.
    protected int
    Width.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor, default width and height are 300.
    QrConfig(int width, int height)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets the background color.
    Gets the character encoding.
    com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
    Gets the error correction level.
    Gets the foreground color.
    com.google.zxing.BarcodeFormat
    Gets the code format.
    int
    Gets the height.
    Gets the logo in the QR code.
    double
    Gets the corner radius of the QR code logo, from 0 to 1, as a ratio of width/height.
    Gets the margin.
    Gets the amount of information in the QR code.
    int
    Gets the scaling ratio of the logo in the QR code, e.g., 5 means 1/5 of the smaller dimension (width or height).
    int
    Gets the width.
    static QrConfig
    of()
    Creates a QrConfig.
    static QrConfig
    of(int width, int height)
    Creates a QrConfig.
    setBackColor(Color backColor)
    Sets the background color, e.g., Color.BLUE.
    Sets the character encoding.
    void
    setEnableEci(boolean enableEci)
    Enables or disables ECI encoding.
    setErrorCorrection(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel errorCorrection)
    Sets the error correction level.
    setForeColor(Color foreColor)
    Sets the foreground color, e.g., Color.BLUE.getRGB().
    setFormat(com.google.zxing.BarcodeFormat format)
    Sets the code format, defaults to QR code.
    setHeight(int height)
    Sets the height.
    setImg(byte[] imageBytes)
    Sets the logo file for the QR code from a byte array.
    Sets the logo for the QR code.
    setImg(File imgFile)
    Sets the logo file for the QR code.
    setImg(String imgPath)
    Sets the logo file for the QR code.
    setImgRound(double imgRound)
    Sets the corner radius of the QR code logo, from 0 to 1, as a ratio of width/height.
    Sets the margin.
    setQrVersion(Integer qrVersion)
    Sets the amount of information in the QR code.
    setRatio(int ratio)
    Sets the scaling ratio of the logo in the QR code, e.g., 5 means 1/5 of the smaller dimension (width or height).
    setShapeHint(com.google.zxing.datamatrix.encoder.SymbolShapeHint shapeHint)
    Sets the symbol shape for DATA_MATRIX.
    setWidth(int width)
    Sets the width.
    HashMap<com.google.zxing.EncodeHintType,Object>
    Converts to Zxing's QR code configuration.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • width

      protected int width
      Width.
    • height

      protected int height
      Height.
    • foreColor

      protected Integer foreColor
      Foreground color (QR code color).
    • backColor

      protected Integer backColor
      Background color, defaults to white, null means transparent.
    • margin

      protected Integer margin
      Margin, 0-4.
    • qrVersion

      protected Integer qrVersion
      Sets the amount of information in the QR code, can be set to an integer from 0-40.
    • errorCorrection

      protected com.google.zxing.qrcode.decoder.ErrorCorrectionLevel errorCorrection
      Error correction level.
    • charset

      protected Charset charset
      Character encoding.
    • img

      protected Image img
      Logo in the QR code.
    • imgRound

      protected double imgRound
      Corner radius of the QR code logo, 0-1, as a ratio of width/height.
    • ratio

      protected int ratio
      Scaling ratio for the logo in the QR code, e.g., 5 means 1/5 of the smaller dimension (width or height).
    • shapeHint

      protected com.google.zxing.datamatrix.encoder.SymbolShapeHint shapeHint
      Symbol shape for DATA_MATRIX.
    • format

      protected com.google.zxing.BarcodeFormat format
      Format of the generated code, defaults to QR code.
  • Constructor Details

    • QrConfig

      public QrConfig()
      Constructor, default width and height are 300.
    • QrConfig

      public QrConfig(int width, int height)
      Constructor.
      Parameters:
      width - Width.
      height - Height.
  • Method Details

    • of

      public static QrConfig of()
      Creates a QrConfig.
      Returns:
      A new QrConfig instance.
    • of

      public static QrConfig of(int width, int height)
      Creates a QrConfig.
      Parameters:
      width - Width.
      height - Height.
      Returns:
      A new QrConfig instance.
    • getWidth

      public int getWidth()
      Gets the width.
      Returns:
      The width.
    • setWidth

      public QrConfig setWidth(int width)
      Sets the width.
      Parameters:
      width - The width.
      Returns:
      this
    • getHeight

      public int getHeight()
      Gets the height.
      Returns:
      The height.
    • setHeight

      public QrConfig setHeight(int height)
      Sets the height.
      Parameters:
      height - The height.
      Returns:
      this
    • getForeColor

      public Integer getForeColor()
      Gets the foreground color.
      Returns:
      The foreground color.
    • setForeColor

      public QrConfig setForeColor(Color foreColor)
      Sets the foreground color, e.g., Color.BLUE.getRGB().
      Parameters:
      foreColor - The foreground color.
      Returns:
      this
    • getBackColor

      public Integer getBackColor()
      Gets the background color.
      Returns:
      The background color.
    • setBackColor

      public QrConfig setBackColor(Color backColor)
      Sets the background color, e.g., Color.BLUE. Null means transparent background.
      Parameters:
      backColor - The background color.
      Returns:
      this
    • getMargin

      public Integer getMargin()
      Gets the margin.
      Returns:
      The margin.
    • setMargin

      public QrConfig setMargin(Integer margin)
      Sets the margin.
      Parameters:
      margin - The margin.
      Returns:
      this
    • getQrVersion

      public Integer getQrVersion()
      Gets the amount of information in the QR code. Can be an integer from 0-40. The QR code image will also change based on the qrVersion. 0 means it changes automatically based on the input information.
      Returns:
      The amount of information in the QR code.
    • setQrVersion

      public QrConfig setQrVersion(Integer qrVersion)
      Sets the amount of information in the QR code. Can be an integer from 0-40. The QR code image will also change based on the qrVersion. 0 means it changes automatically based on the input information.
      Parameters:
      qrVersion - The amount of information in the QR code.
      Returns:
      this
    • getErrorCorrection

      public com.google.zxing.qrcode.decoder.ErrorCorrectionLevel getErrorCorrection()
      Gets the error correction level.
      Returns:
      The error correction level.
    • setErrorCorrection

      public QrConfig setErrorCorrection(com.google.zxing.qrcode.decoder.ErrorCorrectionLevel errorCorrection)
      Sets the error correction level.
      Parameters:
      errorCorrection - The error correction level.
      Returns:
      this
    • setEnableEci

      public void setEnableEci(boolean enableEci)
      Enables or disables ECI encoding. If enableEci=false, the QR code will not contain ECI information, meaning the charset character encoding is set to null. This is best for English characters. If enableEci=true, the QR code will contain ECI information, set according to the charset encoding. This is best for including Chinese characters, otherwise they will be garbled.

      QR code encoding can be in ECI mode or non-ECI mode. In ECI mode, the first byte is used as an encoding identifier, whereas in non-ECI mode, it is the data stream directly. ECI mode is actually a better solution, as it allows for different encoding methods to be used during decoding based on the identifier. Non-ECI mode can only be processed in a single, unified way. However, some devices do not support ECI mode, which can lead to recognition failures. Using barcode scanners/guns might result in a "\000026" character. Using a mobile phone or other QR code parsing software will not have this issue.

      The ECI encoding table shows that UTF-8 corresponds to "\000026" (or the number 22).

      In summary: If the QR code content is entirely characters without Chinese, there is no need to encode using UTF-8. Encoding is only necessary for special symbols like Chinese.

      Parameters:
      enableEci - Whether to enable ECI.
      See Also:
      • EncodeHintType.PDF417_AUTO_ECI
    • getCharset

      public Charset getCharset()
      Gets the character encoding.
      Returns:
      The character encoding.
    • setCharset

      public QrConfig setCharset(Charset charset)
      Sets the character encoding.
      Parameters:
      charset - The character encoding.
      Returns:
      this
    • getImg

      public Image getImg()
      Gets the logo in the QR code.
      Returns:
      The logo image.
    • setImg

      public QrConfig setImg(String imgPath)
      Sets the logo file for the QR code.
      Parameters:
      imgPath - The path to the logo in the QR code.
      Returns:
      this
    • setImg

      public QrConfig setImg(byte[] imageBytes)
      Sets the logo file for the QR code from a byte array.
      Parameters:
      imageBytes - The byte array representation of the logo image.
      Returns:
      this
    • setImg

      public QrConfig setImg(File imgFile)
      Sets the logo file for the QR code.
      Parameters:
      imgFile - The logo file for the QR code.
      Returns:
      this
    • setImg

      public QrConfig setImg(Image img)
      Sets the logo for the QR code.
      Parameters:
      img - The logo for the QR code.
      Returns:
      this
    • getImgRound

      public double getImgRound()
      Gets the corner radius of the QR code logo, from 0 to 1, as a ratio of width/height.
      Returns:
      The corner radius of the QR code logo.
    • setImgRound

      public QrConfig setImgRound(double imgRound)
      Sets the corner radius of the QR code logo, from 0 to 1, as a ratio of width/height.
      Parameters:
      imgRound - The corner radius of the QR code logo.
      Returns:
      this
    • getRatio

      public int getRatio()
      Gets the scaling ratio of the logo in the QR code, e.g., 5 means 1/5 of the smaller dimension (width or height).
      Returns:
      The scaling ratio of the logo in the QR code.
    • setRatio

      public QrConfig setRatio(int ratio)
      Sets the scaling ratio of the logo in the QR code, e.g., 5 means 1/5 of the smaller dimension (width or height).
      Parameters:
      ratio - The scaling ratio of the logo in the QR code.
      Returns:
      this
    • setShapeHint

      public QrConfig setShapeHint(com.google.zxing.datamatrix.encoder.SymbolShapeHint shapeHint)
      Sets the symbol shape for DATA_MATRIX.
      Parameters:
      shapeHint - The symbol shape for DATA_MATRIX.
      Returns:
      this
    • getFormat

      public com.google.zxing.BarcodeFormat getFormat()
      Gets the code format.
      Returns:
      The code format, defaults to QR code.
    • setFormat

      public QrConfig setFormat(com.google.zxing.BarcodeFormat format)
      Sets the code format, defaults to QR code.
      Parameters:
      format - The code format.
      Returns:
      this
    • toHints

      public HashMap<com.google.zxing.EncodeHintType,Object> toHints()
      Converts to Zxing's QR code configuration.
      Returns:
      The configuration.