生成二維碼的方式很多種,這里我只是貼出我根據百度,自己整理的 用java qr code 方式生成二維碼
一、普通二維碼
public QrCodeUtils{ /** * 生成二維碼圖片 * * @param content */ public static void createQrCode(String content) { try { Qrcode qrcode = new Qrcode(); // 設置二維碼排錯率, L(7%)、M(15%)、Q(25%)、H(30%) 排錯率越高可存儲的信息越少,但對二維碼的清晰度要求比較低 qrcode.setQrcodeErrorCorrect('M'); // N代表數字,A代表字符a-Z,B代表其他字符 qrcode.setQrcodeEncodeMode('B'); // 設置二維碼版本,取值范圍為0-40,值越大尺寸越大,存儲的信息越大 qrcode.setQrcodeVersion(7); // 設置圖片高寬度 BufferedImage bufferedImage = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB); // 創建一個 Graphics2D,畫圖 Graphics2D gs = bufferedImage.createGraphics(); // 設置圖片的背景色 gs.setBackground(Color.WHITE); gs.clearRect(0, 0, 139, 139); // 設置圖片顏色 gs.setColor(Color.black); // 將輸出內容轉為byte類型 byte[] contentBytes = content.getBytes("utf-8"); // 設置偏移量,不設置可能導致解析出錯 int pixoff = 2; // 輸出內容 if (contentBytes.length > 0 && contentBytes.length < 130) { boolean[][] codeOut = qrcode.calQrcode(contentBytes); for (int i = 0; i < codeOut.length; i++) { for (int j = 0; j < codeOut.length; j++) { if (codeOut[j][i]) { gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3); } } } } else { System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,130 ]. "); return; } // 沒有logo的二維碼 gs.dispose(); bufferedImage.flush(); // ImageIO.write(bufImg, "jpg", response.getOutputStream()); 生成圖片輸出流中 // 生成二維碼QRCode圖片 File imgFile = new File("D:/qrCode/"); // 生成的圖片在D盤下,名為 qrCode.png ImageIO.write(bufferedImage, "png", imgFile); } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) {
QrCodeUtils.createQrCode("123456789_jo");
}
}
二 、生成一個帶logo的二維碼
public QrCodeLogoUtil{ public static void createLogoQrcode(String content, String logoPath) { try { Qrcode logoQrCode = new Qrcode(); logoQrCode.setQrcodeErrorCorrect('M'); logoQrCode.setQrcodeEncodeMode('B'); logoQrCode.setQrcodeVersion(7); BufferedImage bufferedImage = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB); Graphics2D gs = bufferedImage.createGraphics(); gs.setBackground(Color.WHITE); gs.setColor(Color.BLUE); gs.clearRect(0, 0, 139, 139); byte[] contents = content.getBytes("utf-8"); int opx = 2; if (contents.length > 0 && contents.length < 130) { boolean[][] codeOut = logoQrCode.calQrcode(contents); for (int i = 0; i < codeOut.length; i++) { for (int j = 0; j < codeOut.length; j++) { if (codeOut[j][i]) { gs.fillRect(j * 3 + opx, i * 3 + opx, 3, 3); } } } } else { System.err.println("QRCode content bytes length = " + contents.length + " not in [ 0,130 ]. "); return; } // logo圖片高度 Image logo = ImageIO.read(new File(logoPath)); int widthLogo = logo.getWidth(null) > bufferedImage.getWidth() * 2 / 10 ? (bufferedImage.getWidth() * 2 / 10) : logo.getWidth(null); int heightLogo = logo.getHeight(null) > bufferedImage.getHeight() * 2 / 10 ? (bufferedImage.getHeight() * 2 / 10) : logo.getWidth(null); // 設置logo圖片的位置 int x = (bufferedImage.getWidth() - widthLogo) / 2; int y = (bufferedImage.getHeight() - heightLogo) / 2; gs.drawImage(logo,x,y,widthLogo,heightLogo,null); gs.dispose(); bufferedImage.flush(); // 生成二維碼QRCode圖片 File imgFile = new File("D:/logoQrCode/"); // 生成的圖片在D盤下,名為 qrCode.png ImageIO.write(bufferedImage, "png", imgFile); } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) {
QrCodeUtils.createLogoQrcode("liTing_232323","D:/picture/logo_qrcode.jpg");
}
}
以上,二維碼生成的位置,是自己配置的 。
帶logo二維碼生成代碼中,logo的圖片是我本地的圖片。
以上代碼生成的二維碼,可以用 微信的掃一掃功能, 可以得到你設置的content值。