Aspose.Barcode for Python via Java Generation and Recognition API docs
BarcodeGenerator Class Reference

BarcodeGenerator for backend barcode images generation. More...

Inherits _MwWrapper.

Public Member Functions

def __init__ (self, encode_type, Optional[str] code_text=None)
 BarcodeGenerator constructor. More...
 
str __str__ (self)
 
EncodeTypes barcode_type (self)
 Barcode symbology type. More...
 
None barcode_type (self, EncodeTypes value)
 Barcode symbology type. More...
 
str code_text (self)
 Text to be encoded. More...
 
bool export_to_xml (self, str file_path)
 Exports BarCode properties to the xml-stream specified. More...
 
Image generate_barcode_image (self)
 Generate the barcode image under current settings. More...
 
BarcodeGenerator import_from_xml (cls, str resource)
 Imports BarCode properties from the xml-file specified and creates BarcodeGenerator instance. More...
 
BaseGenerationParameters parameters (self)
 Generation parameters. More...
 
None save (self, str imagePath, BarCodeImageFormat imageFormat)
 Save barcode image to specific file in specific format. More...
 
None set_code_text (self, Union[str, bytes] code_text, Optional[str] encoding, Optional[bool] BoM)
 

Detailed Description

BarcodeGenerator for backend barcode images generation.

Supported symbologies:

1D: Codabar, Code11, Code128, Code39Standard, Code39Extended Code93Standard, 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.

encode_type = EncodeTypes.CODE_128
generator = BarcodeGenerator(encode_type, None)
generator.code_text = "123ABCDFVC"
generator.code_text_encoding = "UTF-8"
generator.save(image_path_to_save1, BarCodeImageFormat.PNG)

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  encode_type,
Optional[str]   code_text = None 
)

BarcodeGenerator constructor.

Parameters
argsmay take the following combinations of arguments: 1) Barcode symbology type. Use EncodeTypes class to setup a symbology 2) type EncodeTypes, Text to be encoded.
barcodeGenerator = BarcodeGenerator(EncodeTypes.EAN_14, "332211")
Exceptions
BarCodeException

Member Function Documentation

◆ __str__()

str __str__ (   self)
Returns a string representation of the BarcodeGenerator object, showing key information.

◆ barcode_type() [1/2]

EncodeTypes barcode_type (   self)

Barcode symbology type.

◆ barcode_type() [2/2]

None barcode_type (   self,
EncodeTypes  value 
)

Barcode symbology type.

◆ code_text()

str code_text (   self)

Text to be encoded.

◆ export_to_xml()

bool export_to_xml (   self,
str  file_path 
)

Exports BarCode properties to the xml-stream specified.

Parameters
file_pathThe path to the file where the XML will be saved.
Returns
: Whether the export completed successfully. Returns True in case of success; False Otherwise.

◆ generate_barcode_image()

Image generate_barcode_image (   self)

Generate the barcode image under current settings.

This sample shows how to create and save a barcode image.

generator = BarcodeGenerator(EncodeTypes.CODE_128, "123ABCDEFG")
pillow_image = generator.generate_barcode_image()
pillow_image.save(image_path_to_save)
@return: Pillow Image object of barcode image

◆ import_from_xml()

BarcodeGenerator import_from_xml (   cls,
str  resource 
)

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

Parameters
resource: The name of the file
Returns
: instance

◆ parameters()

BaseGenerationParameters parameters (   self)

Generation parameters.

Returns
BaseGenerationParameters

◆ save()

None save (   self,
str  imagePath,
BarCodeImageFormat  imageFormat 
)

Save barcode image to specific file in specific format.

Parameters
imagePathPath to save to.
imageFormatOptional format override. If omitted, the format to use is determined from the filename extension. If a file object was used instead of a filename, this parameter should always be used.
generator = BarcodeGenerator(EncodeTypes.CODE_128, "123ABCDEFG")
generator.save(image_path_to_save3, BarCodeImageFormat.PNG)

◆ set_code_text()

None set_code_text (   self,
Union[str, bytes]  code_text,
Optional[str]  encoding,
Optional[bool]  BoM 
)
<p>
 <p>
 Encodes the Unicode {@code <b>codeText</b>} into a byte sequence using the specified {@code <b>encoding</b>}.
 UTF-8 is the most commonly used encoding.
 If the encoding supports it and {@code <b>insertBOM</b>} is set to {@code true}, the function includes a
 {@code <a href="https://en.wikipedia.org/wiki/Byte_order_mark#Byte-order_marks_by_encoding">byte order mark (BOM)</a>}.
 </p>
 <p>
 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, {@code <a href="https://en.wikipedia.org/wiki/Extended_Channel_Interpretation">ECI</a>} encoding is recommended for Unicode data.
 </p>
 <p>
 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.
 </p>
 </p><p><hr><blockquote><pre>
 <p>This example shows how to use {@code SetCodeText} with or without a BOM for 2D barcodes.</p>
 <pre>
 \code
    # Encode codetext using UTF-8 with BOM
    generator = BarcodeGenerator(EncodeTypes.QR, None)
    generator.set_code_text("車種名", "UTF-8", True)
    generator.save("barcode.png", BarCodeImageFormat.PNG)

    reader = BarCodeReader("barcode.png", DecodeType.QR)
    for result in reader.read_barcodes():
        print("BarCode CodeText:", result.code_text)


    # Encode codetext using UTF-8 without BOM
    generator = BarcodeGenerator(EncodeTypes.QR, None)
    generator.set_code_text("車種名", "UTF-8", False)
    generator.save("barcode.png", BarCodeImageFormat.PNG)

    reader = BarCodeReader("barcode.png", DecodeType.QR)
    for result in reader.read_barcodes():
        print("BarCode CodeText:", result.code_text)
 \endcode
    </pre>
 </pre></blockquote></hr></p>
@param codeText CodeText string
@param encoding Applied encoding
@param insertBOM
 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 {@code true}, the BOM is added; if {@code false}, the BOM is omitted even if the encoding normally uses one.