字符串转二维码 - Google Zxing

使用Google Zxing类库生成二维码,以字符串转二维码为例。

private static int QR_WIDTH = 102;
private static int QR_HEIGHT = 102;

public static void qRCode(String content) throws WriterException {
		String format = "png";
		String path = "d:/authy.png";
		HashMap hints = new HashMap();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 字符集
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 纠错级别
		hints.put(EncodeHintType.MARGIN, 2);// 空白
		try {
			BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT,
					hints);
			Path file = new File(path).toPath();
			MatrixToImageWriter.writeToPath(bitMatrix, format, file);
		} catch (Exception e) {
			e.printStackTrace();
		}
}

说明:图片输出方式有多种选项,具体参考MatrixToImageWriter.writeToPath形参列表。