哎呀,看了那么多博客,搬运了那么多代码,第一次自己写博客,我发现即使会使用某技术了,但是要表达、转述出来还是不简单的。希望我能坚持下去哈。
为什么使用Zxing?Google 名气大啊,其他我也不了解啊。
上手还是很容易的,引入jar 包后十几代码的事。
1、在pom.xml 引入依赖
<!-- 条形码、二维码生成 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.0.0</version> </dependency>
2、后台controler层 绘制输出二维码代码@RequestMapping("createQRCode") public String createQRCode(HttpServletRequest request, HttpServletResponse response){
//如果当前用户有渠道,则生成二维码 String id = UserUtils.getUser().getId(); LoanChannel channelByUserId = loanChannelService.getChannelByUserId(id); if (channelByUserId!=null){ JSONObject json = new JSONObject(); json.put( "channelName", channelByUserId.getName()); json.put("channelNo", channelByUserId.getChannelNo()); String content = json.toJSONString();// 内容 int width = 210; // 图像宽度 int height = 200; // 图像高度 String format = "png";// 图像类型 Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置纠错级别 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); BitMatrix bitMatrix = null;// 生成矩阵 try { bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); /** 定义一张空白缓冲流图片 */ BufferedImage image = new BufferedImage(bitMatrix.getWidth(), bitMatrix.getHeight(), BufferedImage.TYPE_INT_RGB); /** 循环二维码字节转化对象中所有点 矩阵转image*/ for (int x = 0; x < bitMatrix.getWidth(); x++){ for (int y = 0; y < bitMatrix.getHeight(); y++){ /** 获取其中一点,判断它是黑色还是白色 true:黑色 false:白色 */ int rgb = bitMatrix.get(x, y) ? 0x000000 : 0xffffff; /** 在空白图片绘制一点 */ image.setRGB(x, y, rgb); } }
//加载logo InputStream stream = this.getClass().getResourceAsStream("/images/logo.png"); BufferedImage logo = ImageIO.read(stream);
//嵌套logo BufferedImage logoMatrix = LogoConfig.LogoMatrix(image, logo); ImageIO.write(logoMatrix,format,response.getOutputStream()); } catch (WriterException e) { logger.info("输出二维码错误",e); e.printStackTrace(); } catch (IOException e) { logger.info("输出二维码错误",e); e.printStackTrace(); } String data=channelByUserId.getName()+"-"+channelByUserId.getChannelNo(); return data; } return ""; }
3、二维码嵌套logo处理类
public class LogoConfig{ /** * 设置 logo * @param matrixImage 源二维码图片 * @return 返回带有logo的二维码图片 * @throws IOException * @author Administrator sangwenhao */ public static BufferedImage LogoMatrix(BufferedImage matrixImage, BufferedImage logo) throws IOException{ /** * 读取二维码图片,并构建绘图对象 */ Graphics2D g2 = matrixImage.createGraphics(); int matrixWidth = matrixImage.getWidth(); int matrixHeigh = matrixImage.getHeight(); //开始绘制图片 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.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); matrixImage.flush() ; g2.dispose(); return matrixImage ; } }
4、前端使用 a标签href 使用图片地址可以直接完成图片的点击下载,download 属性用于指定保存的文件名
img src 由后台输出
<a id="fileName" href="${ctx}/loan/loanChannel/createQRCode" download="渠道.png"> <img src="${ctx}/loan/loanChannel/createQRCode"> </a>
5、效果