通过传入字符串,生成二维码图片或者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 }