java.lang.Object
org.miaixz.bus.extra.qrcode.QrCodeKit
QR code utility class based on Zxing, supporting:
- QR code generation and recognition, see
BarcodeFormat.QR_CODE - Barcode generation and recognition, see
BarcodeFormat.CODE_39and many other standard formats
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringDecodes a QR code or barcode image into text.static StringDecodes a QR code or barcode image into text.static StringDecodes a QR code or barcode image into text with custom decoding hints.static StringDecodes a QR code or barcode image from a file into text.static Stringdecode(InputStream qrCodeInputstream) Decodes a QR code or barcode image from an input stream into text.static com.google.zxing.common.BitMatrixencode(CharSequence content, QrConfig config) Encodes text content into a barcode or QR codeBitMatrix.static BufferedImageGenerates a QR code image (black and white).static FileGenerates a QR code to a file.static voidgenerate(String content, int width, int height, String imageType, OutputStream out) Generates a QR code to an output stream.static BufferedImageGenerates a QR code or barcode image with custom configuration.static FileGenerates a QR code to a file with custom configuration.static voidgenerate(String content, QrConfig config, String imageType, OutputStream out) Generates a QR code to an output stream with custom configuration.static StringgenerateAsAsciiArt(String content, QrConfig qrConfig) Generates an ASCII Art character representation of a QR code.static StringgenerateAsBase64DataUri(String content, QrConfig qrConfig, String imageType) Generates a Base64 encoded QR code in String format.static StringgenerateAsSvg(String content, QrConfig qrConfig) Generates an SVG vector graphic of a QR code.static byte[]generatePng(String content, int width, int height) Generates a PNG format QR code image as a byte array.static byte[]generatePng(String content, QrConfig config) Generates a PNG format QR code image as a byte array with custom configuration.static StringtoAsciiArt(com.google.zxing.common.BitMatrix matrix, QrConfig config) Converts aBitMatrixto an ASCII Art character representation of a QR code.static BufferedImageConverts aBitMatrixto aBufferedImage.static StringConverts aBitMatrixto an SVG string.
-
Field Details
-
QR_TYPE_SVG
SVG vector graphic format.- See Also:
-
QR_TYPE_TXT
Ascii Art character text format.- See Also:
-
-
Constructor Details
-
QrCodeKit
public QrCodeKit()
-
-
Method Details
-
generateAsBase64DataUri
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), seeQR_TYPE_SVG,QR_TYPE_TXT,ImageKit.- Returns:
- The Base64 encoded image string.
-
generatePng
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
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
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
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), seeQR_TYPE_SVG,QR_TYPE_TXT,ImageKit.out- The target output stream.
-
generate
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), seeImageKit.out- The target output stream.
-
generate
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
Generates a QR code or barcode image with custom configuration. The image configuration inQrConfigis 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
Encodes text content into a barcode or QR codeBitMatrix.- Parameters:
content- The text content to encode.config- QR code configuration, including width, height, margin, color, format, etc.- Returns:
- The
BitMatrixrepresenting the encoded content.
-
decode
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
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
Decodes a QR code or barcode image into text.- Parameters:
image- TheImageobject of the QR code.- Returns:
- The decoded text.
-
decode
Decodes a QR code or barcode image into text. This method attempts to parse using bothHybridBinarizerandGlobalHistogramBinarizermodes. Note that some QR codes without a logo may fail to parse in PureBarcode mode, in which case this option should be set tofalse.- Parameters:
image- TheImageobject 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 totrue).- Returns:
- The decoded text.
-
decode
Decodes a QR code or barcode image into text with custom decoding hints. This method attempts to parse using bothHybridBinarizerandGlobalHistogramBinarizermodes.- Parameters:
image- TheImageobject 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 aBitMatrixto aBufferedImage.- Parameters:
matrix- TheBitMatrixto convert.foreColor- The foreground color.backColor- The background color (nullfor transparent background).- Returns:
- The converted
BufferedImage.
-
generateAsSvg
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
Converts aBitMatrixto an SVG string.- Parameters:
matrix- TheBitMatrixto convert.config- TheQrConfigfor SVG rendering.- Returns:
- The SVG vector graphic as a string.
-
generateAsAsciiArt
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
Converts aBitMatrixto an ASCII Art character representation of a QR code.- Parameters:
matrix- TheBitMatrixto convert.config- TheQrConfigfor ASCII Art rendering.- Returns:
- The ASCII Art character representation of the QR code.
-