BarcodeGenerator

Inheritance: java.lang.Object

public final class BarcodeGenerator

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");

Constructors

ConstructorDescription
BarcodeGenerator(BaseEncodeType type)Creates an instance of BarcodeGenerator.
BarcodeGenerator(BaseEncodeType type, String codeText)Creates an instance of BarcodeGenerator.

Methods

MethodDescription
dispose()Clean up any resources being used.
equals(Object arg0)
exportToXml(OutputStream xml)Exports BarCode properties to the xml-stream specified
exportToXml(String xmlFile)Exports BarCode properties to the xml-file specified
generateBarCodeImage()Generate the barcode image under current settings.
getBarcodeType()Barcode symbology type.
getClass()
getCodeText()Text to be encoded.
getParameters()Generation parameters.
hashCode()
importFromXml(InputStream xml)Imports BarCode properties from the xml-stream specified and creates BarcodeGenerator instance.
importFromXml(String xmlFile)Imports BarCode properties from the xml-file specified and creates BarcodeGenerator instance.
notify()
notifyAll()
save(OutputStream stream, BarCodeImageFormat format)Save barcode image to stream in specific format.
save(String filename)Save barcode image to specific file.
save(String filename, BarCodeImageFormat format)Save barcode image to specific file in specific format.
setBarcodeType(BaseEncodeType value)Barcode symbology type.
setCodeText(byte[] codeBytes)Set codetext as sequence of bytes.
setCodeText(String value)Text to be encoded.
setCodeText(String codeText, Charset encoding)
setCodeText(String codeText, Charset encoding, boolean insertBOM)
toString()
wait()
wait(long arg0)
wait(long arg0, int arg1)

BarcodeGenerator(BaseEncodeType type)

public BarcodeGenerator(BaseEncodeType type)

Creates an instance of BarcodeGenerator.

Parameters:

ParameterTypeDescription
typeBaseEncodeTypeBarcode symbology type. Use EncodeTypes class to setup a symbology.

BarcodeGenerator(BaseEncodeType type, String codeText)

public BarcodeGenerator(BaseEncodeType type, String codeText)

Creates an instance of BarcodeGenerator.

Parameters:

ParameterTypeDescription
typeBaseEncodeTypeBarcode symbology type. Use EncodeTypes class to setup a symbology.
codeTextjava.lang.StringText to be encoded.

dispose()

public void dispose()

Clean up any resources being used.

equals(Object arg0)

public boolean equals(Object arg0)

Parameters:

ParameterTypeDescription
arg0java.lang.Object

Returns: boolean

exportToXml(OutputStream xml)

public boolean exportToXml(OutputStream xml)

Exports BarCode properties to the xml-stream specified

Parameters:

ParameterTypeDescription
xmljava.io.OutputStreamThe xml-stream

Returns: boolean - Whether or not export completed successfully. Returns True in case of success; False Otherwise

exportToXml(String xmlFile)

public boolean exportToXml(String xmlFile)

Exports BarCode properties to the xml-file specified

Parameters:

ParameterTypeDescription
xmlFilejava.lang.StringThe name of the file

Returns: boolean - Whether or not export completed successfully.

Returns True in case of success; False Otherwise

generateBarCodeImage()

public 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);

Returns: java.awt.image.BufferedImage - Barcode image. See Bitmap .

getBarcodeType()

public BaseEncodeType getBarcodeType()

Barcode symbology type.

Returns: BaseEncodeType

getClass()

public final native Class<?> getClass()

Returns: java.lang.Class

getCodeText()

public String getCodeText()

Text to be encoded.

Returns: java.lang.String

getParameters()

public BaseGenerationParameters getParameters()

Generation parameters.

Returns: BaseGenerationParameters

hashCode()

public native int hashCode()

Returns: int

importFromXml(InputStream xml)

public static BarcodeGenerator importFromXml(InputStream xml)

Imports BarCode properties from the xml-stream specified and creates BarcodeGenerator instance.

Parameters:

ParameterTypeDescription
xmljava.io.InputStreamThe xml-stream

Returns: BarcodeGenerator - BarcodeGenerator instance

importFromXml(String xmlFile)

public static BarcodeGenerator importFromXml(String xmlFile)

Imports BarCode properties from the xml-file specified and creates BarcodeGenerator instance.

Parameters:

ParameterTypeDescription
xmlFilejava.lang.StringThe name of the file

Returns: BarcodeGenerator - BarcodeGenerator instance

notify()

public final native void notify()

notifyAll()

public final native void notifyAll()

save(OutputStream stream, BarCodeImageFormat format)

public void save(OutputStream stream, BarCodeImageFormat format)

Save barcode image to stream in specific format.

Parameters:

ParameterTypeDescription
streamjava.io.OutputStreamOutput OutputStream.
formatBarCodeImageFormatSpecifies the file format of the output image.

save(String filename)

public void save(String filename)

Save barcode image to specific file.

Parameters:

ParameterTypeDescription
filenamejava.lang.StringPath to save to.

save(String filename, BarCodeImageFormat format)

public void save(String filename, BarCodeImageFormat format)

Save barcode image to specific file in specific format.

Parameters:

ParameterTypeDescription
filenamejava.lang.StringPath to save to.
formatBarCodeImageFormatSpecifies the file format of the output image.

setBarcodeType(BaseEncodeType value)

public void setBarcodeType(BaseEncodeType value)

Barcode symbology type.

Parameters:

ParameterTypeDescription
valueBaseEncodeType

setCodeText(byte[] codeBytes)

public void setCodeText(byte[] codeBytes)

Set codetext as sequence of bytes.

Parameters:

ParameterTypeDescription
codeBytesbyte[]Bytes of codetext

setCodeText(String value)

public void setCodeText(String value)

Text to be encoded.

Parameters:

ParameterTypeDescription
valuejava.lang.String

setCodeText(String codeText, Charset encoding)

public void setCodeText(String codeText, Charset encoding)

Encodes the Unicode codeText into a byte sequence using the specified encoding . UTF-8 is the most commonly used encoding. If the encoding supports it, the function automatically inserts a byte order mark (BOM) .

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, ECI 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 ```
SetCodeText
``` setting 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("\u8eca\u7a2e\u540d", 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("\u8eca\u7a2e\u540d", 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));

Parameters:

ParameterTypeDescription
codeTextjava.lang.StringCodeText string
encodingjava.nio.charset.CharsetApplied encoding

setCodeText(String codeText, Charset encoding, boolean insertBOM)

public void setCodeText(String codeText, Charset encoding, boolean insertBOM)

Encodes the Unicode codeText into a byte sequence using the specified encoding . UTF-8 is the most commonly used encoding. If the encoding supports it and insertBOM is set to true , the function includes a byte order mark (BOM) .

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, ECI 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 ```
SetCodeText
``` with or without a BOM for 2D barcodes.
  
 	//Encode codetext using UTF-8 with BOM
 	BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.QR);
  gen.setCodeText("\u8eca\u7a2e\u540d", 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("\u8eca\u7a2e\u540d", 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());

Parameters:

ParameterTypeDescription
codeTextjava.lang.StringCodeText string
encodingjava.nio.charset.CharsetApplied encoding
insertBOMbooleanIndicates 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.

toString()

public String toString()

Returns: java.lang.String

wait()

public final void wait()

wait(long arg0)

public final native void wait(long arg0)

Parameters:

ParameterTypeDescription
arg0long

wait(long arg0, int arg1)

public final void wait(long arg0, int arg1)

Parameters:

ParameterTypeDescription
arg0long
arg1int