Class BarcodeImage

java.lang.Object
com.codename1.qrcode.BarcodeImage

public final class BarcodeImage extends Object

Generates 1D and 2D barcodes in pure Java and draws them as an Image.

encode(int, String) returns the module grid, true for a dark module, without a quiet zone. A 1D code is a grid of one row. create(int, String, int, int) draws that grid centered in an image of the requested size, with the quiet zone the format needs, at a whole number of pixels per module so a scanner sees clean edges.

Image ean = BarcodeImage.create(BarcodeImage.EAN_13, "400638133393", 600, 200);
Image box = BarcodeImage.create(BarcodeImage.DATA_MATRIX, "PART-0042", 300, 300);
Form f = new Form("Labels", BoxLayout.y());
f.add(new Label(ean)).add(new Label(box));
f.show();

Invalid data, such as a letter in an EAN or a wrong check digit, throws IllegalArgumentException.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Aztec, the smallest compact or full symbol that fits the text as UTF-8 bytes, with about a third of the symbol spent on error correction.
    static final int
    Code 128, switching between code sets A, B and C to keep the symbol short.
    static final int
    Code 39: digits, upper case letters, space and - . $ / + %.
    static final int
    Data Matrix ECC 200, the smallest square symbol that fits the data.
    static final int
    EAN-13: 12 digits, or 13 with a correct check digit.
    static final int
    EAN-8: 7 digits, or 8 with a correct check digit.
    static final int
    Interleaved 2 of 5: an even number of digits.
    static final int
    PDF417, the stacked code on boarding passes and identity cards.
    static final int
    QR code at the medium error correction level, through QrCode.
    static final int
    UPC-A: 11 digits, or 12 with a correct check digit.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    The modulo 10 check digit of EAN-13, EAN-8 and UPC-A: weight 3 on the rightmost data digit, alternating with 1 to the left.
    static int[]
    The Code 128 symbol values for data: the start code, the data, the modulo 103 checksum and the stop code (106).
    static Image
    create(int format, String data, int width, int height)
    Draws data black on white, centered in a width by height image.
    static Image
    create(int format, String data, int width, int height, int dark, int light)
    Like create(int, String, int, int) with ARGB colors.
    static boolean[][]
    encode(int format, String data)
    Encodes data into its module grid, indexed [row][column], without a quiet zone.

    Methods inherited from class Object

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

    • CODE_128

      public static final int CODE_128
      Code 128, switching between code sets A, B and C to keep the symbol short. Takes ASCII 0 to 127.
      See Also:
    • EAN_13

      public static final int EAN_13
      EAN-13: 12 digits, or 13 with a correct check digit.
      See Also:
    • EAN_8

      public static final int EAN_8
      EAN-8: 7 digits, or 8 with a correct check digit.
      See Also:
    • UPC_A

      public static final int UPC_A
      UPC-A: 11 digits, or 12 with a correct check digit.
      See Also:
    • CODE_39

      public static final int CODE_39
      Code 39: digits, upper case letters, space and - . $ / + %.
      See Also:
    • ITF

      public static final int ITF
      Interleaved 2 of 5: an even number of digits.
      See Also:
    • DATA_MATRIX

      public static final int DATA_MATRIX
      Data Matrix ECC 200, the smallest square symbol that fits the data.
      See Also:
    • QR_CODE

      public static final int QR_CODE
      QR code at the medium error correction level, through QrCode.
      See Also:
    • PDF417

      public static final int PDF417
      PDF417, the stacked code on boarding passes and identity cards. Text is encoded as UTF-8 bytes in byte compaction mode; each row is three modules tall in the grid encode(int, String) returns.
      See Also:
    • AZTEC

      public static final int AZTEC
      Aztec, the smallest compact or full symbol that fits the text as UTF-8 bytes, with about a third of the symbol spent on error correction.
      See Also:
  • Method Details

    • encode

      public static boolean[][] encode(int format, String data)

      Encodes data into its module grid, indexed [row][column], without a quiet zone. A 1D format answers a single row.

      Parameters
      • format: one of the format constants, such as CODE_128

      • data: the text to encode

      Returns

      the grid, true for a dark module

    • create

      public static Image create(int format, String data, int width, int height)

      Draws data black on white, centered in a width by height image. A 1D code fills the height; a 2D code stays square.

      Parameters
      • format: one of the format constants, such as EAN_13

      • data: the text to encode

      • width: image width in pixels

      • height: image height in pixels

      Returns

      the barcode image

    • create

      public static Image create(int format, String data, int width, int height, int dark, int light)

      Like create(int, String, int, int) with ARGB colors. Keep the contrast high and the dark modules darker than the ground.

      Parameters
      • format: one of the format constants

      • data: the text to encode

      • width: image width in pixels

      • height: image height in pixels

      • dark: ARGB color of the dark modules

      • light: ARGB color of the ground

      Returns

      the barcode image

    • checkDigit

      public static int checkDigit(String digits)

      The modulo 10 check digit of EAN-13, EAN-8 and UPC-A: weight 3 on the rightmost data digit, alternating with 1 to the left.

      Parameters
      • digits: the data digits without a check digit
      Returns

      the check digit, 0 to 9

    • code128Codewords

      public static int[] code128Codewords(String data)

      The Code 128 symbol values for data: the start code, the data, the modulo 103 checksum and the stop code (106).

      Parameters
      • data: ASCII 0 to 127
      Returns

      the symbol values, each 0 to 106