Class BarcodeGenerator

java.lang.Object
com.aspose.barcode.generation.BarcodeGenerator

public final class BarcodeGenerator extends Object

BarcodeGenerator for backend barcode images generation.

supported symbologies: 1D: Codabar, Code11, Code128, Code39, Code39FullASCII 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.
  
         BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.CODE_128);
         generator.setCodeText("123ABC");
         generator.save("code128.png");
        

  • Constructor Details

    • BarcodeGenerator

      public BarcodeGenerator(BaseEncodeType type)

      Creates an instance of BarcodeGenerator.

      Parameters:
      type - Barcode symbology type. Use EncodeTypes class to setup a symbology.
    • BarcodeGenerator

      public BarcodeGenerator(BaseEncodeType type, String codeText)

      Creates an instance of BarcodeGenerator.

      Parameters:
      type - Barcode symbology type. Use EncodeTypes class to setup a symbology.
      codeText - Text to be encoded.
  • Method Details

    • getParameters

      public BaseGenerationParameters getParameters()

      Generation parameters.

    • getBarcodeType

      public BaseEncodeType getBarcodeType()

      Barcode symbology type.

    • setBarcodeType

      public void setBarcodeType(BaseEncodeType value)

      Barcode symbology type.

    • getCodeText

      public String getCodeText()

      Text to be encoded.

    • setCodeText

      public void setCodeText(String value)

      Text to be encoded.

    • setCodeText

      public void setCodeText(byte[] codeBytes)

      Set codetext as sequence of bytes.

      Parameters:
      codeBytes - Bytes of codetext
    • setCodeText

      public void setCodeText(String codeText, 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 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("車種名", 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);
              gen.setCodeText("車種名", Charset.forName("932"));
              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.forName("932")));
              

      Parameters:
      codeText - CodeText string
      encoding - Applied encoding
    • setCodeText

      public void setCodeText(String codeText, 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 SetCodeText with 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());
              

      Parameters:
      codeText - CodeText string
      encoding - Applied encoding
      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 true, the BOM is added; if false, the BOM is omitted even if the encoding normally uses one.
    • generateBarCodeImage

      public android.graphics.Bitmap 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");
               OutputStream os = new FileOutputStream(outputFile);
               Bitmap generatedBitmap = generator.generateBarCodeImage();
               generatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
               os.flush();
               os.close();
              

      Returns:
      Barcode image. See Bitmap.
    • save

      public void save(OutputStream stream, BarCodeImageFormat format) throws IOException

      Save BarCodeImage to stream in specific format.

      Throws:
      IOException
    • save

      public void save(String filename, BarCodeImageFormat format) throws IOException

      Save barcode image to specific file in specific format.

      Parameters:
      filename - Path to save to.
      format - Specifies the file format of the output image.
      Throws:
      IOException
    • save

      public void save(String filename) throws IOException

      Save barcode image to specific file.

      Parameters:
      filename - Path to save to.
      Throws:
      IOException
    • exportToXml

      public boolean exportToXml(String xmlFile)

      Exports BarCode properties to the xml-file specified

      Parameters:
      xmlFile - The name of the file
      Returns:
      Whether or not export completed successfully.

      Returns <b>True</b> in case of success; <b>False</b> Otherwise

    • exportToXml

      public boolean exportToXml(OutputStream xml) throws IOException
      Exports BarCode properties to the xml-stream specified
      Parameters:
      xml - The xml-stream
      Returns:
      Whether or not export completed successfully. Returns True in case of success; False Otherwise
      Throws:
      IOException
    • importFromXml

      public static BarcodeGenerator importFromXml(String xmlFile)

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

      Parameters:
      xmlFile - The name of the file
      Returns:
      BarcodeGenerator instance
    • importFromXml

      public static BarcodeGenerator importFromXml(InputStream xml)
      Imports BarCode properties from the xml-stream specified and creates BarcodeGenerator instance.
      Parameters:
      xml - The xml-stream
      Returns:
      BarcodeGenerator instance
    • dispose

      public void dispose()

      Clean up any resources being used.