1、下载第三方jar包百度一下:zxing ,参考下面截图下面是zxing源码的地址,需要自己编译一下。当然,你也可以通过m锾攒揉敫aven直接下载编译好的jar包,用到的jar包含(core.jar,javase.jar)
2、直接上代码:Map<EncodeHi艘绒庳焰ntType, Object> hints = new HashMap<呶蓟鹭毵EncodeHintType, Object>(); hints.put(EncodeHintType.MARGIN, 0); BitMatrix bitMatrix = new QRCodeWriter().encode("生成二维码的内容", BarcodeFormat.QR_CODE, 256, 256,hints); int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, bitMatrix.get(x, y) == true ? Color.BLACK.getRGB():Color.WHITE.getRGB()); } } ImageIO.write(image,"png", new File("生成二维码保存的路径"));
3、用你的手机扫描二维码工具,扫一扫看看是不是很ok呢?去二维码的白边,等后续分享