Barcode-Lib4J requires Java 9+

Java Class «ImageCreator»  –  Save Barcode as EPS, PDF, SVG, PNG, BMP, JPG image

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

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

The following example demonstrates the general usage of this class:

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

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

     // Step 3: Initialize an ImageCreator and obtain a Graphics2D
     ImageCreator imageCreator = new ImageCreator(widthMM, heightMM);
     Graphics2D g2d = imageCreator.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")) {
       imageCreator.writeEPS(fos, ImageCreator.COLORSPACE_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);
     // ...
     imageCreator.writePNG(fos, resolutionDPI, resolutionDPI);
 
See also the description of the isFlat method for when to use horizontal and when to use vertical resolution.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Color space constant for CMYK color space.
    static final int
    Color space constant for RGB color space.
    static final int
    Image format constant for BMP format.
    static final int
    Image format constant for EPS format.
    static final int
    Image format constant for JPG format.
    static final int
    Image format constant for PDF format.
    static final int
    Image format constant for PNG format.
    static final int
    Image format constant for SVG format.
    static final int
    Transformation constant for rotation by 0 degrees.
    static final int
    Transformation constant for horizontal flip (0 degrees with mirrored image).
    static final int
    Transformation constant for rotation by 180 degrees.
    static final int
    Transformation constant for horizontal and vertical flip (180 degrees with mirrored image).
    static final int
    Transformation constant for rotation by 270 degrees.
    static final int
    Transformation constant for horizontal and vertical flip (270 degrees with mirrored image).
    static final int
    Transformation constant for rotation by 90 degrees.
    static final int
    Transformation constant for vertical flip (90 degrees with mirrored image).
  • Constructor Summary

    Constructors
    Constructor
    Description
    ImageCreator(double widthMM, double heightMM)
    Constructs a new instance with the specified dimensions for the image to be created.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a Graphics2D object for drawing the barcode to be exported.
    static boolean
    isFlat(int transform)
    Checks whether the specified transformation represents a flat rotation (0° or 180°).
    void
    Sets the background color for the exported barcode image.
    void
    setCreator(String creator)
    Sets the creator metadata for the image file to be created.
    void
    Sets the foreground color for the exported barcode image.
    void
    setOpaque(boolean opaque)
    Sets whether the background of the exported barcode image should be opaque or transparent.
    void
    setTiffResolution(int dpiRes)
    Sets the resolution of the embedded TIFF preview when exporting to EPS format.
    void
    Sets the title metadata for the image file to be created.
    void
    setTransform(int transform)
    Sets the transformation for the exported barcode image.
    void
    write(OutputStream out, int format, int colorSpace, int dpiResX, int dpiResY)
    Writes the barcode image in one of the supported image formats.
    void
    writeBMP(OutputStream out, int dpiResX, int dpiResY)
    Writes the barcode image in BMP format.
    void
    writeEPS(OutputStream out, int colorSpace)
    Writes the barcode image in EPS format.
    void
    writeJPG(OutputStream out, int dpiResX, int dpiResY)
    Writes the barcode image in JPG format.
    void
    writePDF(OutputStream out, int colorSpace)
    Writes the barcode image in PDF format.
    void
    writePNG(OutputStream out, int dpiResX, int dpiResY)
    Writes the barcode image in PNG format.
    void
    Writes the barcode image in SVG format.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • FORMAT_PDF

      public static final int FORMAT_PDF
      Image format constant for PDF format.
    • FORMAT_EPS

      public static final int FORMAT_EPS
      Image format constant for EPS format.
    • FORMAT_SVG

      public static final int FORMAT_SVG
      Image format constant for SVG format.
    • FORMAT_PNG

      public static final int FORMAT_PNG
      Image format constant for PNG format.
    • FORMAT_BMP

      public static final int FORMAT_BMP
      Image format constant for BMP format.
    • FORMAT_JPG

      public static final int FORMAT_JPG
      Image format constant for JPG format.
    • COLORSPACE_RGB

      public static final int COLORSPACE_RGB
      Color space constant for RGB color space.
    • COLORSPACE_CMYK

      public static final int COLORSPACE_CMYK
      Color space constant for CMYK color space.
    • TRANSFORM_0

      public static final int TRANSFORM_0
      Transformation constant for rotation by 0 degrees.
    • TRANSFORM_90

      public static final int TRANSFORM_90
      Transformation constant for rotation by 90 degrees.
    • TRANSFORM_180

      public static final int TRANSFORM_180
      Transformation constant for rotation by 180 degrees.
    • TRANSFORM_270

      public static final int TRANSFORM_270
      Transformation constant for rotation by 270 degrees.
    • TRANSFORM_0N

      public static final int TRANSFORM_0N
      Transformation constant for horizontal flip (0 degrees with mirrored image).
    • TRANSFORM_90N

      public static final int TRANSFORM_90N
      Transformation constant for vertical flip (90 degrees with mirrored image).
    • TRANSFORM_180N

      public static final int TRANSFORM_180N
      Transformation constant for horizontal and vertical flip (180 degrees with mirrored image).
    • TRANSFORM_270N

      public static final int TRANSFORM_270N
      Transformation constant for horizontal and vertical flip (270 degrees with mirrored image).
  • Constructor Details

    • ImageCreator

      public ImageCreator(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
  • Method Details

    • getGraphics2D

      public Graphics2D getGraphics2D()
      Returns a Graphics2D object for drawing the barcode to be exported. Note that the returned Graphics2D object implements only the functionality needed for drawing barcodes and that any necessary RenderingHints are set internally.

      The implemented methods are:

      The only method you are most likely to use in practice is dispose() to release the object's resources when finished.

      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. However, in the case of SVG, characters such as <, >, &, ' and " are allowed, as they are automatically converted to the appropriate HTML entities.
      Parameters:
      title - the title string to set as metadata for the image file
    • setCreator

      public void setCreator(String creator)
      Sets the creator metadata for the image file to be created. This method sets both the "Creator" and "Producer" metadata fields in PDF files at the same time.
      Parameters:
      creator - the creator string to set as metadata for the image file
    • setOpaque

      public void setOpaque(boolean opaque)
      Sets whether the background of the exported barcode image should be opaque or transparent. The latter option 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 that passing any color to the object returned by getGraphics2D() has no effect.

      Parameters:
      color - the foreground color for the barcode image
    • 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 that passing any color to the object returned by getGraphics2D() has no effect.

      Parameters:
      color - the background color for the barcode image
    • setTransform

      public void setTransform(int transform)
      Sets the transformation for the exported barcode image. The parameter can be any of the TRANSFORM_* constants defined in this class. The default is TRANSFORM_0.
      Parameters:
      transform - any of the TRANSFORM_* constants
      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 72 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 means disabling the preview
    • write

      public void write(OutputStream out, int format, int colorSpace, int dpiResX, int dpiResY) throws IOException
      Writes the barcode image in one of the supported image formats. Note that both resolution parameters must be greater than 0 for raster image formats.
      Parameters:
      out - the OutputStream to write the barcode image to
      format - any of the FORMAT_* constants
      colorSpace - must be either COLORSPACE_RGB or COLORSPACE_CMYK
      dpiResX - the horizontal resolution in DPI
      dpiResY - the vertical resolution in DPI
      Throws:
      IOException - if an I/O error occurs while writing the image
    • 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
    • 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
    • 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
    • writePDF

      public void writePDF(OutputStream out, int colorSpace) throws IOException
      Writes the barcode image in PDF format.
      Parameters:
      out - the OutputStream to write the barcode image to
      colorSpace - must be either COLORSPACE_RGB or COLORSPACE_CMYK
      Throws:
      IOException - if an I/O error occurs while writing the image
    • writeEPS

      public void writeEPS(OutputStream out, int colorSpace) throws IOException
      Writes the barcode image in EPS format.
      Parameters:
      out - the OutputStream to write the barcode image to
      colorSpace - must be either COLORSPACE_RGB or COLORSPACE_CMYK
      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
    • isFlat

      public static boolean isFlat(int transform)
      Checks whether the specified transformation represents a flat rotation (0° or 180°).

      This is a convenience method that can be used to determine whether the horizontal or vertical resolution of the output medium is relevant in a particular case.

      For 1D barcodes, which mainly consist of vertical bars, only one of the two resolutions is relevant. For example, when creating a 1D barcode at a 90° or 270° angle, the vertical resolution is crucial as the bar widths must be adjusted to it. Similarly, at a 0° or 180° angle, the horizontal resolution is important.

      (However, for 2D codes, if the output medium has differing horizontal and vertical resolutions, regardless of the transformation used, always use the smaller resolution. For example, in a setting of 300x600 DPI, use 300 DPI as the relevant resolution.)

      Parameters:
      transform - the transformation constant to check
      Returns:
      true if the transformation represents a flat rotation (0 or 180 degrees), false otherwise.
      See Also: