Java生成二维码

来源:互联网 发布:淘宝手机优惠券用 编辑:程序博客网 时间:2024/06/08 06:50

这个例子是在网上找的例子,然后稍稍的修改了下,自己亲测了一下用微信可以扫描。web.xml的配置文件就不发了

public void encodeQRCoder(String content,HttpServletResponse response){Qrcode handler = new Qrcode();try{handler.setQrcodeErrorCorrect('M');handler.setQrcodeEncodeMode('B');handler.setQrcodeVersion(7); byte[] bytes = content.getBytes("UTF-8");BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);Graphics2D gs = bufImg.createGraphics();gs.setBackground(Color.white);gs.clearRect(0, 0, 140, 140);gs.setColor(Color.black);//设置偏移量  不设置可能导致解析出错  int pixoff = 2;if(bytes.length>0 && bytes.length<124){boolean[][] codeOut = handler.calQrcode(bytes);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);}}}gs.dispose();bufImg.flush();//生成二维码QRCode图片ImageIO.write(bufImg, "jpg", response.getOutputStream());}}catch(Exception e){e.printStackTrace();}}



0 0
原创粉丝点击