1. 程式人生 > >java 使用qrcode生成二維碼圖片或者base64字符串

java 使用qrcode生成二維碼圖片或者base64字符串

使用 ear 數字 graphics lean IT eve encoder red

通過傳入字符串,生成二維碼圖片或者base64格式字符串

 1 public static String barcode2Base64(String msg) throws Exception{
 2         Qrcode x = new Qrcode();
 3         //N代表數字,A代表a-z,B代表其他字符
 4         x.setQrcodeEncodeMode(‘B‘);
 5         //設置糾錯等級
 6         x.setQrcodeErrorCorrect(‘M‘);
 7         //設置版本號(1-40)
 8         x.setQrcodeVersion(7);
9 10 int width = 67+12*(7-1); 11 int height = 67+12*(7-1); 12 int pixoff = 2;//偏移量 13 14 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 15 Graphics2D gs = bufferedImage.createGraphics(); 16 gs.setBackground(Color.WHITE);
17 gs.setColor(Color.BLACK); 18 gs.clearRect(0, 0, width, height); 19 20 byte[] d = msg.getBytes("UTF-8"); 21 if(d.length>0&&d.length<120){ 22 boolean[][] s = x.calQrcode(d); 23 for(int i=0;i<s.length;i++){ 24 for
(int j=0;j<s.length;j++){ 25 if(s[j][i]){ 26 gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3); 27 } 28 } 29 } 30 } 31 // 加密 32 gs.dispose(); 33 bufferedImage.flush(); 34 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 35 ImageIO.write(bufferedImage, "bmp", bos);//bos可以是文件輸出流,這裏寫到字節流 36 // 加密 37 BASE64Encoder encoder = new BASE64Encoder(); 38 return encoder.encode(bos.toByteArray()); 39 }

java 使用qrcode生成二維碼圖片或者base64字符串