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 Summary
ConstructorsConstructorDescriptionBarExporter
(double widthMM, double heightMM) Constructs a new instance with the specified dimensions for the image to be created. -
Method Summary
Modifier and TypeMethodDescriptionReturns aGraphics2D
object for drawing the barcode to be exported.void
setBackground
(CompoundColor color) 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
setForeground
(CompoundColor color) 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
(ImageTransform transform) Sets the transformation for the exported barcode image.void
write
(OutputStream out, ImageFormat format, ImageColorModel colorModel, 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, ImageColorModel colorModel) 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, ImageColorModel colorModel) Writes the barcode image in PDF format.void
writePNG
(OutputStream out, int dpiResX, int dpiResY) Writes the barcode image in PNG format.void
writeSVG
(OutputStream out) Writes the barcode image in SVG format.
-
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 millimetersheightMM
- the height of the image in millimeters- Throws:
IllegalArgumentException
- ifwidthMM
orheightMM
is <=0
-
-
Method Details
-
getGraphics2D
Returns aGraphics2D
object for drawing the barcode to be exported.Note: The returned
Graphics2D
object implements only the functionality needed for drawing barcodes. Any necessaryRenderingHints
are set internally. In practice, the only method you'll typically need to call isdispose()
to release the object's resources when finished. The implemented methods are:- Returns:
- a
Graphics2D
object for drawing the barcode to be exported
-
setTitle
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, ornull
to omit it
-
setCreator
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, ornull
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 orfalse
for a transparent background
-
setForeground
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
- ifcolor
isnull
-
setBackground
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
- ifcolor
isnull
-
setTransform
Sets the transformation for the exported barcode image.The default is
ImageTransform.ROTATE_0
.- Parameters:
transform
- theImageTransform
to apply to the image- Throws:
NullPointerException
- iftransform
isnull
- 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
- ifdpiRes
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
- theOutputStream
to write the barcode image toformat
- theImageFormat
to usecolorModel
- theImageColorModel
to usedpiResX
- the horizontal resolution in DPIdpiResY
- the vertical resolution in DPI- Throws:
IOException
- if an I/O error occurs while writing the imageIllegalArgumentException
- ifdpiResX
ordpiResY
is<= 0
for raster formats
-
writePNG
Writes the barcode image in PNG format.- Parameters:
out
- theOutputStream
to write the barcode image todpiResX
- the horizontal resolution in DPIdpiResY
- the vertical resolution in DPI- Throws:
IOException
- if an I/O error occurs while writing the imageIllegalArgumentException
- ifdpiResX
ordpiResY
is<= 0
-
writeBMP
Writes the barcode image in BMP format.- Parameters:
out
- theOutputStream
to write the barcode image todpiResX
- the horizontal resolution in DPIdpiResY
- the vertical resolution in DPI- Throws:
IOException
- if an I/O error occurs while writing the imageIllegalArgumentException
- ifdpiResX
ordpiResY
is<= 0
-
writeJPG
Writes the barcode image in JPG format.A compression quality of
1
is always used.- Parameters:
out
- theOutputStream
to write the barcode image todpiResX
- the horizontal resolution in DPIdpiResY
- the vertical resolution in DPI- Throws:
IOException
- if an I/O error occurs while writing the imageIllegalArgumentException
- ifdpiResX
ordpiResY
is<= 0
-
writePDF
Writes the barcode image in PDF format.- Parameters:
out
- theOutputStream
to write the barcode image tocolorModel
- theImageColorModel
to use- Throws:
IOException
- if an I/O error occurs while writing the image
-
writeEPS
Writes the barcode image in EPS format.- Parameters:
out
- theOutputStream
to write the barcode image tocolorModel
- theImageColorModel
to use- Throws:
IOException
- if an I/O error occurs while writing the image
-
writeSVG
Writes the barcode image in SVG format.- Parameters:
out
- theOutputStream
to write the barcode image to- Throws:
IOException
- if an I/O error occurs while writing the image
-