以下两种方法:
url地址以http://开头。 否则浏览器不能直接跳
宽和高都是一个几wh
边框边距0为无边框,1为最小边框
返回缓冲图像类型的数据,并调用第二个方法respImageBuffer下载到浏览器中
/** * @param url链接内容* @param wh宽度高度* @param margin边框大小* @ return */publicbufferedimageqrcode (字符串URL,Integer wh ; intheight=objects.isnull(wh )? 300:wh;//定义二维码的参数HashMapEncodeHintType,object hints=newhashmapencodehinttype,object (; //定义字符集编码格式的hints.put (encodehinttype.character _ set,' utf-8 ' ); //纠错水平L M Q H纠错能力越高,能存储的越少,一般为m hints.put (encodehinttype.error _ correction,ErrorCorrectionLevel.M //图像边距hints.put (encodehinttype.margin,margin ); bufferedimagebufferedimage=null; try { //最终生成参数列表(1.内容2 .格式3 .宽4 .高5 .二维码参数) bitmatrixbitmatrix=newmultiformatwriter ).encode ) URL,barrer }catch(exceptione ) { new CustomException ) (“二维码存储失败”,400 ); }返回缓冲图像; }缓冲图像类型的数据下载到浏览器中
publicvoidrespimagebuffer, httpservletresponseresponse (try ) bytearrayoutputstreambytearrayoutputstreaar imageoutputstreamimageoutput=imageio.createateate imageio.write(img,' png ',imageOutput ); bytearrayinputstreaminputstream=newbytearrayinputstream (bytearray output stream.to bytearray (); long length=imageOutput.length (; //response response.set content type (应用程序/x-ms下载); response.setcontentlength((int ) length; response.setheader (' content-disposition ',' attachment; filename=ercode.jpg '; //输出流byte[] b=new byte[1024]; servletoutputstreamoutputstream=response.get output stream (长计数=0; while (计数长度) intlen=Inputstream.read(b,0,1024 ); count=len; outputstream.write(b,0,len ); } outputStream.flush (; }catch(exceptione ) { throw new CustomException )“二维码生成失败”,400 ); }