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 aGraphics2Dobject for drawing the barcode to be exported.voidsetBackground(CompoundColor color) Sets the background color for the exported barcode image.voidsetCreator(String creator) Sets the creator metadata for the image file to be created.voidsetForeground(CompoundColor color) Sets the foreground color for the exported barcode image.voidsetOpaque(boolean opaque) Sets whether the background of the exported barcode image should be opaque or transparent.voidsetTiffResolution(int dpiRes) Sets the resolution of the embedded TIFF preview when exporting to EPS format.voidSets the title metadata for the image file to be created.voidsetTransform(ImageTransform transform) Sets the transformation for the exported barcode image.voidwrite(OutputStream out, ImageFormat format, ImageColorModel colorModel, int dpiResX, int dpiResY) Writes the barcode image in one of the supported image formats.voidwriteBMP(OutputStream out, int dpiResX, int dpiResY) Writes the barcode image in BMP format.voidwriteEPS(OutputStream out, ImageColorModel colorModel) Writes the barcode image in EPS format.voidwriteJPG(OutputStream out, int dpiResX, int dpiResY) Writes the barcode image in JPG format.voidwritePDF(OutputStream out, ImageColorModel colorModel) Writes the barcode image in PDF format.voidwritePNG(OutputStream out, int dpiResX, int dpiResY) Writes the barcode image in PNG format.voidwriteSVG(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- ifwidthMMorheightMMis <=0
-
-
Method Details
-
getGraphics2D
Returns aGraphics2Dobject for drawing the barcode to be exported.Note: The returned
Graphics2Dobject implements only the functionality needed for drawing barcodes. Any necessaryRenderingHintsare 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
Graphics2Dobject 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, ornullto 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. 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, ornullto 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-truefor an opaque background orfalsefor 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- ifcolorisnull
-
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- ifcolorisnull
-
setTransform
Sets the transformation for the exported barcode image.The default is
ImageTransform.ROTATE_0.- Parameters:
transform- theImageTransformto apply to the image- Throws:
NullPointerException- iftransformisnull- 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,0to disable the preview- Throws:
IllegalArgumentException- ifdpiResis 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- theOutputStreamto write the barcode image toformat- theImageFormatto usecolorModel- theImageColorModelto usedpiResX- the horizontal resolution in DPIdpiResY- the vertical resolution in DPI- Throws:
IOException- if an I/O error occurs while writing the imageIllegalArgumentException- ifdpiResXordpiResYis<= 0for raster formats
-
writePNG
Writes the barcode image in PNG format.- Parameters:
out- theOutputStreamto 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- ifdpiResXordpiResYis<= 0
-
writeBMP
Writes the barcode image in BMP format.- Parameters:
out- theOutputStreamto 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- ifdpiResXordpiResYis<= 0
-
writeJPG
Writes the barcode image in JPG format.A compression quality of
1is always used.- Parameters:
out- theOutputStreamto 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- ifdpiResXordpiResYis<= 0
-
writePDF
Writes the barcode image in PDF format.- Parameters:
out- theOutputStreamto write the barcode image tocolorModel- theImageColorModelto use- Throws:
IOException- if an I/O error occurs while writing the image
-
writeEPS
Writes the barcode image in EPS format.- Parameters:
out- theOutputStreamto write the barcode image tocolorModel- theImageColorModelto use- Throws:
IOException- if an I/O error occurs while writing the image
-
writeSVG
Writes the barcode image in SVG format.- Parameters:
out- theOutputStreamto write the barcode image to- Throws:
IOException- if an I/O error occurs while writing the image
-