Zxing 生成二维码嵌套logo,并输出到浏览器

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、效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值