zxing QR 코드 로고 아이콘 과 아래쪽 문자 정보 생 성

31909 단어 JAVA자바
ssm 프로젝트 에서 할 수 있 는 것 은 매개 변수 값 에 따라 로고 아이콘 이나 문자 가 있 거나 문자 가 없 거나 로고 가 없 는 등 몇 가지 스타일 을 생 성 할 수 있 습 니 다.
Step 1: maven 프로젝트 의 pom. xml 도입 의존:
		<!--    -->
		<dependency>
			<groupId>com.google.zxing</groupId>
			<artifactId>core</artifactId>
			<version>3.3.1</version>
		</dependency>

단계 2: QRcodeUtil 도구 클래스
public class QRCodeUtil {

    private static final int QRCOLOR = 0xFF000000; //      
    private static final int BGWHITE = 0xFFFFFFFF; //     
    private static final int WIDTH = 400; //     
    private static final int HEIGHT = 400; //     
    
    
    /**    QR     
     * com.google.zxing.EncodeHintType:      ,    
     * EncodeHintType.CHARACTER_SET:        
     * EncodeHintType.ERROR_CORRECTION:      
     *      ErrorCorrectionLevel:      ,L = ~7% correction、M = ~15% correction、Q = ~25% correction、H = ~30% correction
     *          ,    L   ,     ,       ,          
     * EncodeHintType.MARGIN:       ,    ,   ,         
     * */
    private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
    	
        private static final long serialVersionUID = 1L;
        {
            put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//   QR        (H     )      
            put(EncodeHintType.CHARACTER_SET, "utf-8");//       
            put(EncodeHintType.MARGIN, 0);
        }
        
    };
    
    /**
     *      
     * @param logoStr		      logo     ,  null,   logo  
     * @param outputStream	HttpServletResponse   
     * @param qrUrl			     :                    
     * @param note			            ,  null,          
     */
    public static void drawLogoQRCode(String logoStr, HttpServletResponse response, String qrUrl, String note) { 
    	
    	File logoFile=null;
    	
        try {
        	OutputStream outputStream=response.getOutputStream();
        	
        	if(logoStr!=null) {
        		logoFile=new File(logoStr);
        	}
        	
        	
            /**
             * MultiFormatWriter:     ,       ,        encode   ,           
             *      encode(String contents,BarcodeFormat format,int width, int height,Map hints)
             *      contents:   /     
             *      format:    ,     ,     
             *      width:    
             *      height:    
             *      hints:        
             * BarcodeFormat:              ,      ,  1      ,2        
             * BitMatrix: (  )    2D  ,         
             */
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            
            
            /**       :    ,    ,      ,      ,    
             * java.awt.image.BufferedImage:              ,    RenderedImage   
             * BitMatrix   get(int x, int y)         ,      ,   true,        ,        
             * BufferedImage   setRGB(int x, int y, int rgb)         
             *      x:        ,  
             *      y:        ,  
             *      rgb:    ,   16   ,  0xFFFFFF   
             */
            BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
            BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
            //            Bitmap  ,     (0xFFFFFFFF) (0xFF000000)  
            for (int x = 0; x < WIDTH; x++) {
                for (int y = 0; y < HEIGHT; y++) {
                    image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
                }
            }
            
            int width = image.getWidth();
            int height = image.getHeight();
            if (Objects.nonNull(logoFile) && logoFile.exists()) {
                //       
                Graphics2D g = image.createGraphics();
                //   Logo  
                BufferedImage logo = ImageIO.read(logoFile);
                //     logo  
                g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
                g.dispose();
                logo.flush();
            }

            //        
            if (StringUtils.isNotEmpty(note)) {
                //     ,  logo          
                BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
                Graphics2D outg = outImage.createGraphics();
                //          
                outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
                //         
                outg.setColor(Color.BLACK);
                outg.setFont(new Font("  ", Font.BOLD, 30)); //   、  、  
                int strWidth = outg.getFontMetrics().stringWidth(note);
                if (strWidth > 399) {
                  //           
                    //        
                    String note1 = note.substring(0, note.length() / 2);
                    String note2 = note.substring(note.length() / 2, note.length());
                    int strWidth1 = outg.getFontMetrics().stringWidth(note1);
                    int strWidth2 = outg.getFontMetrics().stringWidth(note2);
                    outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
                    BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
                    Graphics2D outg2 = outImage2.createGraphics();
                    outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
                    outg2.setColor(Color.BLACK);
                    outg2.setFont(new Font("  ", Font.BOLD, 30)); //   、  、  

                    outg2.drawString(note2, 200 - strWidth2 / 2, outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
                    outg2.dispose();
                    outImage2.flush();
                    outImage = outImage2;
                } else {
                    outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); //    
                }
                outg.dispose();
                outImage.flush();
                image = outImage;
            }

            image.flush();
            
            /**
             *        ,       ,         File,       
             */
            ImageIO.write(image, "png", outputStream);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Step 3: 사용 방법: (ps: 출력 은 그림 흐름)
	@RequestMapping("/getQRCode")
	public void getQRCode(HttpServletResponse response,String qrUrl) throws Exception {
		String imagePath="F:\\AndroidJavaCode\\one\\hotelService\\src\\main\\webapp\\picture\\login_unknow.png";
	
        //  1:      logo     ,  null,   logo  ,  2:HttpServletResponse   
        //  3:     :                    ,  4:            ,  null,          
		QRCodeUtil.drawLogoQRCode(imagePath, response, qrUrl, "      ");

	}

좋은 웹페이지 즐겨찾기