<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.5.2</version> </dependency> public enum ErrorCorrectionLevel { L(1), M(0), Q(3), H(2); }可以发现纠错等级越高,生成二维码的码点越密集。
<dependency> <groupId>com.github.aytchell</groupId> <artifactId>qrgen</artifactId> <version>3.0.0</version> </dependency>创建二维码
private BufferedImage createRoundedQRCodeImage(String url, int width, int level) throws QrConfigurationException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, WriterException { QrCodeRenderer qrCodeRenderer = new QrCodeRenderer(PixelStyle.ROWS, MarkerStyle.ROUND_CORNERS); ColorConfig colorConfig = new ColorConfig(new RgbValue(0, 0, 0), new RgbValue(255, 255, 255)); Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.ERROR_CORRECTION, getErrorCorrectionLevel(level)); return qrCodeRenderer.encodeAndRender(url, colorConfig, width, width, hints); }
效果
ZXing 下载地址:http://code.google.com/p/zxing/
ZXing 类库 注意点:
源代码中有两处UTF-8的问题,会导致乱码,
其一:com.google.zxing.qrcode.encoder.encoder类中的
internal const System.String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";
此处,将ISO-8859-1改为UTF-8
其二:com.google.zxing.qrcode.decoder.DecodedBitStreamParser类的成员
private const System.String UTF8 = "UTF8";
应将UTF8改为UTF-8