Module bus.extra

Class QrCodeKit

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

public class QrCodeKit extends Object
QR code utility class based on Zxing, supporting:
  • QR code generation and recognition, see BarcodeFormat.QR_CODE
  • Barcode generation and recognition, see BarcodeFormat.CODE_39 and many other standard formats
Since:
Java 17+
Author:
Kimi Liu
  • Field Details

  • Constructor Details

    • QrCodeKit

      public QrCodeKit()
  • Method Details

    • generateAsBase64DataUri

      public static String generateAsBase64DataUri(String content, QrConfig qrConfig, String imageType)
      Generates a Base64 encoded QR code in String format.

      The output format is: data:image/[type];base64,[data]

      Parameters:
      content - The content to encode in the QR code.
      qrConfig - QR code configuration, including width, height, margin, color, etc.
      imageType - The image type (file extension), see QR_TYPE_SVG, QR_TYPE_TXT, ImageKit.
      Returns:
      The Base64 encoded image string.
    • generatePng

      public static byte[] generatePng(String content, int width, int height)
      Generates a PNG format QR code image as a byte array.
      Parameters:
      content - The content to encode in the QR code.
      width - The width of the QR code image.
      height - The height of the QR code image.
      Returns:
      The QR code image as a byte array.
    • generatePng

      public static byte[] generatePng(String content, QrConfig config)
      Generates a PNG format QR code image as a byte array with custom configuration.
      Parameters:
      content - The content to encode in the QR code.
      config - QR code configuration, including width, height, margin, color, etc.
      Returns:
      The QR code image as a byte array.
    • generate

      public static File generate(String content, int width, int height, File targetFile)
      Generates a QR code to a file. The image format depends on the file's extension.
      Parameters:
      content - The text content to encode.
      width - The width (in pixels for general images or SVG, in character units for Ascii Art).
      height - The height (in pixels for general images or SVG, in character units for Ascii Art).
      targetFile - The target file, whose extension determines the output format.
      Returns:
      The target file.
    • generate

      public static File generate(String content, QrConfig config, File targetFile)
      Generates a QR code to a file with custom configuration. The image format depends on the file's extension.
      Parameters:
      content - The text content to encode.
      config - QR code configuration, including width, height, margin, color, etc.
      targetFile - The target file, whose extension determines the output format.
      Returns:
      The target file.
    • generate

      public static void generate(String content, int width, int height, String imageType, OutputStream out)
      Generates a QR code to an output stream.
      Parameters:
      content - The text content to encode.
      width - The width (in pixels for general images or SVG, in character units for Ascii Art).
      height - The height (in pixels for general images or SVG, in character units for Ascii Art).
      imageType - The image type (file extension), see QR_TYPE_SVG, QR_TYPE_TXT, ImageKit.
      out - The target output stream.
    • generate

      public static void generate(String content, QrConfig config, String imageType, OutputStream out)
      Generates a QR code to an output stream with custom configuration.
      Parameters:
      content - The text content to encode.
      config - QR code configuration, including width, height, margin, color, etc.
      imageType - The image type (file extension), see ImageKit.
      out - The target output stream.
    • generate

      public static BufferedImage generate(String content, int width, int height)
      Generates a QR code image (black and white).
      Parameters:
      content - The text content to encode.
      width - The width of the QR code image.
      height - The height of the QR code image.
      Returns:
      The generated QR code image.
    • generate

      public static BufferedImage generate(String content, QrConfig config)
      Generates a QR code or barcode image with custom configuration. The image configuration in QrConfig is only effective for QR codes.
      Parameters:
      content - The text content to encode.
      config - QR code configuration, including width, height, margin, color, etc.
      Returns:
      The generated QR code image (black and white).
    • encode

      public static com.google.zxing.common.BitMatrix encode(CharSequence content, QrConfig config)
      Encodes text content into a barcode or QR code BitMatrix.
      Parameters:
      content - The text content to encode.
      config - QR code configuration, including width, height, margin, color, format, etc.
      Returns:
      The BitMatrix representing the encoded content.
    • decode

      public static String decode(InputStream qrCodeInputstream)
      Decodes a QR code or barcode image from an input stream into text.
      Parameters:
      qrCodeInputstream - The input stream of the QR code image.
      Returns:
      The decoded text.
    • decode

      public static String decode(File qrCodeFile)
      Decodes a QR code or barcode image from a file into text.
      Parameters:
      qrCodeFile - The file containing the QR code image.
      Returns:
      The decoded text.
    • decode

      public static String decode(Image image)
      Decodes a QR code or barcode image into text.
      Parameters:
      image - The Image object of the QR code.
      Returns:
      The decoded text.
    • decode

      public static String decode(Image image, boolean isTryHarder, boolean isPureBarcode)
      Decodes a QR code or barcode image into text. This method attempts to parse using both HybridBinarizer and GlobalHistogramBinarizer modes. Note that some QR codes without a logo may fail to parse in PureBarcode mode, in which case this option should be set to false.
      Parameters:
      image - The Image object of the QR code.
      isTryHarder - Whether to optimize for accuracy (try harder).
      isPureBarcode - Whether to use pure barcode mode (for scanning QR codes with logos, set to true).
      Returns:
      The decoded text.
    • decode

      public static String decode(Image image, Map<com.google.zxing.DecodeHintType,Object> hints)
      Decodes a QR code or barcode image into text with custom decoding hints. This method attempts to parse using both HybridBinarizer and GlobalHistogramBinarizer modes.
      Parameters:
      image - The Image object of the QR code.
      hints - Custom scanning configuration, including algorithm, encoding, complex mode, etc.
      Returns:
      The decoded text.
    • toImage

      public static BufferedImage toImage(com.google.zxing.common.BitMatrix matrix, int foreColor, Integer backColor)
      Converts a BitMatrix to a BufferedImage.
      Parameters:
      matrix - The BitMatrix to convert.
      foreColor - The foreground color.
      backColor - The background color (null for transparent background).
      Returns:
      The converted BufferedImage.
    • generateAsSvg

      public static String generateAsSvg(String content, QrConfig qrConfig)
      Generates an SVG vector graphic of a QR code.
      Parameters:
      content - The content to encode in the QR code.
      qrConfig - QR code configuration, including width, height, margin, color, etc.
      Returns:
      The SVG vector graphic as a string.
    • toSVG

      public static String toSVG(com.google.zxing.common.BitMatrix matrix, QrConfig config)
      Converts a BitMatrix to an SVG string.
      Parameters:
      matrix - The BitMatrix to convert.
      config - The QrConfig for SVG rendering.
      Returns:
      The SVG vector graphic as a string.
    • generateAsAsciiArt

      public static String generateAsAsciiArt(String content, QrConfig qrConfig)
      Generates an ASCII Art character representation of a QR code.
      Parameters:
      content - The content to encode in the QR code.
      qrConfig - QR code configuration, only width, height, and margin are effective.
      Returns:
      The ASCII Art character representation of the QR code.
    • toAsciiArt

      public static String toAsciiArt(com.google.zxing.common.BitMatrix matrix, QrConfig config)
      Converts a BitMatrix to an ASCII Art character representation of a QR code.
      Parameters:
      matrix - The BitMatrix to convert.
      config - The QrConfig for ASCII Art rendering.
      Returns:
      The ASCII Art character representation of the QR code.