java如何生成二维码并且给二维码添加logo
发表于:2025-02-08 作者:千家信息网编辑
千家信息网最后更新 2025年02月08日,这篇文章主要介绍java如何生成二维码并且给二维码添加logo,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!java生成二维码,具体代码如下所示:package com.bus
千家信息网最后更新 2025年02月08日java如何生成二维码并且给二维码添加logo
这篇文章主要介绍java如何生成二维码并且给二维码添加logo,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
java生成二维码,具体代码如下所示:
package com.bus.wx.action.code;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Graphics2D;import java.awt.RenderingHints;import java.awt.geom.RoundRectangle2D;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.Hashtable;import java.util.Map;import javax.imageio.ImageIO;import javax.servlet.ServletOutputStream;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;import com.bus.plugin.wx.action.WxAction;import com.bus.wx.util.Limits;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.client.j2se.MatrixToImageWriter;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.QRCodeWriter;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import com.lys.sys.log.Log;/** * 条形码、二维码 * @author hwj * */@Scope(value = "prototype")@Controller("Bar_Qr_Code_Action")@RequestMapping(value="plug/wx/wwz/{bcflag}/barqrcode")public class Bar_Qr_Code_Action extends WxAction{ private static final int LogoPart = 4; private static final int BLACK = 0xFF000000;//用于设置图案的颜色 private static final int WHITE = 0xFFFFFFFF; //用于背景色 String format = "png";public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, (matrix.get(x, y) ? BLACK : WHITE)); // image.setRGB(x, y, (matrix.get(x, y) ? Color.YELLOW.getRGB() : Color.CYAN.getRGB())); } } return image; } public BufferedImage LogoMatrix(BufferedImage matrixImage) throws IOException{ /** * 读取二维码图片,并构建绘图对象 */ Graphics2D g2 = matrixImage.createGraphics(); int matrixWidth = matrixImage.getWidth(); int matrixHeigh = matrixImage.getHeight(); /** * 读取Logo图片 */ String path=request.getSession().getServletContext().getRealPath("/images/logo/dzjkklog.png"); BufferedImage logo = ImageIO.read(new File(path)); //开始绘制图片 g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制 BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND); g2.setStroke(stroke);// 设置笔画对象 //指定弧度的圆角矩形 RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,20,20); g2.setColor(Color.white); g2.draw(round);// 绘制圆弧矩形 //设置logo 有一道灰色边框 BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND); g2.setStroke(stroke2);// 设置笔画对象 RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20); g2.setColor(new Color(128,128,128)); g2.draw(round2);// 绘制圆弧矩形 g2.dispose(); matrixImage.flush() ; return matrixImage ; } /** * 生成二维码:直接将生成的二维码传输到前台页面 * @param bcflag */ @RequestMapping(value="createQrCodes",method = RequestMethod.GET) public void createQrCodes(@PathVariable String bcflag,String dastid){ String url="扫二维码出现的内容"; if(url!=null&&!"".equals(url)){ ServletOutputStream stream=null; try { int width=430; int height=430; stream=response.getOutputStream(); QRCodeWriter writer=new QRCodeWriter(); Hashtablehints = new Hashtable (); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.MARGIN, 1);//设置二维码边的空度,非负数 BitMatrix bitMatrix = new MultiFormatWriter().encode(url,//要编码的内容 BarcodeFormat.QR_CODE, width, //条形码的宽度 height, //条形码的高度 hints); //BitMatrix m=writer.encode(url, BarcodeFormat.QR_CODE, height,width); BufferedImage image = toBufferedImage(bitMatrix); image = LogoMatrix(image); if (!ImageIO.write(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } } catch (Exception e) { Log.in.info(e.getMessage()); }finally{ if(stream!=null){ try { stream.flush(); stream.close(); } catch (IOException e) { Log.in.info(e.getMessage()); } } } } }}
以上是"java如何生成二维码并且给二维码添加logo"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
二维
二维码
生成
内容
图片
对象
条形
条形码
矩形
圆弧
笔画
篇文章
一道
代码
价值
兴趣
前台
图案
圆角
宽度
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
盛世芳华百鸟朝凤服务器
诸城网络软件开发公司
查询社保数据库重复
数据库如何选择候选码
实时数据服务器哪些比较好
贵州pdu服务器专用电源哪家好
山西通用软件开发进货价
服务器管理卡与网口直通
开设网络安全的一本院校
公司内网网络安全管理制度
pb12.5连接数据库
中行西安软件开发中心
国网通网络技术有限公司
国家网络安全局职能
ios 笔记本软件开发
社区开展网络安全活动方案
企业应用软件开发顾问
高带宽物理服务器
方舟服务器黄色
网络安全攻防应急演练总结
安徽网新网络技术
服务器只有一个ssl证书
华力创通网络安全军工数字货币
java程序用的数据库
服务器设置不可用怎么办
服务器20防护
数据库管理系统属于数据库
o2o电商软件开发服务
通州区综合网络技术服务包括什么
浦江晨曦网络技术有限公司