org.faceless.pdf2
Class BarCode

java.lang.Object
  extended by org.faceless.pdf2.BarCode

public class BarCode
extends Object

This class allows the creation of various bar code symbols. Simply call the appropriate method to create your BarCode then draw the result of getCanvas() onto a page:

 BarCode code = BarCode.newCode128("Code 128 Test");
 PDFCanvas canvas = code.getCanvas();
 page.drawCanvas(canvas, x, y, x+canvas.getWidth(), y+canvas.getHeight());
 

Since:
2.10 - although this class first appeared in 2.0, it was redesigned in 2.10

Field Summary
static char MAXICODE_GS
          Represents the MaxiCode "Group Seperator" characters (U+001D).
static char MAXICODE_LATCHA
          Represents the MaxiCode "LATCH-A" control character.
static char MAXICODE_LATCHB
          Represents the MaxiCode "LATCH-B" control character.
static char MAXICODE_LOCK
          Represents the MaxiCode "LOCK-A", "LOCK-B" and "LOCK-C" control characters
static char MAXICODE_SHIFTA
          Represents the MaxiCode "SHIFT-A" control character.
static char MAXICODE_SHIFTB
          Represents the MaxiCode "SHIFT-B" control character.
static char MAXICODE_SHIFTC
          Represents the MaxiCode "SHIFT-C" control character.
static char MAXICODE_SHIFTD
          Represents the MaxiCode "SHIFT-D" control character.
static char MAXICODE_SHIFTE
          Represents the MaxiCode "SHIFT-E" control character.
static char MAXICODE_THREESHIFTA
          Represents the MaxiCode "3 SHIFT-A" control character.
static char MAXICODE_TWOSHIFTA
          Represents the MaxiCode "2 SHIFT-A" control character.
 
Method Summary
 PDFCanvas getCanvas()
          Return a new PDFCanvas containing the barcode.
 float getHeight()
          Return the height of the symbol.
 float getMinimumHeight()
          Return the recommended minimum height for this symbol.
 float getWidth()
          Return the width of the symbol.
static BarCode newCodabar(String value, float xunit)
          Create a new Codabar BarCode.
static BarCode newCode128(String value)
          Create a new Code-128 BarCode with an X-unit of 1pt (0.353mm)
static BarCode newCode128(String value, double xunit)
           Create a new Code-128 BarCode.
static BarCode newCode39(String value, boolean checksum)
          Create a new Code 3 of 9 code with an X-unit of 1 and a thick/thin bar ratio of 2.8.
static BarCode newCode39(String value, boolean checksum, float xunit, float ratio)
          Create a new Code 3 of 9 code.
static BarCode newCode39Extended(String value, boolean checksum)
          Create a new Extended Code 3 of 9 ("Code 39+") code with an X-unit of 1pt (0.353mm) and a thick/thin bar ratio of 2.8 This algorithm uses the same symbology as Code 3 of 9 but can display all ASCII characters.
static BarCode newCode39Extended(String value, boolean checksum, float xunit, float ratio)
          Create a new Extended Code 3 of 9 ("Code 39+") code.
static BarCode newDeutschePostCode(String value)
          Create a new DeutschePost Interleaved 2 of 5 code.
static BarCode newDeutschePostIdentcode(int dist, int customer, int mailing)
          Create a new DeutschePost "IdentCode", a variation on Interleaved 2 of 5 with a (4,9) weighted modulo 10 checkdigit.
static BarCode newDeutschePostLeitcode(int pz, int street, int number, int product)
          Create a new DeutschePost "LeitCode", a variation on Interleaved 2 of 5 with a (4,9) weighted modulo 10 checkdigit.
static BarCode newEAN13(String value)
          Create a new GS1 EAN-13 code at a scale of 80%, giving a symbol an inch wide.
static BarCode newEAN13(String value, double scale)
          Create a new GS1 EAN-13 code.
static BarCode newIntelligentMail(String value)
          Create a new "Intelligent Mail" BarCode, a 4-state barcode used by the USPS and designed to replace PostNet.
static BarCode newInterleaved25(String value, boolean checksum)
          Create a new Interleaved 2 of 5 BarCode with an X-unit of 1pt (0.353mm) and a thick/thin ratio of 2.8.
static BarCode newInterleaved25(String value, boolean checksum, float xunit, float ratio)
          Create a new Interleaved 2 of 5 BarCode.
static BarCode newMaxiCode(int service, String postcode, int country, String address)
          Create a new MaxiCode representing an address.
static BarCode newMaxiCode(int service, String postcode, String country, String address)
          Create a new MaxiCode representing an address.
static BarCode newMaxiCode(String value, boolean eec)
          Create a new MaxiCode representing an address with the specified message.
static BarCode newPDF417(String value)
          Create a new PDF417 BarCode, a two-dimensional code defined in ISO15438 which can represent any character in Windows Codepage CP437.
static BarCode newPDF417(String value, float xunit, int securitylevel, int columns)
          Create a new PDF417 BarCode, a two-dimensional code defined in ISO15438 which can represent any character in Windows Codepage CP437.
static BarCode newPostnet(String value)
          Create a new PostNet code, a numeric barcode used by the United States Postal Service.
static BarCode newQRCode(byte[] value, double modulesize, int ecc, int version)
          Create a new QR-Code which encodes a series of raw bytes.
static BarCode newQRCode(String value, double modulesize, int ecc, int version)
          Create a new QR-Code, a two-dimensional algorithm defined in ISO18004 and originally developed by Denso corporation.
static BarCode newRM4SCC(String value)
          Create a new Royal Mail 4-state Customer Code, an algorithm used by the Royal Mail in the UK.
 void setColor(Paint color)
          Set the color of the BarCode.
 void setHeight(float height)
          Set the height of this symbol.
 void setInkBleed(double x, double y)
          Set the ink bleed amount - the number of points the bars are expected to expand in a horizontal and vertical direction due to ink bleed or other quirks of the printing process.
 void setShowText(boolean show)
          Request that a human-readable version of the barcode value is displayed below the code.
 void setWidth(float width)
          Set the width of this symbol.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAXICODE_SHIFTA

public static final char MAXICODE_SHIFTA
Represents the MaxiCode "SHIFT-A" control character.

See Also:
Constant Field Values

MAXICODE_SHIFTB

public static final char MAXICODE_SHIFTB
Represents the MaxiCode "SHIFT-B" control character.

See Also:
Constant Field Values

MAXICODE_SHIFTC

public static final char MAXICODE_SHIFTC
Represents the MaxiCode "SHIFT-C" control character.

See Also:
Constant Field Values

MAXICODE_SHIFTD

public static final char MAXICODE_SHIFTD
Represents the MaxiCode "SHIFT-D" control character.

See Also:
Constant Field Values

MAXICODE_SHIFTE

public static final char MAXICODE_SHIFTE
Represents the MaxiCode "SHIFT-E" control character.

See Also:
Constant Field Values

MAXICODE_TWOSHIFTA

public static final char MAXICODE_TWOSHIFTA
Represents the MaxiCode "2 SHIFT-A" control character.

See Also:
Constant Field Values

MAXICODE_THREESHIFTA

public static final char MAXICODE_THREESHIFTA
Represents the MaxiCode "3 SHIFT-A" control character.

See Also:
Constant Field Values

MAXICODE_LATCHA

public static final char MAXICODE_LATCHA
Represents the MaxiCode "LATCH-A" control character.

See Also:
Constant Field Values

MAXICODE_LATCHB

public static final char MAXICODE_LATCHB
Represents the MaxiCode "LATCH-B" control character.

See Also:
Constant Field Values

MAXICODE_LOCK

public static final char MAXICODE_LOCK
Represents the MaxiCode "LOCK-A", "LOCK-B" and "LOCK-C" control characters

See Also:
Constant Field Values

MAXICODE_GS

public static final char MAXICODE_GS
Represents the MaxiCode "Group Seperator" characters (U+001D). This is commonly used when creating addresses for delivery.

See Also:
Constant Field Values
Method Detail

newInterleaved25

public static BarCode newInterleaved25(String value,
                                       boolean checksum)
Create a new Interleaved 2 of 5 BarCode with an X-unit of 1pt (0.353mm) and a thick/thin ratio of 2.8. This algorithm is only suitable for numbers, and requires an even number of digits (a leading "0" will be automatically added if required).

Parameters:
value - the value of the BarCode
checksum - whether to apply a check digit using the standard (1,3) weighted modulo 10 algorithm

newInterleaved25

public static BarCode newInterleaved25(String value,
                                       boolean checksum,
                                       float xunit,
                                       float ratio)
Create a new Interleaved 2 of 5 BarCode. This algorithm is only suitable for numbers, and requires an even number of digits (a leading "0" will be automatically added if required).

Parameters:
value - the value of the BarCode
checksum - whether to apply a check digit using the standard (1,3) weighted modulo 10 algorithm
xunit - the size of the X-Dimension of the barcode, in points.
ratio - the ratio of thick-bar width to thin-bar width - typically between 2 and 3

newDeutschePostLeitcode

public static BarCode newDeutschePostLeitcode(int pz,
                                              int street,
                                              int number,
                                              int product)
Create a new DeutschePost "LeitCode", a variation on Interleaved 2 of 5 with a (4,9) weighted modulo 10 checkdigit.

Parameters:
pz - the postal zone (5 digits)
street - the street number (3 digits)
number - the house number (3 digits)
product - the product number (2 digits)

newDeutschePostIdentcode

public static BarCode newDeutschePostIdentcode(int dist,
                                               int customer,
                                               int mailing)
Create a new DeutschePost "IdentCode", a variation on Interleaved 2 of 5 with a (4,9) weighted modulo 10 checkdigit.

Parameters:
dist - the distribution center code (2 digits)
customer - the customer number (3 digits)
mailing - the mailing number (6 digits)

newDeutschePostCode

public static BarCode newDeutschePostCode(String value)
Create a new DeutschePost Interleaved 2 of 5 code. This algorithm is only suitable for numbers, and requires an odd number of digits (a leading "0" will be automatically added if required). A (4,9) weighted modulo-10 checkdigit will be added.

Parameters:
value - the value of the BarCode

newCode39

public static BarCode newCode39(String value,
                                boolean checksum)
Create a new Code 3 of 9 code with an X-unit of 1 and a thick/thin bar ratio of 2.8. This algorithm can display digits, the 26 upper-case letters, the space character and the symbols '-', '+', '/', '.', '$' and '%'.

Parameters:
value - the value of the BarCode
checksum - whether to apply the standard Code 3 of 9 checksum algorithm

newCode39

public static BarCode newCode39(String value,
                                boolean checksum,
                                float xunit,
                                float ratio)
Create a new Code 3 of 9 code. This algorithm can display digits, the 26 upper-case letters, the space character and the symbols '-', '+', '/', '.', '$' and '%'.

Parameters:
value - the value of the BarCode
checksum - whether to apply the standard Code 3 of 9 checksum algorithm
xunit - the size of the X-Dimension of the barcode, in points.
ratio - the ratio of thick-bar width to thin-bar width - typically between 2 and 3

newCode39Extended

public static BarCode newCode39Extended(String value,
                                        boolean checksum)
Create a new Extended Code 3 of 9 ("Code 39+") code with an X-unit of 1pt (0.353mm) and a thick/thin bar ratio of 2.8 This algorithm uses the same symbology as Code 3 of 9 but can display all ASCII characters. The re-encoding algorithm is described on this page.

Parameters:
value - the value of the BarCode
checksum - whether to apply the standard Code 3 of 9 checksum algorithm

newCode39Extended

public static BarCode newCode39Extended(String value,
                                        boolean checksum,
                                        float xunit,
                                        float ratio)
Create a new Extended Code 3 of 9 ("Code 39+") code. This algorithm uses the same symbology as Code 3 of 9 but can display all ASCII characters. The re-encoding algorithm is described on this page.

Parameters:
value - the value of the BarCode
checksum - whether to apply the standard Code 3 of 9 checksum algorithm
xunit - the size of the X-Dimension of the barcode, in points.
ratio - the ratio of thick-bar width to thin-bar width - typically between 2 and 3

newEAN13

public static BarCode newEAN13(String value)
Create a new GS1 EAN-13 code at a scale of 80%, giving a symbol an inch wide. An EAN-13 code represents a 13 digit number - broadly speaking, the first 7 digits are the country and manufacturer code, the next 5 digits are the product code, and the final digit the checksum. The 12 digit UPC-A barcodes as used in the USA are just EAN-13 codes with a leading "0".

Parameters:
value - the value of the BarCode - 12 or 13 digits (if 12, the check digit will be appended)

newEAN13

public static BarCode newEAN13(String value,
                               double scale)
Create a new GS1 EAN-13 code. An EAN-13 code represents a 13 digit number - broadly speaking, the first 7 digits are the country and manufacturer code, the next 5 digits are the product code, and the final digit the checksum. The 12 digit UPC-A barcodes as used in the USA are just EAN-13 codes with a leading "0".

Parameters:
value - the value of the BarCode - 12 or 13 digits (if 12, the check digit will be appended)
scale - the scale of the code - typically from 0.8 to 2, representing 80% to 200% scale. A value of 1 results in an X-dimension of 0.936pt (0.33mm).

newCodabar

public static BarCode newCodabar(String value,
                                 float xunit)
Create a new Codabar BarCode. Codabar is an elderly symbology that can represent digits and the letters A, B, C, D, which must be used as start/end letters of the code.

Parameters:
value - the value of the BarCode
xunit - the size of the X-Dimension of the barcode in points

newCode128

public static BarCode newCode128(String value)
Create a new Code-128 BarCode with an X-unit of 1pt (0.353mm)

Parameters:
value - the value of the BarCode
See Also:
newCode128(String, double)

newCode128

public static BarCode newCode128(String value,
                                 double xunit)

Create a new Code-128 BarCode. This code can display digits, upper and lower-case letters and most punctuation characters from the U+0000 - U+007E (US-ASCII) range. A checksum is automatically included as part of the barcode.

Code 128 has three "character sets" - A, B or C - which encode different subsets of characters. At any time during the encoding process, a subset can be chosen by embedding the characters U+0091, U+0092 or U+0093 into the String being encoded, to switch to set A, B or C respectively. If no initial character set is chosen, the most appropriate one will be used. So, for example, to force a String to be encoded using set A, you could specify the string \u0092CODEHERE.

GS1 EAN128 barcodes can also be printed by using a newline (\n) to represent the FNC1 control character.

Parameters:
value - the value of the BarCode
xunit - The size of the X-Dimension of the barcode in points. GS1-128 codes recommend an X-unit of 1.4pt (0.495mm)

newPostnet

public static BarCode newPostnet(String value)
Create a new PostNet code, a numeric barcode used by the United States Postal Service. This algorithm encodes information in the height of the bars, so the overall height is fixed to 9 points.

Parameters:
value - the value of the BarCode

newRM4SCC

public static BarCode newRM4SCC(String value)
Create a new Royal Mail 4-state Customer Code, an algorithm used by the Royal Mail in the UK. This code can represent the digits 0-9 and the upper case letters A-Z. This algorithm encodes information in the height of the bars, so the overall height is fixed to 25 points (this includes the 2mm quiet zone top and bottom).

Parameters:
value - the value of the BarCode

newIntelligentMail

public static BarCode newIntelligentMail(String value)
Create a new "Intelligent Mail" BarCode, a 4-state barcode used by the USPS and designed to replace PostNet.

Parameters:
value - a String of digits between 20 and 31 digits long
Since:
2.10.4

newPDF417

public static BarCode newPDF417(String value)
Create a new PDF417 BarCode, a two-dimensional code defined in ISO15438 which can represent any character in Windows Codepage CP437. An appropriate level of error correction and aspect ratio are chosen automatically, and the X-Dimension is set to 1pt (0.353mm)

Parameters:
value - the value of the BarCode
See Also:
newPDF417(String, float, int, int)

newPDF417

public static BarCode newPDF417(String value,
                                float xunit,
                                int securitylevel,
                                int columns)
Create a new PDF417 BarCode, a two-dimensional code defined in ISO15438 which can represent any character in Windows Codepage CP437. The code includes a variable degree of error correction - the recommended maximum is roughly 1600 ASCII characters or 1900 digits and the absolute maximum with no error correction is roughly 1800 ASCII characters of 2700 digits. The Compact, ECI and Macro extensions to PDF417 are not supported.

Parameters:
value - the value of the BarCode
xunit - the X-dimension of the barcode in points - typically around 1.
securitylevel - how much error correction to apply: -1 to choose an appropriate level or a value from 0 (none) to 8 (512 bytes of error correction). Recommended values are 2 for up to about 70 characters, 3 for up to about 200, 4 for up to about 400 and 5 for more. Remember setting this too high for large volumes of text may cause the maxiumum number of codewords to be reached, which will cause an exception.
columns - the number of data codeword columns in the code - a value greater than 0 will fix the width of the code and allow only the height to vary, whereas 0 will allow the code to auto-size to fit the available space (recommended).

newMaxiCode

public static BarCode newMaxiCode(String value,
                                  boolean eec)
Create a new MaxiCode representing an address with the specified message. MaxiCode is a two-dimensional algorithm originally developed by UPS, with a fixed size of 80x80 points. It may encode up to 93 alphanumeric characters from ISO-8859-1 or 138 digits, encoded using several "Code Sets". Typically an appropriate set will be chosen automatically, but for more control the control characters like MAXICODE_SHIFTA, MAXICODE_LATCHB and MAXICODE_LOCK may be inserted directly into the value.

Parameters:
value - the value of the BarCode
eec - whether to use Extended Error Correction (mode 5).

newMaxiCode

public static BarCode newMaxiCode(int service,
                                  String postcode,
                                  int country,
                                  String address)
Create a new MaxiCode representing an address. Both numeric and alphanumeric postcodes can be supplied, which will result in mode 2 or 3 MaxiCodes respectively. For example, to create a MaxiCode for a Belgian delivery address.
 BarCode code = BarCode.newMaxiCode(999, "B1050", 57,
     "Comité Européean de Normalisation"+BarCode.MAXICODE_GS+
     "rue de Stassart 36"+BarCode.MAXICODE_GS+"Bruxelles");
 

Parameters:
service - the service level, from 1 to 999
postcode - the postcode of the address, either up to nine numeric digits or up to six alphanumeric digits
country - the numeric ISO-3166 country code
address - the address to place in the secondary message. Lines in the address should be separated with the MAXICODE_GS character.

newMaxiCode

public static BarCode newMaxiCode(int service,
                                  String postcode,
                                  String country,
                                  String address)
Create a new MaxiCode representing an address. As for newMaxiCode(int, String, int, String) but the country is be specified as a two-letter ISO-3166 country code. For example, to create a MaxiCode for a US address:
class="example">
 BarCode code = BarCode.newMaxiCode(1, "524032140", "US",
     "AIM USA"+BarCode.MAXICODE_GS+"634 ALPHA DRIVE"+BarCode.MAXICODE_GS+"PITTSBURGH PA");
 

Parameters:
service - the service level, from 1 to 999
postcode - the postcode of the address, either up to nine numeric digits or up to six alphanumeric digits
country - the two-letter ISO-3166 country code
address - the address to place in the secondary message. Lines in the address should be separated with the MAXICODE_GS character.

newQRCode

public static BarCode newQRCode(String value,
                                double modulesize,
                                int ecc,
                                int version)
Create a new QR-Code, a two-dimensional algorithm defined in ISO18004 and originally developed by Denso corporation. It can encode up to 7089 numeric characters, 4296 alphanumeric latin or 1817 Japanese characters from the Shift-JIS character set (ie. all Latin and Japanese characters). It can be scanned in high speed in any direction and at the time of writing is the most information-dense format available. The "Structured Append" mode is not supported.

Parameters:
value - the value of the BarCode
modulesize - The size of a module in mm. This value depends on the scanning device, but recommended absolute minimum values for 600dpi printing are 0.17, 0.21 or 0.25mm. 0.5mm seems to be fairly typical.
ecc - the level of error correction, from 1 (lowest) to 4 (highest) offering 7%, 15%, 25% and 30% redundancy respectively. If in doubt we recommend 2.
version - the "version" or size of the code - 0 to auto-size, or a value from 1 to 40.

newQRCode

public static BarCode newQRCode(byte[] value,
                                double modulesize,
                                int ecc,
                                int version)
Create a new QR-Code which encodes a series of raw bytes. Up to 2593 bytes may be encoded.

Parameters:
value - the value of the BarCode - up to 2593 bytes.
modulesize - The size of a module in mm. This value depends on the scanning device, but recommended absolute minimum values for 600dpi printing are 0.17, 0.21 or 0.25mm. 0.5mm seems to be fairly typical.
ecc - the level of error correction, from 1 (lowest) to 4 (highest) offering 7%, 15%, 25% and 30% redundancy respectively. If in doubt we recommend 2.
version - the "version" or size of the code - 0 to auto-size, or a value from 1 to 40.
See Also:
newQRCode(String, double, int, int)

setInkBleed

public void setInkBleed(double x,
                        double y)
Set the ink bleed amount - the number of points the bars are expected to expand in a horizontal and vertical direction due to ink bleed or other quirks of the printing process. A negative number will reduce the bar by the specified amount and a positive number will increase it. Typically this can be left at zero.

Parameters:
x - the number of points to adjust the bar width by
y - the number of poitns to adjust the bar height by.
Since:
2.10.2

setColor

public void setColor(Paint color)
Set the color of the BarCode. A value of null uses the default color, which is black in the GrayScale colorspace

Since:
2.10.3

getWidth

public float getWidth()
Return the width of the symbol. This will include any mandatory quiet zone required by the algorithm, and will match the width of the canvas returned by getCanvas()


setWidth

public void setWidth(float width)
Set the width of this symbol. Not all algorithms can be resized, and for some (eg MaxiCode, the various postal bar codes) this method will throw an IllegalArgumentException. For other algorithms, this will request the size of the symbol is set to no larger than the specified width (the actual width chosen result may be smaller). This method will also increase the height of the barcode to at least that returned by getMinimumHeight()

Parameters:
width - the requested width of the symbol in points.

getMinimumHeight

public float getMinimumHeight()
Return the recommended minimum height for this symbol. The height for most codes is a function of the width, and will also include additional space if text is to be displayed with the symbol. Although codes with heights less than this height may (depending on symbology) be created, they are not guaranteed to be readable by an open system implementation.

Since:
2.10

setShowText

public void setShowText(boolean show)
Request that a human-readable version of the barcode value is displayed below the code. Not all codes support this - none of the two-dimensional codes do, for instance. Altering this value will alter the value returned from getHeight() and getMinimumHeight()


getHeight

public float getHeight()
Return the height of the symbol. This will include space for the quiet zone and any text if setShowText(boolean) is true, and the returned value will match the height of the canvas returned by getCanvas().


setHeight

public void setHeight(float height)
Set the height of this symbol. The specified value should be more than the value returned by getMinimumHeight(). Not all symbologies can have their height adjusted, and this method may throw an IllegalArgumentException if that is the case.

Parameters:
height - the full height of the canvas to be returned by getCanvas(), in points.

getCanvas

public PDFCanvas getCanvas()
Return a new PDFCanvas containing the barcode. The dimensions will match those returned by getWidth() and getHeight(). Although we suggest drawing it onto the page at the size it's returned, it is possible to stretch the canvas to suit.
 PDFCanvas canvas = barcode.getCanvas();
 page.drawCanvas(canvas, x, y, x+canvas.getWidth(), y+canvas.getHeight()):
 

Since:
2.10


Copyright © 2001-2008 Big Faceless Organization