java二维码中间添加用户logo头像(自动计算并且居中)

所用的都是java原生的代码,工具类是通过网络加载图片进行处理,可以手动改为多图片拼接

 

 //拼接一个带头像的二维码图片,默认比例20%
    public static File createQRColorImg(File imgFile, String url) {
        if (null == imgFile || StringUtils.isEmpty(url)) {
            return imgFile;
        }

        //下载图片
        File urlFile = FileUtil.getNetUrl(url);
        if (null == urlFile) {
            return imgFile;
        }

        //开始拼接
        //背景
        ImgBean bckBean = new ImgBean();
        bckBean.setFile(imgFile);
        BufferedImage bckBufferedImage = bckBean.getBufferedImage();
        //得到宽高
        int maxWidth = bckBufferedImage.getWidth();
        int maxHeight = bckBufferedImage.getHeight();

        //宽高小于50的就不做头像处理了,识别率低
        if (maxHeight < 50 || maxWidth < 50) {
            return imgFile;
        }

        //默认比例20%
        Double dft = 0.20d;
        //距左边
        BigDecimal leftb = new BigDecimal(maxWidth).multiply(new BigDecimal(1d - dft)).divide(new BigDecimal(2));
        //距上边
        BigDecimal topb = new BigDecimal(maxHeight).multiply(new BigDecimal(1d - dft)).divide(new BigDecimal(2));
        //中间图片宽高
        BigDecimal cb = new BigDecimal(maxWidth).multiply(new BigDecimal(dft));
        //中间图片
        ImgBean brBean = new ImgBean(urlFile, leftb.intValue(), topb.intValue(), cb.intValue(), cb.intValue());

        //图片列表
        List<ImgBean> imgBeanList = new ArrayList<>();
        imgBeanList.add(bckBean);
        imgBeanList.add(brBean);

        //图片
        File file = ImgJoinUtil.createColorImg(maxWidth, maxHeight, Color.WHITE, imgBeanList, null);

        urlFile.delete();
        if (null != file) {
            imgFile.delete();
            return file;
        } else {
            return imgFile;
        }

    }

其中远程图片下载见另一篇文章FileUtil.getNetUrl(url)

https://blog.youkuaiyun.com/qq_21235239/article/details/99415421

其中图文拼接工具包见另一篇文章ImgJoinUtil

里面包含了ImgBean

https://blog.youkuaiyun.com/qq_21235239/article/details/99416808

生成效果

二维码是通过file生成的,头像是远程地址,这里就有一个二维码的知识介绍了,二维码是可以自动修复的,可以最大修复25%,当然链接不能过短,过短的话二维码生成出来太简单,遮挡的范围太大,也无法识别二维码,所以头像部分建议为20%左右就行了

有疑问留言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

成都java小生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值