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
FieldsModifier and TypeFieldDescriptionstatic final intColor space constant for CMYK color space.static final intColor space constant for RGB color space.static final intImage format constant for BMP format.static final intImage format constant for EPS format.static final intImage format constant for JPG format.static final intImage format constant for PDF format.static final intImage format constant for PNG format.static final intImage format constant for SVG format.static final intTransformation constant for rotation by 0 degrees.static final intTransformation constant for horizontal flip (0 degrees with mirrored image).static final intTransformation constant for rotation by 180 degrees.static final intTransformation constant for horizontal and vertical flip (180 degrees with mirrored image).static final intTransformation constant for rotation by 270 degrees.static final intTransformation constant for horizontal and vertical flip (270 degrees with mirrored image).static final intTransformation constant for rotation by 90 degrees.static final intTransformation constant for vertical flip (90 degrees with mirrored image). -
Constructor Summary
ConstructorsConstructorDescriptionImageCreator(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.static booleanisFlat(int transform) Checks whether the specified transformation represents a flat rotation (0° or 180°).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(int transform) Sets the transformation for the exported barcode image.voidwrite(OutputStream out, int format, int colorSpace, 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, int colorSpace) Writes the barcode image in EPS format.voidwriteJPG(OutputStream out, int dpiResX, int dpiResY) Writes the barcode image in JPG format.voidwritePDF(OutputStream out, int colorSpace) 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.
-
Field Details
-
FORMAT_PDF
public static final int FORMAT_PDFImage format constant for PDF format.- See Also:
-
FORMAT_EPS
public static final int FORMAT_EPSImage format constant for EPS format.- See Also:
-
FORMAT_SVG
public static final int FORMAT_SVGImage format constant for SVG format.- See Also:
-
FORMAT_PNG
public static final int FORMAT_PNGImage format constant for PNG format.- See Also:
-
FORMAT_BMP
public static final int FORMAT_BMPImage format constant for BMP format.- See Also:
-
FORMAT_JPG
public static final int FORMAT_JPGImage format constant for JPG format.- See Also:
-
COLORSPACE_RGB
public static final int COLORSPACE_RGBColor space constant for RGB color space.- See Also:
-
COLORSPACE_CMYK
public static final int COLORSPACE_CMYKColor space constant for CMYK color space.- See Also:
-
TRANSFORM_0
public static final int TRANSFORM_0Transformation constant for rotation by 0 degrees.- See Also:
-
TRANSFORM_90
public static final int TRANSFORM_90Transformation constant for rotation by 90 degrees.- See Also:
-
TRANSFORM_180
public static final int TRANSFORM_180Transformation constant for rotation by 180 degrees.- See Also:
-
TRANSFORM_270
public static final int TRANSFORM_270Transformation constant for rotation by 270 degrees.- See Also:
-
TRANSFORM_0N
public static final int TRANSFORM_0NTransformation constant for horizontal flip (0 degrees with mirrored image).- See Also:
-
TRANSFORM_90N
public static final int TRANSFORM_90NTransformation constant for vertical flip (90 degrees with mirrored image).- See Also:
-
TRANSFORM_180N
public static final int TRANSFORM_180NTransformation constant for horizontal and vertical flip (180 degrees with mirrored image).- See Also:
-
TRANSFORM_270N
public static final int TRANSFORM_270NTransformation constant for horizontal and vertical flip (270 degrees with mirrored image).- See Also:
-
-
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 millimetersheightMM- the height of the image in millimeters
-
-
Method Details
-
getGraphics2D
Returns aGraphics2Dobject for drawing the barcode to be exported. Note that the returnedGraphics2Dobject implements only the functionality needed for drawing barcodes and that any necessaryRenderingHintsare set internally.The implemented methods are:
fill(Shape s)drawString(String str, float x, float y)setFont(Font font)getFont()getFontMetrics(Font f)getFontRenderContext()dispose()
The only method you are most likely to use in practice is
dispose()to release the object's resources when finished.- 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. 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
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 istrue(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 isCompoundColor.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
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 isCompoundColor.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 theTRANSFORM_*constants defined in this class. The default isTRANSFORM_0.- Parameters:
transform- any of theTRANSFORM_*constants- See Also:
-
setTiffResolution
public void setTiffResolution(int dpiRes) Sets the resolution of the embedded TIFF preview when exporting to EPS format. A value of0(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,0means 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 than0for raster image formats.- Parameters:
out- theOutputStreamto write the barcode image toformat- any of theFORMAT_*constantscolorSpace- must be eitherCOLORSPACE_RGBorCOLORSPACE_CMYKdpiResX- the horizontal resolution in DPIdpiResY- the vertical resolution in DPI- Throws:
IOException- if an I/O error occurs while writing the image
-
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 image
-
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 image
-
writeJPG
Writes the barcode image in JPG format. A compression quality of1is 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 image
-
writePDF
Writes the barcode image in PDF format.- Parameters:
out- theOutputStreamto write the barcode image tocolorSpace- must be eitherCOLORSPACE_RGBorCOLORSPACE_CMYK- 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 tocolorSpace- must be eitherCOLORSPACE_RGBorCOLORSPACE_CMYK- 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
-
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:
trueif the transformation represents a flat rotation (0 or 180 degrees),falseotherwise.- See Also:
-