Java QR Code

Quick Response Code (QR Code) is a two-dimensional matrix like barcode, designed by a subsidiary of Toyota to mark their vehicles for tracking in their manufacturing facilities. This is nothing but a type of barcode.
The four standard modes of data for creating QR code is numeric, alphanumeric, byte / binary and Kanji. There are extensions to these standard types available, using which custom data also can be coded.
So why is it popular? QR code can store more volume of data in small area compared to the standard barcode formats. Any place where the barcodes are being used can be replace by QR codes.
Can you scan and comment on what is in this picture?
This became easily popular because of the advent of mobile apps that can be used as a QR code scanner to read the information in QR codes. You can see advertisements in newspapers having QR codes. It may contain product information, price detail, web url, etc. If you have a smartphone with iOS or Android then you can install a QR Code app easily in a minute and scan all those codes to read the information.
Some general uses of QR codes are,
  • contact information
  • calendar event
  • URL
  • GEO location
  • website authentication
  • credit card information

Java API for QR Code

ZXing ("Zebra Crossing") is the popular API for QR code processing in Java. Its library has multiple components and we will be using the ‘core’ for QR code creation in our Java example. Following code is example to create a QR code image and read information from a QR code image.

QR Code Write and Read Program in Java

package java;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import javax.imageio.ImageIO;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 
public class QRCode {
 
  public static void main(String[] args) throws WriterException, IOException,
      NotFoundException {
    String qrCodeData = "Hello World!";
    String filePath = "QRCode.png";
    String charset = "UTF-8"; // or "ISO-8859-1"
    Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
 
    createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200);
    System.out.println("QR Code image created successfully!");
 
    System.out.println("Data read from QR Code: "
        + readQRCode(filePath, charset, hintMap));
 
  }
 
  public static void createQRCode(String qrCodeData, String filePath,
      String charset, Map hintMap, int qrCodeheight, int qrCodewidth)
      throws WriterException, IOException {
    BitMatrix matrix = new MultiFormatWriter().encode(
        new String(qrCodeData.getBytes(charset), charset),
        BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap);
    MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
        .lastIndexOf('.') + 1), new File(filePath));
  }
 
  public static String readQRCode(String filePath, String charset, Map hintMap)
      throws FileNotFoundException, IOException, NotFoundException {
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
        new BufferedImageLuminanceSource(
            ImageIO.read(new FileInputStream(filePath)))));
    Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap,
        hintMap);
    return qrCodeResult.getText();
  }
}

QR Code Write and Read Program Output

QR Code Output

Maven dependency for the ZXing QR Code library:

<dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>core</artifactId>
  <version>2.2</version>
</dependency>
 
<dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>javase</artifactId>
  <version>2.2</version>
</dependency>


 



0 comments:

Post a Comment