The QR code is shortened for Quick Response code. It is an alternative to bar code or we can say that it is a two-dimensional bar-code. contains the matrix of small squares in which information is stored. works the same as a bar-code.
Generating QR Code in Java
The grid arrangement of the black squares on a white background that can be read by an imaging device is called a QR code. The advantage of a is that it can be scanned by smartphons. They need not to carry any handheld device for scanning. It is used to store information such as text, webpage, URL, etc. It uses the following four standardized encoding modes:
- Numeric
- Alphanumeric
- Byte/binary
import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.NotFoundException; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class GenerateQRCode { //static function that creates QR Code public static void generateQRcode(String data, String path, String charset, Map map, int h, int w) throws WriterException, IOException { //the BitMatrix class represents the 2D matrix of bits //MultiFormatWriter is a factory class that finds the appropriate Writer subclass for the BarcodeFormat requested and encodes the barcode with the supplied contents. BitMatrix matrix = new MultiFormatWriter().encode(new String(data.getBytes(charset), charset), BarcodeFormat.QR_CODE, w, h); MatrixToImageWriter.writeToFile(matrix, path.substring(path.lastIndexOf('.') + 1), new File(path)); } //main() method public static void main(String args[]) throws WriterException, IOException, NotFoundException { //data that we want to store in the QR code String str= "THE HABIT OF PERSISTENCE IS THE HABIT OF VICTORY."; //path where we want to get QR Code String path = "C:\\Users\\Anubhav\\Desktop\\QRDemo\\Quote.png"; //Encoding charset to be used String charset = "UTF-8"; Map<EncodeHintType, ErrorCorrectionLevel> hashMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>(); //generates QR code with Low level(L) error correction capability hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); //invoking the user-defined method that creates the QR code generateQRcode(str, path, charset, hashMap, 200, 200);//increase or decrease height and width accodingly //prints if the QR code is generated System.out.println("QR Code created successfully."); } }
Output: