Royalty-free developer license, one time purchase for lifetime use
Java QR-Code Introduction
QR-Code is also known as Denso Barcode, QRCode, Quick Response Code, JIS X 0510, ISO/IEC18004.
Compatibility: Barcode for Java library is compatible with the latest Code-39 ISO specification [ISO/IEC 18004 (Second edition 2006-09-01)].
QR Code is a kind of 2-D (two-dimensional) symbology developed by Denso Wave (a division of Denso Corporation at the time) and released in 1994 with the primary aim of being a symbol that is easily interpreted by scanner equipment.
QR Code is capable of handling all types of data, such as numeric and alphabetic characters, Kanji, Kana, Hiragana, symbols, binary, and control codes.
The symbol versions of QR Code range from Version 1 to Version 40. Each version has a different module configuration or number of modules (the module refers to the black and white dots that make up QR Code). "Module configuration" refers to the number of modules contained in a symbol, commencing with Version 1 (21 x 21 modules) up to Version 40 (177 x 177 modules). Each higher version number comprises 4 additional modules per side. Each QR Code symbol version has the maximum data capacity according to the amount of data, character type and error correction level. In other words, as the amount of data increases, more modules are required to comprise QR Code, resulting in larger QR Code symbols.
QR Code has error correction capability to restore data if the code is dirty or damaged. Four error correction levels are available for users to choose according to the operating environment. Raising this level improves error correction capability but also increases the amount of data QR Code size. To select error correction level, various factors such as the operating environment and QR Code size need to be considered. Level Q or H may be selected for factory environment where QR Code gets dirty, whereas Level L may be selected for clean environment with the large amount of data. Typically, Level M (15%) is most frequently selected. The QR Code error correction feature is implemented by adding a Reed-Solomon Code to the original data.
Java QR-Code Basic Characteristics
QR Code is a matrix symbology with the following characteristics:
Formats: QR Code, with full range of capabilities and maximum data capacity;
Encodable character set:
numeric data (digits 0 - 9);
alphanumeric data (digits 0 - 9; upper case letters A -Z; nine other characters: space, $ % * + - . / : );
byte data (default: ISO/IEC 8859-1);
Kanji characters.
Version (Symbol size) (not including quiet zone): 21 x 21 modules to 177 x 177 modules (Versions 1 to 40, increasing in steps of four modules per side).
Maximum QR Code symbol size, Version 40-L:
numeric data: 7089 characters
alphanumeric data: 4296 characters
Byte data: 2953 characters
Kanji data: 1817 characters
Selectable error correction level: Four levels of Reed-Solomon error correction (referred to as L, M, Q and H in increasing order of capacity) allowing recovery of:
L 7% of the symbol codewords.
M 15% of the symbol codewords.
Q 25% of the symbol codewords.
H 30% of the symbol codewords.
Structured Append: This allows files of data to be represented logically and continuously in up to 16 QR Code symbols. These may be scanned in any sequence to enable the original data to be correctly reconstructed.
Extended Channel Interpretations: This mechanism enables data using character sets other than the default encodable set (e.g. Arabic,Cyrillic, Greek) and other data interpretations (e.g. compacted data using defined compression schemes)or other industry-specific requirements to be encoded.
FNC1 Mode: FNC1 mode is used for messages containing specific data formats. In the "1st position" it designates data formatted in accordance with the GS1 General Specifications. In the "2nd position" it designates data formatted in accordance with a specific industry application previously agreed with AIM Inc. FNC1 mode applies to the entire symbol and is not affected by subsequent mode indicators.
Java QR-Code Encoding Data Scope
Java QR-Code Generator supports:
numeric data (digits 0 - 9);
alphanumeric data (digits 0 - 9; upper case letters A -Z; nine other characters: space, $ % * + - . / : );
byte data (default: ISO/IEC 8859-1);
Kanji characters.
How to Generate QR-Code Barcode in Java Class?
The following Java code illustrates how to create a QR-Code barcode, and output to various formats.
1 QRCode barcode = new QRCode(); 2 barcode.setData("123456789");
// add more QRCode property settings here 3 barcode.renderBarcode("c:/barcode.gif");
Generate QR-Code barcode and encode into gif image format (line 3)
Generate QR-Code and output to BufferedImage object (line 4)
Generate QR-Code in bitmap and output to byte[] (line 5)
Generate QR-Code and draw to defined area (Rectangle2D) on Graphics2D (line 6-8)
Generate QR-Code and output to OutputStream (line 9-10)
Print QR Code in Memory
You can also generate a QR Code in a Java memory object for further processing. The library provides multiple methods:
drawBarcode() – returns a java.awt.image.BufferedImage containing the QR Code image data.
drawBarcode(java.awt.Graphics2D g, java.awt.geom.Rectangle2D rectangle) – draws the QR Code on a Graphics2D object within a specified rectangle.
drawBarcode(java.io.OutputStream outputStream) – outputs the QR Code to a Java OutputStream.
drawBarcodeToBytes() – creates the QR Code image and returns it as a byte array.
How to create barcode with specified size in C#?
QR Code is a 2D barcode that supports various text formats. Using the Java Barcode library, you can convert the following text or data formats to QR Code images in Java projects:
Plain English text using the full 128 ASCII characters
Unicode text
Binary data
GS1 business data messages
Convert ASCII Characters to QR Code
Encoding plain English text (ASCII characters) to QR Code is straightforward.
Set the QR Code encoding ASCII characters using the setData() method (e.g., barcode.setData("your text here")).
Call the drawBarcode() method to output the QR Code to an image file or in memory object.
Encode ASCII Non Printing Characters
The ASCII character set includes 33 non printing characters (control characters). To encode these in a QR Code, the Java Barcode library provides tilde based processing.
Enable the ProcessTilde property by calling setProcessTilde(true).
Set the QR Code data mode to Auto using setDataMode(QRCode.M_AUTO).
In the encoding data, convert each non printing character to its three digit ASCII value prefixed with ~. For example, carriage return (ASCII value 13) is represented as "~013".
Convert Unicode Text to QR Code
The following Java source code demonstrates generating a QR Code with Unicode text encoded.
Create a new QRCode object.
Enable Unicode text encoding by calling setEncodeUnicodeText(true).
Input the Unicode text in the Data property. The example uses Chinese text.
Call drawBarcode() to output the QR Code to a PNG image file.
UTF 8 and UTF 16 Encoding
In the above Java sample, the barcode library generates the QR Code using UTF 8 encoding. You can select a different text encoding format (such as UTF-16) by calling the setUnicodeEncoding() method.
How to Resize the Generated QR Code Image Width and Height
You can set the barcode image dimensions using the barcodeWidth and barcodeHeight properties, or by specifying the X (bar module width) and Y (bar module height) values.
How to Create a Styled QR Code with a Logo Image Using Java
A standard, ISO compliant QR Code may not always meet business and marketing requirements. The library supports displaying logos and styled colors.
The following Java example demonstrates how to create a styled QR Code with an embedded logo image.
Create a QRCode object.
Set the QR Code encoding data using setData().
Select the logo image using setLogo().
Set the logo image dimensions with setLogoActualWidth() and setLogoActualHeight().
Generate and output the QR Code with the embedded logo.
How to Create and Customize QR Code Options in a Java Application
QR Code Error Correction Mode
QR Code uses four levels of Reed Solomon error correction: L, M, Q, and H (in increasing capacity). In the Java QR Code Generation library, set the error correction mode using the ECL property. The default is QRCodeECL.L.
QR Code Version
The QR Code ISO standard defines forty sizes, referred to as Version 1 through Version 40. Using the Java QR Code generator API, set the Version property to one of the values from QRCodeVersion.V1 to QRCodeVersion.V40.
QR Code Structured Append Mode
A large data message can be divided and stored in up to 16 QR Code barcodes using Structured Append mode. In the Java QR Code generator SDK, apply the following properties:
StructuredAppend: set to true to enable Structured Append mode.
SymbolCount: total number of symbols for the data message.
SymbolIndex: position of the current symbol in the sequence (the first symbol index is 0).
QR Code FNC1 in First Position
This FNC1 mode indicator identifies that the encoded data is formatted according to the GS1 Application Identifiers standard. To enable this mode, set the FNC1 property to FNC1.FNC1_1ST_POS.
QR Code Extended Channel Interpretation (ECI) Mode
In the Java QR Code generator library, you can change the encoding character set using QR Code Extended Channel Interpretation (ECI) mode. Apply the following properties in your Java class.
ECI: a six digit ECI value for the new character set defined by AIM (e.g., 000013 for ISO/IEC 8859 11).
ProcessTilde: set to true. Convert the new character data to a byte array.
How to Generate Barcode QR-Code Image in Java Web Applications?
There are two methods to create QR-Code barcode images in your Java web applications.
The simplest way is to stream barcode image using our buildin barcode servlet application.
Under java_barcode_trial package, copy barcode folder to your java servlet container like tomcat.
Restart tomcat. Now you have installed successfully.
To test your installation, open your web browser and navigate to: http://YourDomain:Port/barcode/qrcode?Data=123456789
To create barcode image in your JSP or html page, you need pass the url to IMG tag src value. For example, <img src="http://YourDomain:Port/barcode/qrcode?Data=123456789" /> Using this method, it will not generate any barcode images in your server side.
The second method is to generate barcode images in your server side.
Create a barcode image in server side, like QRCode barcode = new QRCode(); barcode.setData("123456789"); barcode.renderBarcode("C:/Tools/Tomcat 5.5/webapps/YourWebApp/barcode-images/barcode.gif");
In your JSP or HTML page, you need use IMG tag to display generated image, like <img src="http://YourDomain:Port/YourWebApp/barcode-images/barcode.gif" />
How to Generate Barcode QR-Code Image in Jasper Reports, iReport, Eclipse BIRT?
Set the data property with the value to encode. Type is String. Servlet URL Parameter: "Data".
Set the dataMode property. Default is QRCode.MODE_AUTO. Servlet URL Parameter: "DataMode". Sample: &DataMode=0.
QRCode.MODE_AUTO (0 default): It allows encoding all 256 possible 8-bit byte values. This includes all ASCII characters value from 0 to 127 inclusive and provides for international character set support
QRCode.MODE_ALPHANUMERIC (1): It allows encoding alphanumeric data (digits 0 - 9; upper case letters A -Z; nine other characters: space, $ % * + - . / : ).
QRCode.MODE_BYTE (2): It allows encoding byte data (default: ISO/IEC 8859-1).
QRCode.MODE_NUMERIC (3): It allows encoding numeric data (digits 0 - 9).
QRCode.MODE_KANJI (4): It allows encoding Kanji characters.
Set the processTilde property to true, if you want use the tilde character "~" to specify special characters in the encoding data. Default is false. Servlet URL Parameter: "ProcessTilde". Value: "t" (true), "f" (false). Sample: &ProcessTilde=t. Format of the tilde:
~NNN: is used to represent the ASCII character with the value of NNN. NNN is from 000 - 255.
~6NNNNN: is used to represent the Unicode. NNNNN is from 00000 - 65535.
~7NNNNNN: is used to specify the Extended Channel Interpretations and NNNNNN is a value between 000000 and 999999.
~9NNNNN: is used to represent the Shift JIS. It is for Shift JIS 0x8140 ~ 0x9FFC and 0xE040 ~ 0xEBBF.
Set the version property. Default is 1. Valid values are from 1 to 40. About Version and Symbology Size Servlet URL Parameter: "Version". Sample: &Version=1.
QR Code can be divided into multiple data areas. Conversely, information stored in multiple QR Code symbols can be reconstructed as single data symbols.One data symbol can be divided into up to 16 symbols.
SetisStructuredAppend property to true, then Structured Append is enabled. About Structure Append Servlet Parameter: "IsStructuredAppend".
Set symbolCount property to the number of total symbols which make the sequence. Servlet Parameter: "SymbolCount".
Set symbolIndex property to the position of current symbol in the secuence (Start with 0). Servlet Parameter: "SymbolIndex".
Set the fnc1Mode property. Servlet URL Parameter: "FNC1Mode". Sample: FNC1Mode=0
0 (none)
1 (enabled)
Setting up barcode image size.
Set property uom (Unit of Measure) for properties X, Y, leftMargin, rightMargin, topMargin and bottomMargin. Default is Linear.UOM_PIXEL (0). Valid values are Linear.UOM_PIXEL (0), Linear.UOM_CM (1), Linear.UOM_Inch (2). Servlet URL Parameter: "UOM". Value: 0 (pixel), 1 (cm), 2 (inch). Sample: &UOM=0.
Set the moduleSize (for barcode module width & height) property. Type is float. Default is 1. Servlet URL Parameter: "ModuleSize". Sample: &ModuleSize=5.
Set the leftMargin, rightMargin, topMargin and bottomMargin properties, and types are all float. Default values are 0 for all 4 margin settings. Servlet URL Parameter: "LeftMargin", "RightMargin", "TopMargin", "BottomMargin". Sample: &LeftMargin=0.
Set the resolution property (Value is expressed in DPI - Dots per inch). Default is 72 dpi. Servlet URL Parameter: "Resolution". Sample: &Resolution=72.
With rotate property, you can display barcode horizontally or vertically. Default value is 0. Servlet URL Parameter: "Rotate". Sample: &Rotate=0. Value can be:
set dataMode value to QRCode.MODE_AUTO or QRCode.MODE_KANJI.
set processTilde value to true.
set Kanji value. there are two ways to set kanji data value. According to QRCode specification, library is using Shift JIS encoding system.
call method setKanjiData(int[] binaryData). Each int value represent one kanji character in Shift JIS encoding. Valid kanji values in Shift JIS encoding are from 0x8140 to 0x9FFC, and from 0xE040 to 0xEBBF. Please do not set data value here.
set property data. Use "~9NNNNN" to represent each Kanji character in Shift JIS encoding. For example, input character . Shift JIS Value is 0x935F, and int value is 37727. in method a, you call setKanjiData({37727}). in method b, you set data = "~937727".