二维码生成

项目中用到的,以免以后会再次遇到。

一。需要两个jar包

  zxing-javase.jar

  zxing.jar

二。配置需要生成二维码的参数

三。生成二维码方法,方法返回值可改变

//生成二维码
	public String generateQRCode(String orderId,String qrCode){
		try {
		//物理路径
		String path = ApplicationContext.getProperty("RechargeCode");
		File file = new File(path);
		String filePath = file.getName() + File.separator + orderId + ".png";
		path += orderId + ".png";
		int width = 900;   
		        int height = 900;   
		        String format = "png";
		        Hashtable hints= new Hashtable();   
		        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");   
		        BitMatrix bitMatrix = new MultiFormatWriter().encode(qrCode, BarcodeFormat.QR_CODE, width, height,hints);   
		        File outputFile = new File(path);   
		        MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
		        return filePath;
		} catch (Exception e) {
		e.printStackTrace();
		}
		return null;
		}

  

posted @ 2017-06-24 17:34  向着10K踏步走  阅读(180)  评论(1编辑  收藏  举报