用JAVA生成二维码

使用SwetakeQRCode在Java项目中生成二维码
首先将SwetakeQRCode拷贝在项目lib中,加入Build Path,然后:

		byte[] buff = "http://www.mrjeye.org".getBytes("utf-8");
		Qrcode qrcode = new Qrcode();
		qrcode.setQrcodeVersion(3);
		qrcode.setQrcodeErrorCorrect('M');
		qrcode.setQrcodeEncodeMode('B');
		
		boolean[][] bRect = qrcode.calQrcode(buff);

		if(bRect.length == 0 ) {
			return;
		}
		BufferedImage bufferedImage = new BufferedImage(DEFAULT_WIDTH, DEFAULT_WIDTH, BufferedImage.TYPE_INT_RGB);

		int unitWidth = DEFAULT_WIDTH / bRect.length;

		Graphics graphics = bufferedImage.getGraphics();

		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, DEFAULT_WIDTH, DEFAULT_WIDTH);
		graphics.setColor(Color.BLACK);
		
		// 逐一加载boolean数组,画出二维码图片
		for (int i = 0; i < bRect.length; i++) {
			StringBuilder builder = new StringBuilder();
			for (int j = 0; j < bRect.length; j++) {
				builder.append(bRect[j][i] ? "X" : " ").append(" ");
				if (bRect[j][i]) {
					graphics.fillRect(j*unitWidth, i*unitWidth, unitWidth-1, unitWidth-1);
				}
			}
			System.out.println(builder.toString());
		}
		// 最后保存在文件系统中
		ImageIO.write(bufferedImage, "jpeg", new File("d:/1.jpg"));

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