Invisiblize Textual Data When Creating Barcode&QR code in Java
In this tutorial, we are going to introduce how to invisiblize the textual data in the middle of creating a Barcode&QR code using Spire.Barcode for Java and its settings.setShowText(false); method.
Invisiblize the Textual Data When Creating a Barcode
import com.spire.barcode.BarCodeGenerator; import com.spire.barcode.BarCodeType; import com.spire.barcode.BarcodeSettings; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class CreateBarcode { public static void main(String[] args) throws IOException { BarcodeSettings settings = new BarcodeSettings();//Instantiate BarcodeSettings object settings.setType(BarCodeType.Codabar);//Set the Barcode type settings.setData("123456789");//Set the Barcode data settings.setShowText(false);//Make the textual data invisible settings.setShowTextOnBottom(true);//Make the textual data visible at the bottom of the Barcode settings.setX(0.8f);//Set width for the black and white stripes settings.setImageHeight(50);//Set height for the created Barcode image settings.setImageWidth(70);//Set width for the created Barcode image settings.hasBorder(true);//Make the border visible settings.setBorderColor(new Color(135,206,250));//Set the border color settings.setBorderWidth(1);//Set the border width settings.setBackColor(new Color(240,255,255));//Set background color for the Barcode BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);//Instantiage BarCodeGenerator object BufferedImage bufferedImage = barCodeGenerator.generateImage();//Generate image data based on settings and save it to BufferedImage instance ImageIO.write(bufferedImage, "png", new File("Codabar.png"));//Save the Barcode as PNG } }
Output
Invisiblize the Textual Data When Creating a QR code
import com.spire.barcode.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class CreateQR { public static void main(String[] args) throws IOException { BarcodeSettings settings = new BarcodeSettings();//Instantiate BarcodeSettings object settings.setType(BarCodeType.QR_Code);//Set Barcode type as QR code settings.setData("Hello 123456789");//Set the data settings.setShowText(false);// Make the textual data invisible settings.setQRCodeDataMode(QRCodeDataMode.Alpha_Number);//Set data mode settings.setImageWidth(50);// Set widthfor the created QR code image settings.setImageHeight(50);// Set height for the created QR code image settings.setX(3.0f);//Set width for QR code model settings.setQRCodeECL(QRCodeECL.H);//Set QR code ECL settings.hasBorder(false);//make the border invisible BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);//Instantiage BarCodeGenerator object BufferedImage bufferedImage = barCodeGenerator.generateImage();//Generate image data based on settings and save it to BufferedImage instance ImageIO.write(bufferedImage, "png", new File("QRCode.png"));//Save the QR code as PNG } }
Output
Scan Barcode in Java
Spire.Barcode for Java supports to generate various types of Barcodes such as Code 128, Pdf 417, QR code etc. At the same time, it also supports to read data from the generated barcode images. This article demonstrates how to scan a barcode image using Spire.Barcode for Java.
Prerequisite:
Import Spire.Barcode.jar file
Download Spire.Barcode for Java and import Spire.Barcode.jar file into the project. The detail steps of how to import Spire.Barcode.jar can be found in this article: How to Create Barcode Using Spire.Barcode for Java.
Code:
To scan a barcode image, we can use the scan method in BarcodeScanner class.
import com.spire.barcode.BarCodeType; import com.spire.barcode.BarcodeScanner; public class SpireBarcode_test { public static void main(String[] args) throws Exception { String[] datas = BarcodeScanner.scan("C:\\code128.jpg", BarCodeType.Code_128); System.out.print(datas[0]); } }
Code 128 image:
Output:
Generate QR Code in Java
In the previous post, we have demonstrated how to use Spire.Barcode for Java to create barcode images in Java applications. This tutorial provides code sample for generating QR code in Java.
package GenerateBarcode; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import com.spire.barcode.BarCodeGenerator; import com.spire.barcode.BarCodeType; import com.spire.barcode.BarcodeSettings; import com.spire.barcode.QRCodeECL; import com.sun.javafx.print.Units; public class QR_Code { public static void main(String[] args) throws IOException { //create an instance of BarcodeSetteings BarcodeSettings settings = new BarcodeSettings(); //set barcode type settings.setType(BarCodeType.QR_CODE); //set barcode data settings.setData("ABC 123456789"); //set dispaly text settings.setData2D("ABC 123456789"); //show text on bottom settings.setShowTextOnBottom(true); //set the border invisible settings.hasBorder(false); //set width of the barcode module settings.setX(2); //set the error correction level settings.setQRCodeECL(QRCodeECL.M); //create BarCodeGenerator object based on settings BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings); //generate image data and store in BufferedImage instance BufferedImage bufferedImage = barCodeGenerator.generateImage(); //save to image ImageIO.write(bufferedImage,"png",new File("QR_CODE.png")); System.out.println("Complete!"); } }
Output:
How to Create Barcode Using Spire.Barcode for Java
This article presents how to import Spire.Barcode.jar in Java application and create barcode image using the classes involved.
Import Spire.Barcode.jar file
Step 1: Download Spire.Barcode for Java from the following URL. Unzip the package into a directory, you'll get Spire.Barcode.jar and Spire.Common.jar in the lib folder.
Step 2: Create a Java project in Eclipse.
Step 3: Right click the project name, select "New" - "Folder" to create a folder named "Lib".
Step 4: Copy the Spire.Barcode.jar file and Spire.Common.jar file to this folder.
Step 5: Select all the jar files, then right click on one of them and select "Build Path" – "Add to Build Path".
Until now, Spire.Barcode.jar and Spire.Common.jar have been referenced in your Java project. Expand the specific jar file in the “Package Explore” pane, and you’ll be able to view the classes, methods, properties, etc. inside the jar file.
Using the code
Following code snippet provides an example of creating Code 128 barcode using Spire.Barcode for Java.
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import com.spire.barcode.BarCodeGenerator; import com.spire.barcode.BarCodeType; import com.spire.barcode.BarcodeSettings; public class CODE_128 { public static void main(String[] args) throws IOException { //create an instance of BarcodeSetteings BarcodeSettings settings = new BarcodeSettings(); //set barcode type settings.setType(BarCodeType.CODE_128); //set barcode data settings.setData("123456789"); //set the display text settings.setData2D("123456789"); //show text on bottom settings.setShowTextOnBottom(true); //set the border invisible settings.hasBorder(false); //create BarCodeGenerator object based on settings BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings); //generate image data and store in BufferedImage instance BufferedImage bufferedImage = barCodeGenerator.generateImage(); //save to .png file format ImageIO.write(bufferedImage, "png", new File("CODE128.png")); System.out.println("Complete!"); } }
Output: