2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

QRコードメモ

Posted at

参考

ライブラリのダウンロードURL

https://mvnrepository.com/artifact/com.google.zxing/core/3.3.0
https://mvnrepository.com/artifact/com.google.zxing/javase/3.3.0

手順

  1. core-3.3.0.jarとjavase-3.3.0.jarをWebContent/WEB-INF/libに置く
  2. 以下のServletとJSPを作成(JSP側から実行)
qrCode.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>QRコード表示</title>
</head>
<body>
<h1>QRコード表示</h1>
<%-- <img>タグのsrc属性にQRコード描画サーブレットを指定 --%>
<img style="width: 100px; height: 100px" src="QRcodeGenerator">
</body>
</html>
QRcodeGenerator.java


import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.ConcurrentHashMap;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

/**
 * Servlet implementation class Qrcode
 */
@WebServlet("/QRcodeGenerator")
public class QRcodeGenerator extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //QRコード生成したい文字列の設定
        String source = "★★★";
        //QRコード生成時のエンコーディング
        String encoding = "UTF-8";
        //サイズ(ピクセル)
        int size = 100;

        //生成処理
        ConcurrentHashMap hints = new ConcurrentHashMap();
        //エラー訂正レベル指定
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        //エンコーディング指定
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
        //マージン指定
        hints.put(EncodeHintType.MARGIN, 0);
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix bitMatrix;
		try {
			bitMatrix = writer.encode(source, BarcodeFormat.QR_CODE, size, size, hints);
	        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
	    	// 画像をクライアントに返却する
			response.setContentType("image/png");
			OutputStream os = response.getOutputStream();
			ImageIO.write(image, "png", os);
			os.flush();
		} catch (WriterException e) {
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?