- All Implemented Interfaces:
Cloneable
Usage:
- Providing common settings such as code type (QR Code, DataMatrix, ...), the content, character set and quiet zone.
- Providing type-specific settings for the 2D code type you want to create.
- Check compatibility between the content and selected charset using
canEncode(). - Call
buildSymbol()to generate the drawable code symbol.
// Step 1: Common settings
TwoDCode tdc = new TwoDCode(TwoDType.QRCODE);
tdc.setContent("Hello World!");
tdc.setCharset(null); // null = ISO 8859-1, includes no ECI
tdc.setQuietZone(TwoDType.QRCODE.getDefaultQuietZone());
// Step 2: Type-specific settings
tdc.setQRCodeVersion(QRCodeVersion.AUTO);
tdc.setQRCodeErrCorr(QRCodeErrorCorrection.M);
// Step 3 and 4: Generate drawable symbol
TwoDSymbol symbol = null;
if (tdc.canEncode()) {
try {
symbol = tdc.buildSymbol();
} catch (Exception ex) {
// Handle the Exception
}
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGenerates a drawable 2D symbol based on the provided properties.booleanChecks if the specified charset can encode the given content.clone()Returns a copy of this object.intReturns the error correction level of the Aztec.Returns the size of the Aztec symbol.Returns the character set used for encoding the content.Returns the content encoded in the 2D code.Returns the shape of the DataMatrix symbol.Returns the size of the DataMatrix symbol.Returns the error correction level of the PDF417.Returns the size of the PDF417 symbol.Returns the error correction level of the QR Code.Returns the version of the QR Code.intReturns the size of the quiet zone around the 2D code.getType()Returns the type of the 2D code.voidsetAztecErrCorr(int errorCorrectionLevel) Sets the error correction level for Aztec, specified in percent.voidsetAztecSize(AztecSize size) Sets the size of the Aztec code symbol.voidsetCharset(Charset charset) Specifies the character set used to encode the content in the 2D code.voidsetContent(String content) Sets the content to be encoded in the 2D code.voidSets the shape of the DataMatrix symbol.voidSets the size of the DataMatrix symbol.voidsetPDF417ErrCorr(PDF417ErrorCorrection errorCorrectionLevel) Sets the error correction level for PDF417.voidsetPDF417Size(PDF417Size size) Sets the size of the PDF417 symbol.voidsetQRCodeErrCorr(QRCodeErrorCorrection errorCorrectionLevel) Sets the error correction level for QR Code.voidsetQRCodeVersion(QRCodeVersion version) Sets the version of the QR Code.voidsetQuietZone(int sizeInModules) Sets the size of the quiet zone for the 2D code.voidSets the type of the 2D code.
-
Constructor Details
-
TwoDCode
public TwoDCode()Creates a new instance with default settings.- Code type:
TwoDType.QRCODE - Content: "ABCD... 01234"
- Charset:
null(equivalent to ISO 8859-1 with no ECI block included) - Quiet zone size: 1
Settings by code type:
- QR Code version:
QRCodeVersion.AUTO - QR Code error correction level:
QRCodeErrorCorrection.M - DataMatrix size:
DataMatrixSize.AUTO - DataMatrix shape:
DataMatrixShape.AUTO - PDF417 size:
PDF417Size(0, 0) - fit to content in both dimensions - PDF417 error correction level:
PDF417ErrorCorrection.EC2 - Aztec size:
AztecSize.AUTO - Aztec error correction level: 23
- Code type:
-
TwoDCode
Creates a new instance with the specified code type.All other settings are the same as when using the
default constructor.- Parameters:
codeType- the type of the 2D code to set- Throws:
NullPointerException- if the provided code type isnull
-
-
Method Details
-
setType
Sets the type of the 2D code.- Parameters:
codeType- the type of the 2D code to set- Throws:
NullPointerException- if the provided code type isnull
-
setContent
Sets the content to be encoded in the 2D code.GS1 DataMatrix and GS1 QR Code: It is recommended to validate the content using the
GS1Validatorclass before setting. Please refer to the class documentation for the expected input format requirements. Example:import de.vwsoft.barcodelib4j.oned.BarcodeException; import de.vwsoft.barcodelib4j.oned.GS1Validator; GS1Validator validator = null; try { validator = new GS1Validator(gs1Data, fnc1Char); } catch (BarcodeException ex) { // Validation failed } if (validator != null) twoDCode.setContent(validator.getContent());- Parameters:
content- the content to be encoded- Throws:
NullPointerException- if the provided content isnullIllegalArgumentException- if the provided content is empty
-
setCharset
Specifies the character set used to encode the content in the 2D code.If set to
null(default), no character set information (ECI) is built into the code, and the ISO 8859-1 character set is used. This is the default character set for all supported 2D types unless another character set is explicitly specified.DataMatrix: The current implementation allows the use of character sets other than ISO 8859-1 only when its size is set to
AUTO.GS1 DataMatrix and GS1 QR Code: Specifying a character set is unnecessary since GS1 data consists only of ASCII characters, which are fully compatible with ISO 8859-1.
- Parameters:
charset- the character set to be used for encoding the content, ornullto indicate the default ISO 8859-1 charset with no ECI
-
setQuietZone
public void setQuietZone(int sizeInModules) Sets the size of the quiet zone for the 2D code.For an explanation of what quiet zones are and the default quiet zone sizes for each 2D code type, see
TwoDType.getDefaultQuietZone().- Parameters:
sizeInModules- the size of the quiet zone in modules- Throws:
IllegalArgumentException- if the specified size is negative
-
setQRCodeVersion
Sets the version of the QR Code.The version determines the size and data capacity of the QR Code. Valid versions range from 1 (21x21 modules) to 40 (177x177 modules). Use
QRCodeVersion.AUTOto automatically select the smallest version capable of encoding the content.- Parameters:
version- the version of the QR Code orQRCodeVersion.AUTO- Throws:
NullPointerException- if the specified version isnull
-
setQRCodeErrCorr
Sets the error correction level for QR Code.The error correction level specifies the amount of redundant data added to the symbol to enable error recovery. QR Codes support four levels of error correction: L (Low - ~7%), M (Medium - ~15%), Q (Quartile - ~25%), and H (High - ~30%). The higher the error correction level, the more redundancy is added, resulting in a higher resistance to errors, but also in a larger symbol size.
- Parameters:
errorCorrectionLevel- the error correction level to set for QR Code- Throws:
NullPointerException- if the specified error correction level isnull
-
setDataMatrixSize
Sets the size of the DataMatrix symbol.The size determines the data capacity of the DataMatrix symbol. Valid sizes range from 10x10 modules to 144x144 modules. The 30 available sizes include 24 square and 6 rectangular variants. Use
DataMatrixSize.AUTOto automatically select the smallest size capable of encoding the content.- Parameters:
size- the size of the DataMatrix symbol orDataMatrixSize.AUTO- Throws:
NullPointerException- if the specified size isnull
-
setDataMatrixShape
Sets the shape of the DataMatrix symbol.Valid values are:
DataMatrixShape.SQUARE- Square shapeDataMatrixShape.RECTANGLE- Rectangular shapeDataMatrixShape.AUTO- Automatically select the shape that best fits the content
Note: This setting is ignored when a fixed size (not
AUTO) is specified viasetDataMatrixSize, as the size already determines whether the symbol is square or rectangular.- Parameters:
shape- the shape of the DataMatrix symbol- Throws:
NullPointerException- if the provided shape isnull
-
setPDF417Size
Sets the size of the PDF417 symbol.The PDF417 symbol size is specified in terms of the number of columns and rows. The number of columns can range from 1 to 30, and the number of rows can range from 3 to 90. If the columns or rows value is set to
0, the respective value is automatically selected based on the content to be encoded.- Parameters:
size- the size of the PDF417 symbol- Throws:
NullPointerException- if the provided size isnull
-
setPDF417ErrCorr
Sets the error correction level for PDF417.The parameter specifies the amount of redundant data added to the symbol to enable error recovery. The higher the error correction level, the more redundancy is added, resulting in a higher resistance to errors, but also in a larger symbol size. PDF417 supports error correction levels from
0to8.- Parameters:
errorCorrectionLevel- the error correction level to set for PDF417- Throws:
NullPointerException- if the specified error correction level isnull
-
setAztecSize
Sets the size of the Aztec code symbol.Aztec symbols come in two types that differ in their internal structure. Compact symbols (1 to 4 layers, 15x15 to 27x27 modules) have a smaller bullseye finder pattern and less error correction overhead, making them efficient for small data amounts. Normal symbols (1 to 32 layers, 19x19 to 151x151 modules) have a larger bullseye finder pattern and more robust error correction, providing better readability and greater data capacity. Use
AztecSize.AUTOto automatically select the smallest size capable of encoding the content, preferring compact sizes when possible.- Parameters:
size- the size of the Aztec symbol orAztecSize.AUTO- Throws:
NullPointerException- if the specified size isnull
-
setAztecErrCorr
public void setAztecErrCorr(int errorCorrectionLevel) Sets the error correction level for Aztec, specified in percent.The error correction level determines the amount of redundancy data added to the symbol for error detection and correction. Aztec symbols support error correction levels ranging from 5% to 95%. Higher error correction levels add more redundancy data, increasing the symbol's ability to withstand errors but also increasing its size.
- Parameters:
errorCorrectionLevel- the error correction level for Aztec, specified in percent- Throws:
IllegalArgumentException- if the specified error correction level is outside the valid range
-
canEncode
public boolean canEncode()Checks if the specified charset can encode the given content.If no character set is specified (
null), ISO 8859-1 is checked as this is the default charset used by all supported 2D code implementations.For several reasons, it is recommended to call this method before building the 2D code symbol using the
buildSymbol()method:- Some 2D code implementations (QR Code, Aztec) will generate the symbol even if the content cannot be encoded correctly with the specified charset, resulting in a 2D code that no longer contains the original information.
- In contrast, other implementations (DataMatrix, PDF417) will properly throw an exception if the content cannot be encoded. Such exceptions can be avoided by having this method perform the check in advance.
- Returns:
trueif the specified charset can encode the content,falseotherwise
-
buildSymbol
Generates a drawable 2D symbol based on the provided properties.Any exceptions thrown by this method are forwarded exceptions caused by the internal call to ZXing's
Writer#encode(). Since this method is implemented differently in the ZXing classes depending on the code type, it can throw different exceptions, even though onlyWriterExceptionis declared by theWriter#encode()methods. Therefore, all exceptions are forwarded as a generalizedjava.lang.Exception.For example, the
DataMatrixWriterand thePDF417Writermay throw different exceptions, even for the same reason, such as when non-encodable characters are detected in the content. While the firstWriterthrows the declaredWriterException, the second may throw anIllegalArgumentException.The
Writerimplementations of the other two code types (QR Code and Aztec) do not throw an exception at all when encountering non-encodable characters, but instead encode the content with a loss of information due to incorrect character encoding. This underlines the importance of checking this first by callingcanEncode().- Returns:
- a
TwoDSymbolobject representing the generated 2D symbol - Throws:
Exception- if an error occurs during the symbol generation process
-
getType
Returns the type of the 2D code.- Returns:
- the type of the 2D code
-
getContent
Returns the content encoded in the 2D code.- Returns:
- the content encoded in the 2D code
-
getCharset
Returns the character set used for encoding the content.- Returns:
- the character set used for encoding the content
-
getQuietZone
public int getQuietZone()Returns the size of the quiet zone around the 2D code.- Returns:
- the size of the quiet zone around the 2D code
-
getQRCodeVersion
Returns the version of the QR Code.- Returns:
- the version of the QR Code
-
getQRCodeErrCorr
Returns the error correction level of the QR Code.- Returns:
- the error correction level of the QR Code
-
getDataMatrixSize
Returns the size of the DataMatrix symbol.- Returns:
- the size of the DataMatrix symbol
-
getDataMatrixShape
Returns the shape of the DataMatrix symbol.- Returns:
- the shape of the DataMatrix symbol
-
getPDF417Size
Returns the size of the PDF417 symbol.- Returns:
- the size of the PDF417 symbol
-
getPDF417ErrCorr
Returns the error correction level of the PDF417.- Returns:
- the error correction level of the PDF417
-
getAztecSize
Returns the size of the Aztec symbol.- Returns:
- the size of the Aztec symbol
-
getAztecErrCorr
public int getAztecErrCorr()Returns the error correction level of the Aztec.- Returns:
- the error correction level of the Aztec
-
clone
Returns a copy of this object.The returned copy is independent of the original and can be modified without affecting it. All instance members are either primitive or immutable types, or will be automatically rebuilt when any of the instance's properties change. Thus, the copy can be handled as if it were a "deep copy".
-