How to print, customize 2d, 1d barcode colors using Java?
If you don't want to create a typical black-and-white barcode image, OnBarcode Java Barcode Generator gives you the option to change barcode foreground color (bar color), background color, and the text color (color for the human readable text).
This can be easily achieved through some Java class coding. To start with, please create a new Java Class Project and add OnBarcode Java Library (jar) to your module libraries. Follow the sample code below, and you will create a colorful barcode!
This can be easily achieved through some Java class coding. To start with, please create a new Java Class Project and add OnBarcode Java Library (jar) to your module libraries. Follow the sample code below, and you will create a colorful barcode!
Barcode bar & space module colors
Using OnBarcode Java Barcode Generator library, you can quickly customize and print barcode bar and space modules colors through
methods
setForeColor() for bar color and setBackColor for space color in Java application.
QRCode barcode = new QRCode(); barcode.setData("https://www.onbarcode.com"); barcode.setForeColor(Color.red); barcode.setBackColor(Color.white); barcode.drawBarcode("c://Output//OnBarcode.com//java-barcode-bar-space-colors.png");
Barcode with transparent background color
Using Java Barcode library, you can generate 2d and 1d barcode in PNG image with transparent background color.
To create a barcode with transparent background, you need set the background color alpha channel as 0 in Java program.
To create a barcode with transparent background, you need set the background color alpha channel as 0 in Java program.
EAN13 barcode = new EAN13(); barcode.setData("123456789012"); barcode.setForeColor(Color.white); barcode.setBackColor(new Color(0,0,0,0)); barcode.setTextColor(Color.white); barcode.drawBarcode("c://Output//OnBarcode.com//java-barcode-background-transparent.png");
1D barcode text label color
You can customize the linear barcode human readable text color using method
setTextColor() in Java application.
Code39 barcode = new Code39(); barcode.setData("CODE-39"); barcode.setForeColor(Color.red); barcode.setBackColor(Color.white); barcode.setTextColor(Color.red); barcode.drawBarcode("c://Output//OnBarcode.com//java-barcode-text-color.png");
Print barcode in EPS format with CMYK color mode
You can draw and print a barcode in ESP image format with CMYK color mode applied using Java Barcode Generator library.
The generated barcode in EPS format viewed by Acrobat
- Set color mode as CMYK using method
setUseCMYKInEPS(true) - Choose barcode output file format as EPS in method
setOutputFileFormat(IBarcode.EPS)
EAN13 barcode = new EAN13(); barcode.setData("123456789012"); barcode.setForeColor(Color.blue); barcode.setBackColor(Color.white); barcode.setTextColor(Color.blue); barcode.setUseCMYKInEPS(true); barcode.setOutputFileFormat(IBarcode.EPS); barcode.drawBarcode("C://Output//OnBarcode.com//java-barcode-eps-cmyk-color.eps");
The generated barcode in EPS format viewed by Acrobat
How to create barcode in raster image format (PNG, JPG) in Java?
This Java Barcode Generator provides a PNG image format at default for generated barcodes. Of course, you can also opt to create barcode images in Gif and Jpeg file formats. This can easily be done through Java programming. The Java class sample code is provided here for you.
In Java class, call method
In Java class, call method
setOutputFileFormat() with target image format input.
IBarcode.PNG, print barcode in PNG image formatIBarcode.GIF, draw barcode in GIF image formatIBarcode.JPG, output barcode in JPG (JPEG) image format
Code128 barcode = new Code128(); barcode.setData("Code-128"); barcode.setOutputFileFormat(IBarcode.PNG); //barcode.setOutputFileFormat(IBarcode.GIF); //barcode.setOutputFileFormat(IBarcode.JPG); barcode.drawBarcode("C://Output//OnBarcode.com//java-barcode-raster-image-png.png");
High resolution (dpi) barcode image
To create a high quality barcode raster image for printing usage, you need make sure the generated barcode image resolution is higher than the printer's.
You can adjust the generated barcode image resolution property throguh
You can adjust the generated barcode image resolution property throguh
- Method
setResolution(): The value of the resolution should be larger than your printer's resolution.
barcode.setResolution(900);
How to create barcode in vector image format (SVG, PNG) in Java?
Using Java Barcode Generator library, you can generate and output barcodes with vector image formats in Java application.
To create a barcode in vector image format, you need choose the right output file format in barcode generation in Java code.
- SVG: Scalable Vector Graphics (SVG) is a web-friendly vector file format. It is the only vector image supported by web browsers.
- EPS: Encapsulated PostScript (EPS) is a Document Structuring Convention (DSC) conforming PostScript document format usable as a graphics file format.
To create a barcode in vector image format, you need choose the right output file format in barcode generation in Java code.
IBarcode.SVG, print barcode in SVG image formatIBarcode.EPS, draw barcode in ESP image format
Code128 barcode = new Code128(); barcode.setData("Code-128"); barcode.setOutputFileFormat(IBarcode.SVG); //barcode.setOutputFileFormat(IBarcode.EPS); barcode.drawBarcode("C://Output//OnBarcode.com//java-barcode-svg.svg");
Common Asked Questions
Why is a barcode black and white in color?
2D and 1D Barcodes usually both are black bars and white space because this creates the highest contrast and is the easiest for barcode reader software and barcode scanners to decode.
Using Java Barcode library, by default you will generate 2d and 1d barcodes in black and white in Java apps.
You can customize the barcode module and space colors through
method
setForeColor(), and setBackColor() in Java codes.
What colors should barcodes be avoided?
When you are selecting colors for barcodes, it is important that you have selected colors with high contrast.
If the bar and space module colors are both too light or too dark, the barcode scanner device or barcode reader software may not be able to decode.
Can barcodes be grey?
Yes. You can choose gray color as barcode bar modules color or space color. However you need make sure the space color and bar color have high contrast.
In Java project, you can apply the printed barcode bar color using method
setForeColor(), and barcode space color using method
setBackColor().
Do QR Code colors matter?
QR codes don't have to be black and white in order to be scannable. You can choose right colors to increase brand recognition. However you
need choose color combination with high contrast for QR Code bar and space module colors.
Using Java Barcode Generator library, you can draw, customize and apply the generated QR Code bar color through method
setForeColor(),
and barcode space color through method
setBackColor() in class com.onbarcode.barcode.QRCode.
What is SVG?
SVG stands for Scalable Vector Graphics. It is a vector image format which is perfect for barcode image rendering and viewing in web browser.
Java Barcode Generator library supports QR Code and other 2d, 1d barcode encoding and generation in SVG image format in Java class.
Is SVG better than PNG?
SVG is a vector image format, which is considered a more advanced image file type than PNG. Both image formats can be used in web browser.
OnBarcode Java Barcode library supports QR Code, Code 128, and 20+ 2d, linear barcodes generations in PNG and SVG image formats in Java applications.