java生成二维码并且给二维码添加logo
java生成二维码,具体代码如下所示:
packagecom.bus.wx.action.code; importjava.awt.BasicStroke; importjava.awt.Color; importjava.awt.Graphics2D; importjava.awt.RenderingHints; importjava.awt.geom.RoundRectangle2D; importjava.awt.image.BufferedImage; importjava.io.ByteArrayInputStream; importjava.io.ByteArrayOutputStream; importjava.io.File; importjava.io.IOException; importjava.io.InputStream; importjava.util.HashMap; importjava.util.Hashtable; importjava.util.Map; importjavax.imageio.ImageIO; importjavax.servlet.ServletOutputStream; importorg.springframework.context.annotation.Scope; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.PathVariable; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importsun.misc.BASE64Decoder; importsun.misc.BASE64Encoder; importcom.bus.plugin.wx.action.WxAction; importcom.bus.wx.util.Limits; importcom.google.zxing.BarcodeFormat; importcom.google.zxing.EncodeHintType; importcom.google.zxing.MultiFormatWriter; importcom.google.zxing.client.j2se.MatrixToImageWriter; importcom.google.zxing.common.BitMatrix; importcom.google.zxing.qrcode.QRCodeWriter; importcom.google.zxing.qrcode.decoder.ErrorCorrectionLevel; importcom.lys.sys.log.Log; /** *条形码、二维码 *@authorhwj * */ @Scope(value="prototype") @Controller("Bar_Qr_Code_Action") @RequestMapping(value="plug/wx/wwz/{bcflag}/barqrcode") publicclassBar_Qr_Code_ActionextendsWxAction{ privatestaticfinalintLogoPart=4; privatestaticfinalintBLACK=0xFF000000;//用于设置图案的颜色 privatestaticfinalintWHITE=0xFFFFFFFF;//用于背景色 Stringformat="png"; publicstaticBufferedImagetoBufferedImage(BitMatrixmatrix){ intwidth=matrix.getWidth(); intheight=matrix.getHeight(); BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB); for(intx=0;xhints=newHashtable (); hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); hints.put(EncodeHintType.MARGIN,1);//设置二维码边的空度,非负数 BitMatrixbitMatrix=newMultiFormatWriter().encode(url,//要编码的内容 BarcodeFormat.QR_CODE, width,//条形码的宽度 height,//条形码的高度 hints); //BitMatrixm=writer.encode(url,BarcodeFormat.QR_CODE,height,width); BufferedImageimage=toBufferedImage(bitMatrix); image=LogoMatrix(image); if(!ImageIO.write(image,format,stream)){ thrownewIOException("Couldnotwriteanimageofformat"+format); } }catch(Exceptione){ Log.in.info(e.getMessage()); }finally{ if(stream!=null){ try{ stream.flush(); stream.close(); }catch(IOExceptione){ Log.in.info(e.getMessage()); } } } } } }
总结
以上所述是小编给大家介绍的java生成二维码并且给二维码添加logo,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。