Develop

QR Code 생성 및 출력하기 본문

웹 개발/Java

QR Code 생성 및 출력하기

개발 기록 2024. 5. 16. 10:35

Maven QR Code 의존성 추가

google zxing 3.3.0버전을 사용함 

<!--    QR Code     -->
        <!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>

 

 


 

QR Code 생성 및 새화면에 QR Code 띄우기

 

Controller

    /**
     * QRCode 생성
     */
    @GetMapping("/createQr/{eqpmntSn}")
    public Object createQr(@ModelAttribute("searchVo") EqpmntVo searchVo
                        ,EqpmntVo eqpmntVo
                        , Model model) throws Exception {
        // 주소
        String codeUrl = new String(("http://example/eqpmntQr/" + eqpmntVo.getEqpmntSn()).getBytes("UTF-8"), "ISO-8859-1");
        
        // QR코드 생성 및 출력
        BitMatrix qr = new MultiFormatWriter().encode(codeUrl, BarcodeFormat.QR_CODE, 600, 600);  // 크기 600 x 600
        
        // 화면 띄우기
        try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
            MatrixToImageWriter.writeToStream(qr, "PNG", out);
            return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(out.toByteArray());
        }
    }

 

 

 => view에서 버튼 클릭시 /createQr/{eqpmntSn} 이 실행되도록 했음

컨트롤러 실행시 알아서 새창이 열리고 QR코드가 나타남


 

QR Code 생성 및 View 페이지에 출력하기

 

Controller

	/**
     * 장비관리 리스트 조회
     */
	@GetMapping("/list")
	public String list(@ModelAttribute("searchVo") EqpmntVo searchVo
						, Model model) throws Exception {
		// 페이징
		int count = eqpmntService.countEqpmnt(searchVo);
		Integer pageCnt = count/10;
		if ((count % 10) > 0) {
			pageCnt += 1;
		}
		searchVo.setPageCnt(pageCnt);
		searchVo.setFirstIndex((searchVo.getPageIndex()-1)*10);
        
		// 목록 조회
		List<EqpmntVo> eqpmntList = eqpmntService.findEqpmntList(searchVo);
		
		// QR Code 생성
	    for(int i = 0; i < eqpmntList.size(); i++) {
		    // QR코드 인식시 이동할 주소
		    String codeUrl = new String(("http:/example/eqpmntQr/" + eqpmntList.get(i).getEqpmntSn()).getBytes("UTF-8"), "ISO-8859-1");
            
	        // QR코드 생성
            BitMatrix qr = new MultiFormatWriter().encode(codeUrl, BarcodeFormat.QR_CODE, 100, 100);  // 크기 100 x 100
            
            // QR코드 이미지 생성
            BufferedImage qrCodeImg = MatrixToImageWriter.toBufferedImage(qr);
            
            // QR코드 이미지를 바이트 배열로 변환, byteArrayOutputStream에 저장
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(qrCodeImg, "png", byteArrayOutputStream);
            byteArrayOutputStream.flush();
            byte[] qrCodeBytes = byteArrayOutputStream.toByteArray();
            byteArrayOutputStream.close();
            String qrr = Base64.encode(qrCodeBytes);
            eqpmntList.get(i).setQrCode(qrr);
            
		}
		
		// 장비상태에 따른 장비 개수 조회
		EqpmntVo eqpmntStateCount = eqpmntService.countEqpmntState(searchVo);
	
		model.addAttribute("count", count);
		model.addAttribute("searchVo", searchVo);
		model.addAttribute("list", eqpmntList);
		model.addAttribute("eqpmntStateCount", eqpmntStateCount);
		return "eqpmnt/list";
	}

 

 

view(jsp)

<img src="data:image/png;base64,<c:out value="${eqpmnt.qrCode}"/>" alt = "QR Code"><br>

 

 

결과

 

 

 


 

참고한 글

 

[Springboot/Java] QR 코드 생성/저장/출력 (tistory.com)

 

[Springboot/Java] QR 코드 생성/저장/출력

Java, Oracle, Spring boot 환경에서 백엔드로 구축 1. Mapper 설계 1 2 3 4 5 6 7 8 9 @Mapper public interface QrCodeMapper { /** * 아래의 매개변수를 DB에 QR코드 정보로 삽입하는 역할 * @param link * @param qrCode */ void insertQ

isshosng.tistory.com