Barcode-Lib4J requires Java 11+

Java Class «BarExporter»

java.lang.Object
de.vwsoft.barcodelib4j.image.BarExporter

public class BarExporter extends Object
Exports 1D and 2D barcodes to vector (PDF, EPS, SVG) and raster (PNG, BMP, JPG) images.

The following example demonstrates the general usage of this class:

     // Step 1: Create a 1D or 2D Barcode
     Barcode barcode = Barcode.newInstance(BarcodeType.CODE128);
     // ...

     // Step 2: Specify dimensions of the resulting image in millimeters
     final double widthMM = 50.0, heightMM = 30.0;

     // Step 3: Initialize a BarExporter and obtain a Graphics2D
     BarExporter exporter = new BarExporter(widthMM, heightMM);
     Graphics2D g2d = exporter.getGraphics2D();

     // Step 4: Use the specified dimensions again to draw the barcode
     barcode.draw(g2d, 0.0, 0.0, widthMM, heightMM);
     g2d.dispose();

     // Step 5: Write the image file
     try (FileOutputStream fos = new FileOutputStream("ean-13.eps")) {
       exporter.writeEPS(fos, ImageColorModel.CMYK);
     } catch (IOException ex) {
       // ...
     }
 

When exporting to raster formats (PNG, BMP, JPG) and/or when the barcode graphic is intended for later printing on a low-resolution printer, you should specify a resolution. The resolution should be used in the write method, while the draw method should receive the dot size in millimeters. The dot size is calculated using the formula: 25.4 / resolution. Example:

     // An average label printer's typical (low) resolution
     int resolutionDPI = 300;

     // Calculate the dot size in millimeters
     double dotSizeMM = 25.4 / resolutionDPI;

     // Adjust the 'draw' and the 'write' method calls
     barcode.draw(g2d, 0.0, 0.0, widthMM, heightMM, dotSizeMM, 0.0, 0.0);
     // ...
     exporter.writePNG(fos, resolutionDPI, resolutionDPI);
 
See also the description of the ImageTransform.isFlat() method for when to use horizontal and when to use vertical resolution.
  • Constructor Details

    • BarExporter

      public BarExporter(double widthMM, double heightMM)
      Constructs a new instance with the specified dimensions for the image to be created.
      Parameters:
      widthMM - the width of the image in millimeters
      heightMM - the height of the image in millimeters
      Throws:
      IllegalArgumentException - if widthMM or heightMM is <= 0
  • Method Details

    • getGraphics2D

      public Graphics2D getGraphics2D()
      Returns a Graphics2D object for drawing the barcode to be exported.

      Note: The returned Graphics2D object implements only the functionality needed for drawing barcodes. Any necessary RenderingHints are set internally. In practice, the only method you'll typically need to call is dispose() to release the object's resources when finished. The implemented methods are:

      Returns:
      a Graphics2D object for drawing the barcode to be exported
    • setTitle

      public void setTitle(String title)
      Sets the title metadata for the image file to be created.

      This is only supported for PDF, EPS, and SVG formats. The title is added to the metadata of the file. Ensure that all characters in the title string are supported by the selected file format. Note that SVG automatically escapes the characters <, >, &, ' and " to appropriate XML entities.

      Parameters:
      title - the title string to set as metadata for the image file, or null to omit it
    • setCreator

      public void setCreator(String creator)
      Sets the creator metadata for the image file to be created.

      This is only supported for EPS and PDF files, where this method sets the "Creator" metadata. For PDF, the "Producer" field is also set to the same value. Ensure that all characters in the creator string are supported by the selected file format.

      Parameters:
      creator - the creator string to set as metadata for the image file, or null to omit it
    • setOpaque

      public void setOpaque(boolean opaque)
      Sets whether the background of the exported barcode image should be opaque or transparent.

      Transparency is only supported by the PDF, EPS, SVG, and PNG formats. The default is true (opaque).

      Parameters:
      opaque - true for an opaque background or false for a transparent background
    • setForeground

      public void setForeground(CompoundColor color)
      Sets the foreground color for the exported barcode image.

      The specified color is used for the bars and any associated text elements in the barcode. The default color is CompoundColor.CC_BLACK.

      Note: Colors passed to the getGraphics2D() object are ignored. Use this method instead.

      Parameters:
      color - the foreground color for the barcode image
      Throws:
      NullPointerException - if color is null
    • setBackground

      public void setBackground(CompoundColor color)
      Sets the background color for the exported barcode image.

      The specified color is used for the spaces, quiet zones and other non-bar areas in the barcode. The default color is CompoundColor.CC_WHITE.

      Note: Colors passed to the getGraphics2D() object are ignored. Use this method instead.

      Parameters:
      color - the background color for the barcode image
      Throws:
      NullPointerException - if color is null
    • setTransform

      public void setTransform(ImageTransform transform)
      Sets the transformation for the exported barcode image.

      The default is ImageTransform.ROTATE_0.

      Parameters:
      transform - the ImageTransform to apply to the image
      Throws:
      NullPointerException - if transform is null
      See Also:
    • setTiffResolution

      public void setTiffResolution(int dpiRes)
      Sets the resolution of the embedded TIFF preview when exporting to EPS format.

      A value of 0 (default) means that no TIFF preview is embedded in the EPS file.

      EPS files can have a TIFF preview to provide a visual representation of the content, particularly useful for viewers that do not natively support EPS. A typical resolution for the TIFF preview should be at least 150 DPI, which provides sufficient quality for preview purposes without increasing the file size excessively.

      Parameters:
      dpiRes - resolution in DPI for the embedded TIFF preview, 0 to disable the preview
      Throws:
      IllegalArgumentException - if dpiRes is negative
    • write

      public void write(OutputStream out, ImageFormat format, ImageColorModel colorModel, int dpiResX, int dpiResY) throws IOException
      Writes the barcode image in one of the supported image formats.

      For raster image formats, both resolution parameters must be greater than 0.

      Parameters:
      out - the OutputStream to write the barcode image to
      format - the ImageFormat to use
      colorModel - the ImageColorModel to use
      dpiResX - the horizontal resolution in DPI
      dpiResY - the vertical resolution in DPI
      Throws:
      IOException - if an I/O error occurs while writing the image
      IllegalArgumentException - if dpiResX or dpiResY is <= 0 for raster formats
    • writePNG

      public void writePNG(OutputStream out, int dpiResX, int dpiResY) throws IOException
      Writes the barcode image in PNG format.
      Parameters:
      out - the OutputStream to write the barcode image to
      dpiResX - the horizontal resolution in DPI
      dpiResY - the vertical resolution in DPI
      Throws:
      IOException - if an I/O error occurs while writing the image
      IllegalArgumentException - if dpiResX or dpiResY is <= 0
    • writeBMP

      public void writeBMP(OutputStream out, int dpiResX, int dpiResY) throws IOException
      Writes the barcode image in BMP format.
      Parameters:
      out - the OutputStream to write the barcode image to
      dpiResX - the horizontal resolution in DPI
      dpiResY - the vertical resolution in DPI
      Throws:
      IOException - if an I/O error occurs while writing the image
      IllegalArgumentException - if dpiResX or dpiResY is <= 0
    • writeJPG

      public void writeJPG(OutputStream out, int dpiResX, int dpiResY) throws IOException
      Writes the barcode image in JPG format.

      A compression quality of 1 is always used.

      Parameters:
      out - the OutputStream to write the barcode image to
      dpiResX - the horizontal resolution in DPI
      dpiResY - the vertical resolution in DPI
      Throws:
      IOException - if an I/O error occurs while writing the image
      IllegalArgumentException - if dpiResX or dpiResY is <= 0
    • writePDF

      public void writePDF(OutputStream out, ImageColorModel colorModel) throws IOException
      Writes the barcode image in PDF format.
      Parameters:
      out - the OutputStream to write the barcode image to
      colorModel - the ImageColorModel to use
      Throws:
      IOException - if an I/O error occurs while writing the image
    • writeEPS

      public void writeEPS(OutputStream out, ImageColorModel colorModel) throws IOException
      Writes the barcode image in EPS format.
      Parameters:
      out - the OutputStream to write the barcode image to
      colorModel - the ImageColorModel to use
      Throws:
      IOException - if an I/O error occurs while writing the image
    • writeSVG

      public void writeSVG(OutputStream out) throws IOException
      Writes the barcode image in SVG format.
      Parameters:
      out - the OutputStream to write the barcode image to
      Throws:
      IOException - if an I/O error occurs while writing the image