웹사이트 검색

Java QR 코드 생성기 - zxing 예제


오늘은 자바 QR코드 생성기 프로그램에 대해 알아보겠습니다. 기술 및 장치에 정통한 경우 QR 코드를 알고 있어야 합니다. 요즘에는 블로그, 웹사이트, 심지어 일부 공공 장소 등 모든 곳에서 찾을 수 있습니다. 이것은 QR 코드 스캐너 앱을 사용하여 QR 코드를 스캔하면 텍스트를 표시하거나 URL인 경우 웹 페이지로 리디렉션하는 모바일 앱에서 매우 인기가 있습니다. 나는 이것을 최근에 보았고 매우 흥미로웠다. QR 코드에 대해 알고 싶다면 Wikipedia QR 코드 페이지에서 많은 유용한 정보를 찾을 수 있습니다.

자바 QR 코드 생성기

많은 웹사이트에서 QR 코드 이미지를 발견했을 때 Java QR 코드 생성기를 찾기 시작했습니다. 일부 오픈 소스 API를 조사한 결과 zxing이 간단하고 사용하기 가장 좋은 것으로 나타났습니다. QR 코드 이미지를 생성하려면 핵심 라이브러리만 필요합니다. maven 프로젝트에 아래 종속성을 추가하기만 하면 됩니다.

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

명령줄을 통해 QR 이미지를 읽으려면 JavaSE 라이브러리를 사용해야 합니다. 아래 종속성을 추가할 수 있습니다.

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

QR 코드 이미지를 생성하는 zxing 예제

다음은 zxing API로 QR 코드 이미지를 생성하는 데 사용할 수 있는 프로그램입니다. GenerateQRCode.java

package com.journaldev.qrcode.generator;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class GenerateQRCode {

	public static void main(String[] args) throws WriterException, IOException {
		String qrCodeText = "https://www.journaldev.com";
		String filePath = "JD.png";
		int size = 125;
		String fileType = "png";
		File qrFile = new File(filePath);
		createQRImage(qrFile, qrCodeText, size, fileType);
		System.out.println("DONE");
	}

	private static void createQRImage(File qrFile, String qrCodeText, int size, String fileType)
			throws WriterException, IOException {
		// Create the ByteMatrix for the QR-Code that encodes the given String
		Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
		hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
		QRCodeWriter qrCodeWriter = new QRCodeWriter();
		BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);
		// Make the BufferedImage that are to hold the QRCode
		int matrixWidth = byteMatrix.getWidth();
		BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
		image.createGraphics();

		Graphics2D graphics = (Graphics2D) image.getGraphics();
		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, matrixWidth, matrixWidth);
		// Paint and save the image using the ByteMatrix
		graphics.setColor(Color.BLACK);

		for (int i = 0; i < matrixWidth; i++) {
			for (int j = 0; j < matrixWidth; j++) {
				if (byteMatrix.get(i, j)) {
					graphics.fillRect(i, j, 1, 1);
				}
			}
		}
		ImageIO.write(image, fileType, qrFile);
	}

}

QR 코드를 읽는 zxing 예제

테스트할 모바일 앱이 없더라도 걱정하지 마십시오. 명령줄을 통해 zxing API로 QR 코드를 읽을 수 있습니다. 아래는 QR코드 이미지 파일을 읽어오는 명령어입니다. zxing이 의존하는 클래스 경로의 추가 jar에 주목하십시오.

$java -cp $HOME/.m2/repository/com/google/zxing/javase/3.3.2/javase-3.3.2.jar:.:$HOME/.m2/repository/com/google/zxing/core/3.3.2/core-3.3.2.jar:$HOME/.m2/repository/com/beust/jcommander/1.72/jcommander-1.72.jar:$HOME/.m2/repository/com/github/jai-imageio/jai-imageio-core/1.3.1/jai-imageio-core-1.3.1.jar com.google.zxing.client.j2se.CommandLineRunner JD.png

GitHub 리포지토리에서 QR 코드 생성기 및 리더 maven 프로젝트를 다운로드할 수 있습니다.