How to convert digits, text to barcode in Java class?
All 1d and 2d barcode formats support number (digits) data encoding, and most of the popular barcode formats support plain text encoding also.
The following Java code explains how to generate a Code 128 barcode with number and string text encoded using Java.
The following Java code explains how to generate a Code 128 barcode with number and string text encoded using Java.
- Create a Code 128 barcode object using class
Code128 - Set the encoding text string with digits and characters using method
setData() - Generate and print barcode to an image file
Code128 barcode = new Code128(); barcode.setData("Code-128"); barcode.drawBarcode("c://Output//OnBarcode.com//java-barcode-data-number-text.png");
How to convert ASCII characters to barcode in Java class?
The full ASCII table includes 128 characters. There are 95 printable characters and 33 control characters (non-printing chars).
You can directly input the printable 95 ASCII characters using method
The following barcode formats support full 128 ASCII characters encoding
You can directly input the printable 95 ASCII characters using method
setData() using Java Barcode Generator library. However you cannot directly input the non-printable characters in Java IDE code editor.
The following barcode formats support full 128 ASCII characters encoding
- Code 128
- Code 39 extension mode
- Data Matrix
- PDF417
- QR Code
- Enable process tilde
setProcessTilde(true)to handle special characters, such as non-printing chars - Convert each non-printing char's ASCII byte value to a three-digit string, and add '~' in the beginning of the each char. Here a char 'carrage return' with ASCII value 13, will be converted to "~013".
- Pass the processed string to method
setData()to encode barcode
Code128 barcode = new Code128(); barcode.setProcessTilde(true); String nonPrintChar = "~013"; barcode.setData("Code" + nonPrintChar + "128"); barcode.drawBarcode("c://Output//OnBarcode.com//java-barcode-data-ascii-non-printing.png");
How to create 2d barcode with Unicode text encoded in Java application?
The ISO/IEC 8859-1 standard character set is used in various barcode types, as it covers a wide range of characters in English, French, Italian, Spanish, etc.
However, if you want to encode data from other character sets, such as Arabic, Chinese, Japenese and Greek,
you can simply convert the special char to Unicode encoding first, and then encode it in QR Code, Data Matrix or PDF-417.
Below is the Java demo code for 2D barcodes generation & encryption, in which UTF-8 encoding is applied to convert the original data to Unicode. Please note that the decoded data need to be converted back so you can get your original data for the barcode image.
Below is the Java demo code for 2D barcodes generation & encryption, in which UTF-8 encoding is applied to convert the original data to Unicode. Please note that the decoded data need to be converted back so you can get your original data for the barcode image.
- Enable Unicode text encoding using method
setEncodeUnicodeText(true)s - Input the Unicode text string in method
setData(), and print the 2d barcode to an image file
QRCode barcode = new QRCode(); barcode.setEncodeUnicodeText(true); barcode.setData("你好"); barcode.drawBarcode("c://Output//OnBarcode.com//java-barcode-data-unicode-text.png");
How to create 2d barcode with binary data in Java application?
Most of the 2d barcodes support binary data mode.
You can encode and create a 2d barcode (QR Code, Data Matrix, PDF417) from binary data, such as image, audio, or video data file.
Using OnBarcode Java Barcode Generator library, you will quickly generate 2d barcode from a binary data in Java web or desktop application.
Using OnBarcode Java Barcode Generator library, you will quickly generate 2d barcode from a binary data in Java web or desktop application.
- Set the 2d Data Matrix encoding data mode as BASE256 in
setDataMode(DataMatrix.M_BASE256). The Data Matrix Base 256 encoding mode shall be used to encode any 8-bit byte data. - Enable process tilde
setProcessTilde(true)to handle binary data - Convert each byte value to a three-digit string, and add '~' in the beginning of the each char
- Pass the processed string to method
setData()to encode and print 2d barcode
String message = "你好"; byte[] bytes = message.getBytes("UTF-8"); StringBuilder dataInBytes = new StringBuilder(); for (int i=0; i<bytes.length; i++) { int unsignedByte = bytes[i] & 0xFF; dataInBytes.append("~" + String.format("%03d", unsignedByte)); } DataMatrix barcode = new DataMatrix(); barcode.setDataMode(DataMatrix.M_BASE256); barcode.setProcessTilde(true); barcode.setData(dataInBytes.toString()); barcode.drawBarcode("c://Output//OnBarcode.com//java-barcode-data-binary.png");
How to Encode GS1 Barcodes in Java Class?
This Java Barcode Library can encode UPC/EAN Barcodes with GS1 standard pre-configured.
Alternatively, if you want to encode GS1-compatible QR Code, Data Matrix, GS1-Databar, GS1-128/EAN-128 or ITF-14,
please follow the basic Java class sample code here.
Please note that the checksum character is automatically calculated and added to some linear barcode with obligatory check digit requirement. If you want to reduce barcode size, change barcode value, customize image color / orientation, or switch to another output method, simple add more in the Java barcode syntax below.
The following Java source code will generate a GS1-128 barcode using Java Barcode Generator library.
Please note that the checksum character is automatically calculated and added to some linear barcode with obligatory check digit requirement. If you want to reduce barcode size, change barcode value, customize image color / orientation, or switch to another output method, simple add more in the Java barcode syntax below.
The following Java source code will generate a GS1-128 barcode using Java Barcode Generator library.
EAN128 barcode = new EAN128(); barcode.setData("(00)395123451234567895(01)09501101530003"); barcode.drawBarcode("c://Output//OnBarcode.com//java-barcode-data-gs1.png");
How to encode EAN/UPC add-on code using Java Barcode library?
The EAN/UPC barcodes (EAN-13, EAN-8, UPC-A, UPC-E, ISBN, ISSN) supports 2-digit or 5-digit add-on code encoding.
EAN-13 with 2 digits add-on code
The following Java source code will generate an EAN-13 barcode with 2 digits add-on code using Java Barcode Generator library.
- Set add-on code using method
setSupData()
EAN13 barcode = new EAN13(); barcode.setData("123456789012"); barcode.setSupData("01"); barcode.drawBarcode("c://Output//OnBarcode.com//java-barcode-data-ean-upc-addon-2.png");
UPC-A with 5 digits add-on code
The following Java source code will generate a UPC-A barcode with 5 digits add-on code using Java Barcode Generator library.
- Set add-on code using method
setSupData()
UPCA barcode = new UPCA(); barcode.setData("12345678901"); barcode.setSupData("12345"); barcode.drawBarcode("c://Output//OnBarcode.com//java-barcode-data-ean-upc-addon-5.png");
Java Barcode Generation Guide for Linear & 2D Barcode Types
- Java Linear Barcodes Generation:
- Java 2D Barcodes Generation: Data Matrix, PDF417, QR Code
