【java】将url生成二维码工具

pom.xml 引入依赖

		
			com.google.zxing
			core
			3.3.3
		
		
			com.google.zxing
			javase
			3.3.3
		
// 使用方式
@GetMapping("/get_code_img")
    public void get_code_img(HttpServletResponse response) {
        String url = "http://www.jishupeng.cn";
        try {
            // 生成二维码配置
            Map hint = new HashMap<>();
            // 设置纠错等级
            hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            // 设置字符集
            hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
            // 设置二维码图像
            BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 300, 300, hint);

            // 使用输出流写给客户端
            ServletOutputStream outputStream = response.getOutputStream();
            MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(java,java)