티스토리 뷰

URL을 기반으로 QRCode를 생성하는 JSP 소스를 간단하게 정리하도록 하겠습니다.

1. QRCode 생성을 위한 jar 파일 설정하기

QRCode를 생성하기 위해서 zxing 라이브러리의 core.jar와 javase.jar 파일을 사용하면 됩니다.
zxing 라이브러리는 http://code.google.com/p/zxing/에서 받을 수 있구요.
위 jar 파일을 생성하기 위해서는 ant build를 활용하면 됩니다. (ant build는 나중에 기회가 되면 한번 정리하도록 하지요.. )

하지만 검색해보면 해당 jar 파일들이 많이 있으니 그걸 사용하셔도 됩니다.
저도 하나 올려놓도록 하지요..

qrcode.zip
위 zip파일의 압축을 풀고 core.jar와 javase.jar 파일을 웹프로젝트의 WEB-INF\lib에 복사하시면 됩니다.


2. URL 입력 화면 만들기

QRCode를 생성하기 위해서 먼저 입력 화면을 만들어야 겠지요..
간단한 HTML이니 참고만 하시면 됩니다. 파일명은 index.jsp로 했네요.

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"%>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5. <title>QRCode 생성 - 미니</title>  
  6. </head>  
  7. <body>  
  8. <h1>URL QRCode 생성하기 - 미니</h1>  
  9. <form action="qrcode.jsp" method="post">  
  10.     URL 입력: <input type="text" name="url" value="" style="width: 500px" maxlength="200"/>  
  11.     <input type="submit" value="QRCode 생성"/>  
  12. </form>  
  13. </body>  
  14. </html>  


실행 화면은 다음과 같습니다.



3. QRCode 생성하기

QRCode를 생성하는 것은 생각보다 간단합니다.
다음 소스를 살펴보면 주석과 함께 적어놨으니 쉽게 이해하실 수 있을 겁니다.

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"%>  
  2. <%@ page import="java.io.File, java.util.UUID" %>  
  3. <%@ page import="java.awt.image.BufferedImage, javax.imageio.ImageIO" %>  
  4. <%@ page import="com.google.zxing.qrcode.QRCodeWriter, com.google.zxing.common.BitMatrix, com.google.zxing.BarcodeFormat, com.google.zxing.client.j2se.MatrixToImageWriter" %>  
  5. <%  
  6.     String url = request.getParameter("url");  
  7.     int nCheck = 1;  
  8.     String savedFileName = "";  
  9.   
  10.     // url 확인  
  11.     if (url == null || url.equals("") || !url.startsWith("http")) {  
  12.         nCheck = 0;  
  13.     }  
  14.     else {  
  15.         // 파일 설정  
  16.         File path = new File(application.getRealPath("/") + "qrcode/images/");  
  17.         savedFileName = UUID.randomUUID().toString().replace("-""");  
  18.         if (!path.exists()) path.mkdirs();  
  19.           
  20.         // QRCode 생성  
  21.         QRCodeWriter writer = new QRCodeWriter();  
  22.         BitMatrix qrCode = writer.encode(url, BarcodeFormat.QR_CODE, 200200);  
  23.           
  24.         BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(qrCode);  
  25.           
  26.         ImageIO.write(qrImage, "PNG"new File(path, savedFileName+".png"));  
  27.     }  
  28. %>  
  29. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  30. <html>  
  31. <head>  
  32. <title>QRCode 생성 - 미니</title>  
  33. </head>  
  34. <body>  
  35. <h1>URL QRCode 생성하기 - 미니</h1>  
  36. <%  
  37.     if (nCheck == 1) {  
  38.         String qrcode = request.getContextPath() + "/qrcode/images/" + savedFileName + ".png";  
  39.         out.print("<img src='" + qrcode + "'/><p/>");  
  40.     }  
  41.     else {  
  42.         out.print("잘못된 URL 입니다.<p/>");  
  43.     }  
  44. %>  
  45. <a href="./index.jsp">다시 생성 </a>  
  46. </body>  
  47. </html>  


역시 실행 파일은 다음과 같네요..



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함