Showing posts with label qr code. Show all posts
Showing posts with label qr code. Show all posts

Monday, 17 May 2021

Java: Read QR code

Below snippet is used to read the information from QR code.

BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new FileInputStream(filePath)))));  
Result rslt = new MultiFormatReader().decode(binaryBitmap);  
String info = result.getText();

 

Find the below working application.

 

QRCodeUtil.java

package com.sample.app.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Paths;
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.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 QRCodeUtil {

	public static boolean generateQRCode(String message, String filePath, Charset encodingFormat, int imageHeight,
			int imageWidth) {

		try {
			Map<EncodeHintType, ErrorCorrectionLevel> hashMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
			hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

			BitMatrix bitMatrix = new MultiFormatWriter().encode(new String(message.getBytes(encodingFormat)),
					BarcodeFormat.QR_CODE, imageHeight, imageWidth);

			String fileFormat = filePath.substring(filePath.lastIndexOf('.') + 1);
			MatrixToImageWriter.writeToPath(bitMatrix, fileFormat, Paths.get(filePath));
			return true;

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public static String getInfoFromQRCode(String filePath, Charset encodingFormat)
			throws FileNotFoundException, IOException, NotFoundException {
		BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new FileInputStream(filePath)))));  
		Result result = new MultiFormatReader().decode(binaryBitmap);  
		return result.getText();  
	}
}

 

QrCodeReadDemo.java

package com.sample.app;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import com.google.zxing.NotFoundException;
import com.sample.app.util.QRCodeUtil;

public class QrCodeReadDemo {
	public static void main(String args[]) throws FileNotFoundException, NotFoundException, IOException {
		String filePath = "/Users/Shared/secret.png";

		boolean isQRCodeGenerated = QRCodeUtil.generateQRCode("Transfer 1000 from account A to Account B", filePath,
				StandardCharsets.UTF_8, 250, 250);

		if (isQRCodeGenerated) {
			System.out.println("QR code generated successfully");
		} else {
			System.out.println("QR code generation failed");
		}
		
		String message = QRCodeUtil.getInfoFromQRCode(filePath, StandardCharsets.UTF_8);
		
		System.out.println(message);
		
	}
}

 

Output

QR code generated successfully
Transfer 1000 from account A to Account B

 

 

 

 

 

 

Previous                                                    Next                                                    Home

Java: Create QR code

Below snippet is used to generate qr code.

Map<EncodeHintType, ErrorCorrectionLevel> hashMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

BitMatrix bitMatrix = new MultiFormatWriter().encode(new String(message.getBytes(encodingFormat)),BarcodeFormat.QR_CODE, imageHeight, imageWidth);

String fileFormat = filePath.substring(filePath.lastIndexOf('.') + 1);
MatrixToImageWriter.writeToPath(bitMatrix, fileFormat, Paths.get(filePath));

 

Find the below working application.

 

QRCodeUtil.java

 

package com.sample.app.util;

import java.nio.charset.Charset;
import java.nio.file.Paths;
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.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCodeUtil {

	public static boolean generateQRCode(String message, String filePath, Charset encodingFormat, int imageHeight,
			int imageWidth) {

		try {
			Map<EncodeHintType, ErrorCorrectionLevel> hashMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
			hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

			BitMatrix bitMatrix = new MultiFormatWriter().encode(new String(message.getBytes(encodingFormat)),
					BarcodeFormat.QR_CODE, imageHeight, imageWidth);

			String fileFormat = filePath.substring(filePath.lastIndexOf('.') + 1);
			MatrixToImageWriter.writeToPath(bitMatrix, fileFormat, Paths.get(filePath));
			return true;

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

}


QrCodeGenerateDemo.java

package com.sample.app;

import java.nio.charset.StandardCharsets;

import com.sample.app.util.QRCodeUtil;

public class QrCodeGenerateDemo {

	public static void main(String args[]) {
		String filePath = "/Users/Shared/secret.png";

		boolean isQRCodeGenerated = QRCodeUtil.generateQRCode("Transfer 1000 from account A to Account B", filePath,
				StandardCharsets.UTF_8, 250, 250);

		if (isQRCodeGenerated) {
			System.out.println("QR code generated successfully");
		} else {
			System.out.println("QR code generation failed");
		}
	}
}


Output

QR code generated successfully

 

Generated secret.png file


Previous                                                    Next                                                    Home

Introduction to QRCode

QR Code stands for quick response code, it is a two dimensional bar code invented in 1994 by the Japanese automotive company Denso Wave.

 

Features of QR code

a. Capable of handling all types of data

QR code is capable of handling all types of data like numeric and alphabetic characters, Kanji, Kana, Hiragana, symbols, binary, and control codes. You can store upto 7089 characters in one symbol.

 

b. Smaller in size

Since QR code is a two dimensional bar code, it can store the information both horizaontally and vertically. QR Code can encode the same amount of data in approximately one-tenth the space of a traditional barcode.

 

c. Data restore capability

QR code has error correction capability. We can restore the data, even the symbol is partially damaged.

 

d. High speed reading of the content is possible.

 

I would recommend you to go through below link for more details.

https://en.wikipedia.org/wiki/QR_code

Previous                                                    Next                                                    Home

Java: QR Code introductory tutorial

In this tutorial, you are going to learn

a.   Introduction to QR code

b.   How to generate QR code

c.    How to read QR code

 

Dependencies used

<dependencies>
	<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
	<dependency>
		<groupId>com.google.zxing</groupId>
		<artifactId>core</artifactId>
		<version>3.4.1</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
	<dependency>
		<groupId>com.google.zxing</groupId>
		<artifactId>javase</artifactId>
		<version>3.4.1</version>
	</dependency>
</dependencies>

 

 

 

Previous                                                    Next                                                    Home