Using ComplexBarcodeGenerator

BarcodeGenerator vs ComplexBarcodeGenerator

Aspose.BarCode for Java provides two generation workflows:

  • BarcodeGenerator creates a barcode from a barcode symbology and ready-to-encode text.
  • ComplexBarcodeGenerator creates a barcode from a typed complex codetext object.

Use BarcodeGenerator when the application already has the exact text that must be encoded. Use ComplexBarcodeGenerator when the application works with a supported Complex Barcode standard, such as Swiss QR, HIBC, Royal Mail Mailmark, MaxiCode, or USA Driver ID.

The complete source code for this article is available on GitHub:

View ComplexBarcodeGeneratorExample.java

Regular barcode generation

BarcodeGenerator is the general-purpose barcode generation API. It receives the target symbology and the exact text to encode.

String codeText = "Plain application text";

BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR, codeText);
generator.save(outputPath, BarCodeImageFormat.PNG);

This is the right choice when the barcode stores application-defined text, identifiers, URLs, or other data that does not need a dedicated Complex Barcode model.

The generator does not interpret the text as Swiss QR, HIBC, Mailmark, MaxiCode, or another structured standard. It simply encodes the supplied text into the selected barcode symbology.

Complex Barcode generation

ComplexBarcodeGenerator works with typed codetext objects from the com.aspose.barcode.complexbarcode package.

For example, Swiss QR payment data is represented by SwissQRCodetext:

SwissQRCodetext codetext = new SwissQRCodetext();
codetext.getBill().setAccount("CH4431999123000889012");
codetext.getBill().setAmount(100.25);
codetext.getBill().setCurrency("CHF");

ComplexBarcodeGenerator generator = new ComplexBarcodeGenerator(codetext);
generator.save(outputPath, BarCodeImageFormat.PNG);

In this workflow, the application fills a structured Java object. Aspose.BarCode constructs the standardized payload and encodes it into the barcode symbology required by the Complex Barcode type.

Main difference

API Input Best used for
BarcodeGenerator Ready-to-encode string Plain barcodes and custom application data
ComplexBarcodeGenerator Typed complex codetext object Supported Complex Barcode standards

Both APIs can produce a barcode with the same encoded payload if the final standardized codetext is already known. However, only ComplexBarcodeGenerator lets the application work with a typed Complex Barcode object instead of manually building the standardized text.

Encoding an already constructed payload

A complex codetext object can expose the final standardized text:

String constructedCodetext = codetext.getConstructedCodetext();

This text can be encoded with the regular generator:

BarcodeGenerator regularGenerator =
        new BarcodeGenerator(EncodeTypes.QR, constructedCodetext);

This produces a QR Code that contains the same payload. However, this approach bypasses the main benefit of the Complex Barcode API: building the payload from a typed object.

Use BarcodeGenerator when:

  • the barcode content is already a final string;
  • the application uses a regular barcode symbology directly;
  • no Complex Barcode standard-specific model is needed.

Use ComplexBarcodeGenerator when:

  • the barcode represents a supported Complex Barcode standard;
  • the application should work with typed business fields;
  • the standardized payload should be constructed by Aspose.BarCode;
  • the encoded data may later need to be decoded back into a typed object.

Complete workflow

The example class demonstrates three cases:

  1. generating a plain QR Code with BarcodeGenerator;
  2. generating Swiss QR with ComplexBarcodeGenerator;
  3. comparing the payload generated by both approaches.

The important distinction is not the visual barcode image. The important distinction is the data model used by the application before generation.