Class BarcodeBuilder
- All Implemented Interfaces:
Serializable
,Cloneable
- validation: can be turned on or off to perform parameter validation,
- text: required parameter that assign the text contents for the barcode,
- size: the requested size of the barcode, if not specified, the smallest size is assumed,
- errorCorrection: what degree of error correction to use depending on barcode,
- charset: what character set or name to use where applicable,
- shape: Specifies the matrix shape for Data Matrix,
- margin: margin, in pixels, to use when generating the barcode, the meaning can vary by format,
- compact: whether to use compact mode for PDF417 barcodes,
- compaction: what compaction mode to use for PDF417 barcodes,
- dimensions: minimum and maximum number of rows and columns for PDF417 barcodes,
- layers: required number of layers for AZTEC barcodes,
- version: exact version for encoded QR_CODE barcodes,
Only the text
parameter is required, but the size
parameter is recommended.
Once parameters are set, the builder can produce a BarcodeURN that can be used within IIZI in various places, create an SVG image String with or without XML header, a java.awt.BufferedImage
, or write the barcode to a file or an output stream.
NOTE: This class is not thread safe by design in order to keep garbage collection as low as possible. If you need multiple thread access, make sure to synchronize on the BarcodeBuilder
instance you use between the threads.
- Author:
- Christopher Mindus
- See Also:
Method Summary
Modifier and TypeMethodDescriptionCreates a BufferedImage of the barcode that can be used in java.awt transformations or other operations.asSVG()
Creates an SVG image for the barcode.asURN()
Gets the String to as a{@link BarcodeURN}
for this barcode.Gets the String to as a{@link BarcodeURN}
for this barcode.Specifies what character set to use for encoding valid for the barcode types or formatsBarcodeType.AZTEC
,BarcodeType.PDF_417
andBarcodeType.QR_CODE
.Specifies what character set to use for encoding valid for the barcode types or formatsBarcodeType.AZTEC
,BarcodeType.PDF_417
andBarcodeType.QR_CODE
.Checks the basic parameters, regardless of if validation mode is enabled or not.clone()
Clones this instance.compact
(boolean compact) Sets whether to use compact mode forBarcodeType.PDF_417
barcodes.compaction
(Compaction compaction) Sets whether to use compaction mode forBarcodeType.PDF_417
barcodes.dimensions
(int minCols, int maxCols, int minRows, int maxRows) Sets the dimensions for a PDF417 barcode, i.e.boolean
Checks if two builders are equal.errorCorrection
(int errorCorrectionLevel) Assigns the error correction for a AZTEC or PDF417 barcodes.errorCorrection
(ErrorCorrectionLevel errorCorrectionLevel) Assigns the error correction for a QR code.Gets the barcode type or format.Gets the QR code error correction level.int
Gets the height of the barcode.getHints()
Gets a copy of the hints used for the Zxing barcode encoder.Gets the parameters as a string as:getText()
Gets the current barcode text contents.int
getWidth()
Gets the width of the barcode.int
hashCode()
Gets the hashcode of this builder.boolean
Returns if the validation mode is enabled.layers
(int number) Specifies the required number of layers for anBarcodeType.AZTEC
barcode.margin
(int margin) Sets the margin in pixels to use when generating the barcode, the meaning can vary by format.shape
(SymbolShapeHint shape) Specifies the matrix shape for Data Matrix.size
(int size) Assigns the size for a as the width and height, typically used for a 2-dimensional barcode such asBarcodeType.AZTEC
,BarcodeType.DATA_MATRIX
andBarcodeType.QR_CODE
.size
(int width, int height) Assigns the width and height for the barcode.Assign the text contents for the barcode.Writes the barcode to a file in .PNG format.Writes the barcode to a file in the requestedformatName
such as "png" or "jpg".void
toOutputStream
(OutputStream pngOutputStream) Writes the barcode to an output stream in .PNG format.void
toOutputStream
(String formatName, OutputStream outputStream) Writes the barcode to an output stream in .PNG format.toString()
Returns a string representation of this instance.validate
(boolean isEnabled) Turns validation mode on or off.version
(int number) Specifies the exact version of QR code to be encoded.
Method Details
validate
Turns validation mode on or off.- Parameters:
isEnabled
- Flag to enable validation.- Returns:
- This instance in order to be able to concatenate methods for this builder.
text
Assign the text contents for the barcode.The text contents is directly validated if validation is enabled, otherwise it will be validated when the builder performs some output operation such as creating the BarcodeURN or an image (SVG, BufferedImage or written to file or output stream).
- Parameters:
text
- The text contents.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
NullPointerException
- If thetext
isnull
.BarcodeValidationException
- If thetext
is not valid for the barcode type or format when validation mode is enabled.
size
Assigns the size for a as the width and height, typically used for a 2-dimensional barcode such asBarcodeType.AZTEC
,BarcodeType.DATA_MATRIX
andBarcodeType.QR_CODE
.Note that the width and height values can be enlarged if the barcode bitmap does not fit inside the requested dimension.
- Parameters:
size
- The width and height to is set to this size.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
IllegalArgumentException
- If thesize
is negative.
size
Assigns the width and height for the barcode.Note that the values can be enlarged if the barcode bitmap does not fit inside the requested dimension.
- Parameters:
width
- The width.height
- The height.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
IllegalArgumentException
- If thesize
is negative.
errorCorrection
Assigns the error correction for a QR code. If validation mode is not enabled and this barcode type or format is notBarcodeType.QR_CODE
, then this method will do nothing.- Parameters:
errorCorrectionLevel
- The error correction level, ornull
to reset to the defaultErrorCorrectionLevel.L
level used by default.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
BarcodeValidationException
- If this barcode type or format is not aBarcodeType.QR_CODE
and validation mode is enabled.
errorCorrection
Assigns the error correction for a AZTEC or PDF417 barcodes. If validation mode is not enabled and this barcode type or format is notBarcodeType.AZTEC
orBarcodeType.PDF_417
, then this method will do nothing.- Parameters:
errorCorrectionLevel
- For Aztec the value represents the minimal percentage (0-100) of error correction words.
For PDF417, valid values being 0 to 8.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
IllegalArgumentException
- If theerrorCorrectionLevel
value is out of range for the barcode type or format.BarcodeValidationException
- If this barcode type or format is not aBarcodeType.QR_CODE
and validation mode is enabled.
charset
Specifies what character set to use for encoding valid for the barcode types or formatsBarcodeType.AZTEC
,BarcodeType.PDF_417
andBarcodeType.QR_CODE
. If validation mode is enabled for these barcode type or format, aBarcodeValidationException
is thrown, otherwise this method will do nothing.- Parameters:
charsetName
- The character set name, ornull
reset to default value.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
BarcodeValidationException
- If this barcode type or format is not aBarcodeType.AZTEC
,BarcodeType.PDF_417
orBarcodeType.QR_CODE
and validation mode is enabled, or if it is one of these barcode types or formats and thecharsetName
is illegal or unsupported in this Java VM.
charset
Specifies what character set to use for encoding valid for the barcode types or formatsBarcodeType.AZTEC
,BarcodeType.PDF_417
andBarcodeType.QR_CODE
. If validation mode is enabled for these barcode type or format, aBarcodeValidationException
is thrown, otherwise this method will do nothing.- Parameters:
charset
- The character set, ornull
reset to default value.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
BarcodeValidationException
- If this barcode type or format is not aBarcodeType.AZTEC
,BarcodeType.PDF_417
orBarcodeType.QR_CODE
and validation mode is enabled.
shape
Specifies the matrix shape for Data Matrix. It can be used to force square or rectangular symbols. Only the barcode type or formatBarcodeType.DATA_MATRIX
supports this method. If validation mode is enabled and the barcode is not DATA_MATRIX, aBarcodeValidationException
is thrown, otherwise this method will do nothing.- Parameters:
shape
- The shape, ornull
to reset to default{@link SymbolShapeHint#FORCE_NONE}
. Valid values are otherwise{@link SymbolShapeHint.FORCE_NONE}
,{@link SymbolShapeHint.FORCE_SQUARE}
or{@link SymbolShapeHint.FORCE_RECTANGLE}
.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
BarcodeValidationException
- If this barcode type or format is not aBarcodeType.DATA_MATRIX
and validation mode is enabled.
margin
Sets the margin in pixels to use when generating the barcode, the meaning can vary by format. Supported barcodes all one-dimensional barcode formats or types,BarcodeType.PDF_417
andBarcodeType.QR_CODE
. For other formats, this method will do nothing unless validation mode is enabled in which case aBarcodeValidationException
is thrown.All supported barcode formats or types:
BarcodeType.CODABAR
,BarcodeType.CODE_39
,BarcodeType.CODE_93
,BarcodeType.CODE_128
,BarcodeType.EAN_13
,BarcodeType.ITF
,BarcodeType.PDF_417
,BarcodeType.QR_CODE
,BarcodeType.UPC_A
,BarcodeType.UPC_E
.
Unsupported barcode formats or types:
- Parameters:
margin
- The margin or a negative value (e.g. @code -1}) to reset to default value.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
BarcodeValidationException
- If this barcode type or format is not supported and validation mode is enabled.
compact
Sets whether to use compact mode forBarcodeType.PDF_417
barcodes.This method will do nothing for other barcode formats or types if validation is not enabled, otherwise a
BarcodeValidationException
is thrown.- Parameters:
compact
- Flag indicating compact PDF417 mode, default isfalse
.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
BarcodeValidationException
- If this barcode type or format is notBarcodeType.PDF_417
and validation mode is enabled.
compaction
Sets whether to use compaction mode forBarcodeType.PDF_417
barcodes.This method will do nothing for other barcode formats or types if validation is not enabled, otherwise a
BarcodeValidationException
is thrown.- Parameters:
compaction
- The PDF417 Compaction mode, ornull
to reset to defaultCompaction.AUTO
Compaction mode. Valid values are:{@link Compaction#AUTO}
,{@link Compaction#TEXT}
,{@link Compaction#BYTE}
or{@link Compaction#NUMERIC}
.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
BarcodeValidationException
- If this barcode type or format is notBarcodeType.PDF_417
and validation mode is enabled.
dimensions
Sets the dimensions for a PDF417 barcode, i.e. the minimum and maximum number of rows and columns.This method will do nothing for other barcode formats or types if validation is not enabled, otherwise a
BarcodeValidationException
is thrown.To reset the values to default, set all parameter values to zero.
- Parameters:
minCols
- Minimum allowed columns.maxCols
- Maximum allowed columns.minRows
- Minimum allowed rows.maxRows
- Maximum allowed rows.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
BarcodeValidationException
- If this barcode type or format is notBarcodeType.PDF_417
and validation mode is enabled, or if either value is zero or a minimum value equal or larger than its corresponding maximum value.
layers
Specifies the required number of layers for anBarcodeType.AZTEC
barcode.This method will do nothing for other barcode formats or types if validation is not enabled, otherwise a
BarcodeValidationException
is thrown.To reset the values to default, set the
layerNumbers
value to zero.- Parameters:
number
- Required number of layers for AZTEC barcodes:- A negative number (-1, -2, -3, -4) specifies a compact Aztec code.
- Zero indicates to use the minimum number of layers (the default).
- A positive number (1, 2,... 32) specifies a normal (non-compact) Aztec code.
- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
BarcodeValidationException
- If validation mode is enabled and this barcode type or format is notBarcodeType.AZTEC
ornumber
is less than-4
or larger than32
.
version
Specifies the exact version of QR code to be encoded.This method will do nothing for other barcode formats or types if validation is not enabled, otherwise a
BarcodeValidationException
is thrown.To reset the version to default recommended version depending of the error correction and the barcode text content, set the
number
value to zero.- Parameters:
number
- Version number: zero for default, otherwise a value between 1 and 40.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
BarcodeValidationException
- If validation mode is enabled and this barcode type or format is notBarcodeType.QR_CODE
or versionnumber
is not in the range of 1 to 40.
checkParams
Checks the basic parameters, regardless of if validation mode is enabled or not.- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
BarcodeValidationException
- For missing or invalid values.
asURNString
Gets the String to as a{@link BarcodeURN}
for this barcode.- Returns:
- The
BarcodeURN
String. - Throws:
BarcodeException
- If there are missing or invalid parameters assigned in this builder encountered during barcode encoding.
asURN
Gets the String to as a{@link BarcodeURN}
for this barcode.- Returns:
- The
BarcodeURN
String. - Throws:
BarcodeException
- If there are missing or invalid parameters assigned in this builder encountered during barcode encoding.
asSVG
Creates an SVG image for the barcode.- Returns:
- The SVG image string without XML header.
- Throws:
BarcodeException
- If there are missing or invalid parameters assigned in this builder encountered during barcode encoding.
asBufferedImage
Creates a BufferedImage of the barcode that can be used in java.awt transformations or other operations.- Returns:
- The BufferedImage.
- Throws:
BarcodeException
- If there are missing or invalid parameters assigned in this builder encountered during barcode encoding.
toOutputStream
Writes the barcode to an output stream in .PNG format.- Parameters:
pngOutputStream
- The output stream that will receive the contents of the PNG file image data of the barcode.- Throws:
BarcodeException
- If there are missing or invalid parameters assigned in this builder encountered during barcode encoding.IOException
- For I/O errors writing the file.
toOutputStream
public void toOutputStream(String formatName, OutputStream outputStream) throws BarcodeException, IOException Writes the barcode to an output stream in .PNG format.- Parameters:
formatName
- The image format name, typically "png" (or "jpg").outputStream
- The output stream that will receive the contents of the file image data of the barcode in the requestedformatName
.- Throws:
BarcodeException
- If there are missing or invalid parameters assigned in this builder encountered during barcode encoding.IOException
- For I/O errors writing the file.
toFile
Writes the barcode to a file in .PNG format.- Parameters:
pngFile
- The file, should have the ".png" file extension (but this is not checked).- Returns:
- The file written.
- Throws:
BarcodeException
- If there are missing or invalid parameters assigned in this builder encountered during barcode encoding.IOException
- For I/O errors writing the file.
toFile
Writes the barcode to a file in the requestedformatName
such as "png" or "jpg".- Parameters:
formatName
- The image format name, typically "png" (or "jpg").file
- The file, should have the appropriate file extension such as"dot" + formatName
.- Returns:
- The file written.
- Throws:
BarcodeException
- If there are missing or invalid parameters assigned in this builder encountered during barcode encoding.IOException
- For I/O errors writing the file.
getParamsString
Gets the parameters as a string as:format=type,text=contents,width=w,height=h,...
- Returns:
- A string representation of set parameters.
toString
Returns a string representation of this instance.equals
Checks if two builders are equal.hashCode
public int hashCode()Gets the hashcode of this builder.clone
Clones this instance. This method can be used to clone a builder in order to e.g. add parameters and not to modify the first builder.getBarcodeType
Gets the barcode type or format.- Returns:
- The
BarcodeType
.
isValidating
public boolean isValidating()Returns if the validation mode is enabled.- Returns:
- The validation enabled flag.
getText
Gets the current barcode text contents.- Returns:
- The text, or
null
if not set.
getWidth
public int getWidth()Gets the width of the barcode.- Returns:
- The requested width, or zero if not set or for smallest width.
getHeight
public int getHeight()Gets the height of the barcode.- Returns:
- The requested height, or zero if not set or for smallest height.
getErrorCorrection
Gets the QR code error correction level.- Returns:
- The error correction level, or
null
if not set or default Low (ErrorCorrectionLevel.L
= ~7%) or this barcode type is not a QR code.
getHints
Gets a copy of the hints used for the Zxing barcode encoder.- Returns:
- A map of hints, or
null
if none are set.