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 SummaryModifier 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 formats- BarcodeType.AZTEC,- BarcodeType.PDF_417and- BarcodeType.QR_CODE.Specifies what character set to use for encoding valid for the barcode types or formats- BarcodeType.AZTEC,- BarcodeType.PDF_417and- BarcodeType.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 for- BarcodeType.PDF_417barcodes.- compaction- (Compaction compaction) Sets whether to use compaction mode for- BarcodeType.PDF_417barcodes.- dimensions- (int minCols, int maxCols, int minRows, int maxRows) Sets the dimensions for a PDF417 barcode, i.e.- booleanChecks 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.- intGets 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.- booleanReturns if the validation mode is enabled.- layers- (int number) Specifies the required number of layers for an- BarcodeType.AZTECbarcode.- 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 as- BarcodeType.AZTEC,- BarcodeType.DATA_MATRIXand- BarcodeType.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 requested- formatNamesuch 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- validateTurns 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.
 
- textAssign 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 the- textis- null.
- BarcodeValidationException- If the- textis not valid for the barcode type or format when validation mode is enabled.
 
- sizeAssigns the size for a as the width and height, typically used for a 2-dimensional barcode such as- BarcodeType.AZTEC,- BarcodeType.DATA_MATRIXand- BarcodeType.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 the- sizeis negative.
 
- sizeAssigns 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 the- sizeis negative.
 
- errorCorrectionAssigns the error correction for a QR code. If validation mode is not enabled and this barcode type or format is not- BarcodeType.QR_CODE, then this method will do nothing.- Parameters:
- errorCorrectionLevel- The error correction level, or- nullto reset to the default- ErrorCorrectionLevel.Llevel 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 a- BarcodeType.QR_CODEand validation mode is enabled.
 
- errorCorrectionAssigns the error correction for a AZTEC or PDF417 barcodes. If validation mode is not enabled and this barcode type or format is not- BarcodeType.AZTECor- BarcodeType.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 the- errorCorrectionLevelvalue is out of range for the barcode type or format.
- BarcodeValidationException- If this barcode type or format is not a- BarcodeType.QR_CODEand validation mode is enabled.
 
- charsetSpecifies what character set to use for encoding valid for the barcode types or formats- BarcodeType.AZTEC,- BarcodeType.PDF_417and- BarcodeType.QR_CODE. If validation mode is enabled for these barcode type or format, a- BarcodeValidationExceptionis thrown, otherwise this method will do nothing.- Parameters:
- charsetName- The character set name, or- nullreset 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 a- BarcodeType.AZTEC,- BarcodeType.PDF_417or- BarcodeType.QR_CODEand validation mode is enabled, or if it is one of these barcode types or formats and the- charsetNameis illegal or unsupported in this Java VM.
 
- charsetSpecifies what character set to use for encoding valid for the barcode types or formats- BarcodeType.AZTEC,- BarcodeType.PDF_417and- BarcodeType.QR_CODE. If validation mode is enabled for these barcode type or format, a- BarcodeValidationExceptionis thrown, otherwise this method will do nothing.- Parameters:
- charset- The character set, or- nullreset 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 a- BarcodeType.AZTEC,- BarcodeType.PDF_417or- BarcodeType.QR_CODEand validation mode is enabled.
 
- shapeSpecifies the matrix shape for Data Matrix. It can be used to force square or rectangular symbols. Only the barcode type or format- BarcodeType.DATA_MATRIXsupports this method. If validation mode is enabled and the barcode is not DATA_MATRIX, a- BarcodeValidationExceptionis thrown, otherwise this method will do nothing.- Parameters:
- shape- The shape, or- nullto 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 a- BarcodeType.DATA_MATRIXand validation mode is enabled.
 
- marginSets 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_417and- BarcodeType.QR_CODE. For other formats, this method will do nothing unless validation mode is enabled in which case a- BarcodeValidationExceptionis 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.
 
- compactSets whether to use compact mode for- BarcodeType.PDF_417barcodes.- This method will do nothing for other barcode formats or types if validation is not enabled, otherwise a - BarcodeValidationExceptionis thrown.- Parameters:
- compact- Flag indicating compact PDF417 mode, default is- false.
- Returns:
- This instance in order to be able to concatenate methods for this builder.
- Throws:
- BarcodeValidationException- If this barcode type or format is not- BarcodeType.PDF_417and validation mode is enabled.
 
- compactionSets whether to use compaction mode for- BarcodeType.PDF_417barcodes.- This method will do nothing for other barcode formats or types if validation is not enabled, otherwise a - BarcodeValidationExceptionis thrown.- Parameters:
- compaction- The PDF417 Compaction mode, or- nullto reset to default- Compaction.AUTOCompaction 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 not- BarcodeType.PDF_417and validation mode is enabled.
 
- dimensionsSets 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 - BarcodeValidationExceptionis 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 not- BarcodeType.PDF_417and validation mode is enabled, or if either value is zero or a minimum value equal or larger than its corresponding maximum value.
 
- layersSpecifies the required number of layers for an- BarcodeType.AZTECbarcode.- This method will do nothing for other barcode formats or types if validation is not enabled, otherwise a - BarcodeValidationExceptionis thrown.- To reset the values to default, set the - layerNumbersvalue 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 not- BarcodeType.AZTECor- numberis less than- -4or larger than- 32.
 
- versionSpecifies 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 - BarcodeValidationExceptionis thrown.- To reset the version to default recommended version depending of the error correction and the barcode text content, set the - numbervalue 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 not- BarcodeType.QR_CODEor version- numberis not in the range of 1 to 40.
 
- checkParamsChecks 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.
 
- asURNStringGets the String to as a- {@link BarcodeURN}for this barcode.- Returns:
- The BarcodeURNString.
- Throws:
- BarcodeException- If there are missing or invalid parameters assigned in this builder encountered during barcode encoding.
 
- asURNGets the String to as a- {@link BarcodeURN}for this barcode.- Returns:
- The BarcodeURNString.
- Throws:
- BarcodeException- If there are missing or invalid parameters assigned in this builder encountered during barcode encoding.
 
- asSVGCreates 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.
 
- asBufferedImageCreates 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.
 
- toOutputStreamWrites 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.
 
- toOutputStreampublic 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 requested- formatName.
- 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.
 
- toFileWrites 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.
 
- toFileWrites the barcode to a file in the requested- formatNamesuch 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.
 
- getParamsStringGets the parameters as a string as:- format=type,text=contents,width=w,height=h,... - Returns:
- A string representation of set parameters.
 
- toStringReturns a string representation of this instance.
- equalsChecks if two builders are equal.
- hashCodepublic int hashCode()Gets the hashcode of this builder.
- cloneClones 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.
- getBarcodeTypeGets the barcode type or format.- Returns:
- The BarcodeType.
 
- isValidatingpublic boolean isValidating()Returns if the validation mode is enabled.- Returns:
- The validation enabled flag.
 
- getTextGets the current barcode text contents.- Returns:
- The text, or nullif not set.
 
- getWidthpublic int getWidth()Gets the width of the barcode.- Returns:
- The requested width, or zero if not set or for smallest width.
 
- getHeightpublic int getHeight()Gets the height of the barcode.- Returns:
- The requested height, or zero if not set or for smallest height.
 
- getErrorCorrectionGets the QR code error correction level.- Returns:
- The error correction level, or nullif not set or default Low (ErrorCorrectionLevel.L= ~7%) or this barcode type is not a QR code.
 
- getHintsGets a copy of the hints used for the Zxing barcode encoder.- Returns:
- A map of hints, or nullif none are set.