生成二维码

今天做了个推广二维码的功能,主要依赖谷歌开源的core架包实现,推广可以使用链接或者二维码(实质上二维码就是携带了链接信息而已)。二维码相比链接好处在于方便不需要记住或者去复制粘贴链接,毕竟扫码已经很普遍了。不好的地方有些扫码软件不给你自动跳转仅展示扫码出来的结果(比如微信扫码不是所有链接都跳,可能是微信的风控/白名单导致不跳)

推广链接比如: http:// www.yulisao.com/down.html?id=12345678 输入到浏览器就可以下载, 链接后面的参数如果比较重要的话最好做下加密。

二维码扫码下载:生成结果如下图。
在这里插入图片描述

二维码携带的信息可以是链接也可以是纯文案,携带的内容越多二维码就越密集,所以尽量不要太多,不然生成的二维码太密集导致黑乎乎一团可能识别不了。网上也有在线生成二维码的网站,可以直接用,比如草料二维码。如若要自动生成或者批量还是上代码吧。

测试主代码

package per.main;

import java.io.File;

/**
 * 生成二维码测试主程序
 * 
 * @author yulisao
 * @createDate 2020年5月21日
 */
public class MainTest {
	
	public static void main(String[] args) {
		String logoFilePath = "C://Users//Administrator//Desktop//qrtest//logo.jpg";	
		String QrCodeFilePath = "C://Users//Administrator//Desktop//qrtest//qrcode.jpg";
		File logoFile = new File(logoFilePath);
		File QrCodeFile = new File(QrCodeFilePath);
		
		String url = "http:// www.yulisao.com/down.html?id=12345678";
		String text = "测试生成二维码";
		int bgType = 1;  // 1 : 透明背景 , 2 : 白色背景	
		
	    QrCodeUtil.makeQRCode(logoFile, QrCodeFile, url, text, bgType);
	    
	}

}

下面是一个生成二维码的工具类,里面的有些参数还可以自己改改,制作出个性化的二维码。

package per.utils;

import java.awt.Color;

public class QrCodeUtil {
    private static final int QRBLACK = 0xFF000000; // 黑色二维码
    private static final int QRWHITE = 0x00FFFFFF; // 透明二维码
    private static final int BGWHITE = 0x00FFFFFF; // 透明背景
    private static final int BGLUCENCY = 0xFFFFFFFF; // 白色背景
   
    private static final int QRSIZE = 1000; // 二维码宽    
    private static final String IMGFMT = "png"; // 二维码图片格式    
    private static Map<EncodeHintType, Object> hints = new EncodeHintTypeExt(); // 用于设置QR二维码参数

    
    /**
     * 
     * @param logoFile 二维码中间的logo图片(可为空)
     * @param codeFile 生成后输出路径
     * @param qrUrl 二维码扫码结果(跳转网址或展示的文案)
     * @param text 备注(可为空)
     * @param bgType 二维码类型 1 : 透明背景 , 2 : 白色背景
     */
    public static void makeQRCode(File logoFile, File codeFile, String qrUrl, String text, int bgType) {
        try {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            // 二维码内容,编码类型,生成图片宽度,生成图片高度,设置参数
            BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, QRSIZE, QRSIZE, hints);
            BufferedImage image = null;
            if (bgType == 1) {
                image = new BufferedImage(QRSIZE, QRSIZE, BufferedImage.TYPE_INT_ARGB);
                // 创建比特矩阵
                for (int x = 0; x < QRSIZE; x++) {
                    for (int y = 0; y < QRSIZE; y++) {
                        image.setRGB(x, y, bm.get(x, y) ? QRBLACK : BGWHITE);
                    }
                }
            } else if (bgType == 2) {
                image = new BufferedImage(QRSIZE, QRSIZE, BufferedImage.TYPE_INT_ARGB);
                // 创建比特矩阵
                for (int x = 0; x < QRSIZE; x++) {
                    for (int y = 0; y < QRSIZE; y++) {
                        image.setRGB(x, y, bm.get(x, y) ? QRWHITE : BGLUCENCY);
                    }
                }
            } else {
				// 自定义别的背景颜色和二维码颜色
			}

            // 添加logo图片
            int width = image.getWidth();
            int height = image.getHeight();
            if (logoFile.exists()) {
                Graphics2D g = image.createGraphics();// 构建绘图对象
                BufferedImage logo = ImageIO.read(logoFile);
                g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);// 开始绘制logo图片
                g.dispose();
                logo.flush();
            }

            // 自定义文本描述
            if (StringUtils.isEmpty(text)) {
                // 创建新图片,再把二维码图片和文字图片放这个新建的图片上
                BufferedImage outImage = new BufferedImage(1000, 1113, BufferedImage.TYPE_4BYTE_ABGR);
                Graphics2D outg = outImage.createGraphics();                
                outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);// 把二维码放到新的图片
                outg.setColor(Color.BLACK);// 把文字放到新的图片上
                outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号
                int strWidth = outg.getFontMetrics().stringWidth(text);
                if (strWidth > QRSIZE) {// 长度过长就换成两行
                    String text1 = text.substring(0, text.length() / 2);
                    String text2 = text.substring(text.length() / 2, text.length());
                    int strWidth1 = outg.getFontMetrics().stringWidth(text1);
                    int strWidth2 = outg.getFontMetrics().stringWidth(text2);
                    outg.drawString(text1, 500 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
                    BufferedImage outImage2 = new BufferedImage(1000, 1212, BufferedImage.TYPE_4BYTE_ABGR);
                    Graphics2D outg2 = outImage2.createGraphics();
                    outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
                    outg2.setColor(Color.BLACK);
                    outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号
                    outg2.drawString(text2, 500 - strWidth2 / 2,
                            outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
                    outg2.dispose();
                    outImage2.flush();
                    outImage = outImage2;
                } else {
                    outg.drawString(text, 500 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 放文字
                }
                outg.dispose();
                outImage.flush();
                image = outImage;
            }

            image.flush();

            ImageIO.write(image, IMGFMT, codeFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
package per.utils;

import java.util.HashMap;

public class EncodeHintTypeExt extends HashMap<EncodeHintType, Object> {
	private static final long serialVersionUID = 1L;
	{
		put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
		put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码方式
		put(EncodeHintType.MARGIN, 0);
	}
}

谷歌公开的包里面也有识别解析二维码的方法,有需要的时候再去研究一下。QRCodeReader

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值