Java

QR코드 생성하기

top2blue 2015. 7. 29. 11:07
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
 
import javax.imageio.ImageIO;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
 
/*
 pom.xml에 추가한다.
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.2.0</version>
</dependency>
        
 maven이 아닐경우 다음의 jar파일을 다운받아
 javase-x.x.x.jar 와 core-x.x.x.jar를 빌드패스에 추가한다.
 
 */
public class QRCodeTest {
    public static void main(String[] args) {
        String url = "http://m.naver.com";
        makeQRCode(url, 200200".""daum");
    }
    /*
     * QR코드 생성 유틸
     * @param url : QR 생성 URL
     * @param width : 폭
     * @param height : 높이
     * @param filePath : 폴더
     * @param fileName : 파일명
     */
    public static void makeQRCode(String url, int width,int height,String filePath,String fileName){
        File file = null;
        try {
            // 경로가 존재하지 않으면 경로 생성
            file = new File(filePath);
            if(!file.exists()){
                file.mkdirs();
            }
            // UTF-8로 인코딩된 문자열을 ISO-8859-1로 생성
            url = new String(url.getBytes("UTF-8"),"ISO-8859-1");
            // QRCodeWriter객체 생성
            QRCodeWriter writer = new QRCodeWriter();
            BitMatrix matrix = writer.encode(url, BarcodeFormat.QR_CODE, width, height);
            // 배경색과 전경색 지정
            int foregroundColor = 0xFF000000;
            int backgroundColor = 0xFFAA0000;
            MatrixToImageConfig config = new MatrixToImageConfig(foregroundColor,backgroundColor);
            // BufferedImage 객체 생성
            BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(matrix,config);
            // 이미지 저장
            ImageIO.write(qrImage, "png"new File(filePath+File.separator+fileName+".png"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
 
cs

core-3.2.0.jar

javase-3.2.0.jar

zxing
Homepage : https://code.google.com/p/zxing/
source : https://github.com/zxing/zxing

웹 상에서 QR코드 바로 만들 수 있는 URL
Homepage : http://mqr.kr/generate/url/

jQuery QR 코드 만들기
Homepage : http://jeromeetienne.github.io/jquery-qrcode/
소스 : https://github.com/jeromeetienne/jquery-qrcode
blog : http://notes.jetienne.com/2011/04/07/jquery-qrcode.html