반응형
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.4.0</version>
</dependency>

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.4.0</version>
</dependency>
@RequestMapping(value = "/qr.do", method = RequestMethod.GET)
public @ResponseBody void client(HttpServletRequest request, HttpServletResponse response, 
	HttpSession session) throws IllegalStateException, IOException, WriterException {

    /** 문제 내기 **/
    int randomCode = (int) (Math.random() * 100) + 1;
    String randomCodeStr = String.valueOf(randomCode);

    String strData = new String(randomCodeStr.getBytes("UTF8"), "ISO-8859-1");

    /** qr 생성 **/
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix = qrCodeWriter.encode(strData, BarcodeFormat.QR_CODE, 512, 512); // 200,200은 width,
                                                                                            // height
    MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF2e4e96, 0xFFFFFFFF);
    BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig);

    /** 생성 qr을 화면에 노출 **/
    response.setContentType("image/png");
    OutputStream out = response.getOutputStream();

    ImageIO.write(bufferedImage, "png", out);

}
반응형