public final class BarcodeGenerator
extends java.lang.Object
BarcodeGenerator for backend barcode images generation.
supported symbologies: 1D: Codabar, Code11, Code128, Code39, Code39FullASCII Code93, Code93Extended, EAN13, EAN8, Interleaved2of5, MSI, Standard2of5, UPCA, UPCE, ISBN, GS1Code128, Postnet, Planet EAN14, SCC14, SSCC18, ITF14, SingaporePost ... 2D: Aztec, DataMatrix, PDf417, QR code ...
This sample shows how to create and save a barcode image.BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.CODE_128); generator.setCodeText("123ABC"); generator.save("code128.png");
| Constructor and Description |
|---|
BarcodeGenerator(BaseEncodeType type)
Creates an instance of BarcodeGenerator.
|
BarcodeGenerator(BaseEncodeType type,
java.lang.String codeText)
Creates an instance of BarcodeGenerator.
|
| Modifier and Type | Method and Description |
|---|---|
void |
dispose()
Clean up any resources being used.
|
boolean |
exportToXml(java.io.OutputStream xml)
Exports BarCode properties to the xml-stream specified
|
boolean |
exportToXml(java.lang.String xmlFile)
Exports BarCode properties to the xml-file specified
|
java.awt.image.BufferedImage |
generateBarCodeImage()
Generate the barcode image under current settings.
|
BaseEncodeType |
getBarcodeType()
Barcode symbology type.
|
java.lang.String |
getCodeText()
Text to be encoded.
|
BaseGenerationParameters |
getParameters()
Generation parameters.
|
static BarcodeGenerator |
importFromXml(java.io.InputStream xml)
Imports BarCode properties from the xml-stream specified and creates BarcodeGenerator instance.
|
static BarcodeGenerator |
importFromXml(java.lang.String xmlFile)
Imports BarCode properties from the xml-file specified and creates BarcodeGenerator instance.
|
void |
save(java.io.OutputStream stream,
BarCodeImageFormat format)
Save barcode image to stream in specific format.
|
void |
save(java.lang.String filename)
Save barcode image to specific file.
|
void |
save(java.lang.String filename,
BarCodeImageFormat format)
Save barcode image to specific file in specific format.
|
void |
setBarcodeType(BaseEncodeType value)
Barcode symbology type.
|
void |
setCodeText(byte[] codeBytes)
Set codetext as sequence of bytes.
|
void |
setCodeText(java.lang.String value)
Text to be encoded.
|
void |
setCodeText(java.lang.String codeText,
java.nio.charset.Charset encoding) |
void |
setCodeText(java.lang.String codeText,
java.nio.charset.Charset encoding,
boolean insertBOM) |
public BarcodeGenerator(BaseEncodeType type)
Creates an instance of BarcodeGenerator.
type - Barcode symbology type. Use EncodeTypes class to setup a symbology.public BarcodeGenerator(BaseEncodeType type, java.lang.String codeText)
Creates an instance of BarcodeGenerator.
type - Barcode symbology type. Use EncodeTypes class to setup a symbology.codeText - Text to be encoded.public BaseGenerationParameters getParameters()
Generation parameters.
public BaseEncodeType getBarcodeType()
Barcode symbology type.
public void setBarcodeType(BaseEncodeType value)
Barcode symbology type.
public java.lang.String getCodeText()
Text to be encoded.
public void setCodeText(java.lang.String value)
Text to be encoded.
public void setCodeText(byte[] codeBytes)
Set codetext as sequence of bytes.
codeBytes - Bytes of codetextpublic void setCodeText(java.lang.String codeText,
java.nio.charset.Charset encoding)
Encodes the Unicode <b>codeText</b> into a byte sequence using the specified <b>encoding</b>.
UTF-8 is the most commonly used encoding.
If the encoding supports it, the function automatically inserts a
<a href="https://en.wikipedia.org/wiki/Byte_order_mark#Byte-order_marks_by_encoding">byte order mark (BOM)</a>.
This function is intended for use with 2D barcodes only (e.g., Aztec, QR, DataMatrix, PDF417, MaxiCode, DotCode, HanXin, RectMicroQR, etc.).
It enables manual encoding of Unicode text using national or special encodings; however, this method is considered obsolete in modern applications.
For modern use cases, <a href="https://en.wikipedia.org/wiki/Extended_Channel_Interpretation">ECI</a> encoding is recommended for Unicode data.
Using this function with 1D barcodes, GS1-compliant barcodes (including 2D), or HIBC barcodes (including 2D) is not supported by the corresponding barcode standards and may lead to unpredictable results.
This example shows how to use
SetCodeTextsetting Unicode-encoded text for 2D barcodes using different encodings://Encode QR Code text using UTF-8 with BOM BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.QR); gen.setCodeText("車種名", StandardCharsets.UTF_8; gen.save("barcode.png", BarCodeImageFormat.PNG); BarCodeReader reader = new BarCodeReader("barcode.png", DecodeType.QR); for(BarCodeResult result : reader.readBarCodes()) System.out.println("BarCode CodeText: " + result.getCodeText()); //Encode DataMatrix text using Shift-JIS (Japanese encoding) BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.DATA_MATRIX); Charset charset = Charset.forName("Shift_JIS"); gen.setCodeText("車種名", charset); gen.save("barcode.png", BarCodeImageFormat.PNG); BarCodeReader reader = new BarCodeReader("barcode.png", DecodeType.DATA_MATRIX); for(BarCodeResult result : reader.readBarCodes()) System.out.println("BarCode CodeText: " + result.getCodeText(charset));
codeText - CodeText stringencoding - Applied encodingpublic void setCodeText(java.lang.String codeText,
java.nio.charset.Charset encoding,
boolean insertBOM)
Encodes the Unicode <b>codeText</b> into a byte sequence using the specified <b>encoding</b>.
UTF-8 is the most commonly used encoding.
If the encoding supports it and <b>insertBOM</b> is set to true, the function includes a
<a href="https://en.wikipedia.org/wiki/Byte_order_mark#Byte-order_marks_by_encoding">byte order mark (BOM)</a>.
This function is intended for use with 2D barcodes only (e.g., Aztec, QR, DataMatrix, PDF417, MaxiCode, DotCode, HanXin, RectMicroQR, etc.).
It enables manual encoding of Unicode text using national or special encodings; however, this method is considered obsolete in modern applications.
For modern use cases, <a href="https://en.wikipedia.org/wiki/Extended_Channel_Interpretation">ECI</a> encoding is recommended for Unicode data.
Using this function with 1D barcodes, GS1-compliant barcodes (including 2D), or HIBC barcodes (including 2D) is not supported by the corresponding barcode standards and may lead to unpredictable results.
This example shows how to use
SetCodeTextwith or without a BOM for 2D barcodes.//Encode codetext using UTF-8 with BOM BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.QR); gen.setCodeText("車種名", StandardCharsets.UTF_8, true); gen.save("barcode.png", BarCodeImageFormat.PNG); BarCodeReader reader = new BarCodeReader("barcode.png", DecodeType.QR); for(BarCodeResult result : reader.readBarCodes()) System.out.println("BarCode CodeText: " + result.getCodeText()); //Encode codetext using UTF-8 without BOM BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.QR); gen.setCodeText("車種名", StandardCharsets.UTF_8, false); gen.save("barcode.png", BarCodeImageFormat.PNG); BarCodeReader reader = new BarCodeReader("barcode.png", DecodeType.QR); for(BarCodeResult result : reader.readBarCodes()) System.out.println("BarCode CodeText: " + result.getCodeText());
codeText - CodeText stringencoding - Applied encodinginsertBOM - Indicates whether to insert a byte order mark (BOM) when the specified encoding supports it (e.g., UTF-8, UTF-16, UTF-32).
If set to true, the BOM is added; if false, the BOM is omitted even if the encoding normally uses one.public java.awt.image.BufferedImage generateBarCodeImage()
Generate the barcode image under current settings.
This sample shows how to create and save a barcode image.BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.CODE_128); File outputFile = new File("test.png"); BufferedImage image = generator.generateBarCodeImage(); ImageIO.write(image, "png",outputFile);
Bitmap.public void save(java.io.OutputStream stream,
BarCodeImageFormat format)
throws java.io.IOException
Save barcode image to stream in specific format.
stream - Output OutputStream.format - Specifies the file format of the output image.java.io.IOExceptionpublic void save(java.lang.String filename,
BarCodeImageFormat format)
throws java.io.IOException
Save barcode image to specific file in specific format.
filename - Path to save to.format - Specifies the file format of the output image.java.io.IOExceptionpublic void save(java.lang.String filename)
throws java.io.IOException
Save barcode image to specific file.
filename - Path to save to.java.io.IOExceptionpublic boolean exportToXml(java.lang.String xmlFile)
Exports BarCode properties to the xml-file specified
xmlFile - The name of the fileReturns <b>True</b> in case of success; <b>False</b> Otherwise
public boolean exportToXml(java.io.OutputStream xml)
throws java.io.IOException
xml - The xml-streamjava.io.IOExceptionpublic static BarcodeGenerator importFromXml(java.lang.String xmlFile)
Imports BarCode properties from the xml-file specified and creates BarcodeGenerator instance.
xmlFile - The name of the filepublic static BarcodeGenerator importFromXml(java.io.InputStream xml)
xml - The xml-streampublic void dispose()
Clean up any resources being used.