关于使用谷歌矩阵代码生成微信二维码的方法

注意:请先检查是否在maven中添加了fastjson以及谷歌zxing的依赖包,本示例使用的依赖包及对应版本如下:

<dependencies>
		<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>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.1.29</version>
		</dependency>
</dependencies>

测试代码如下:

public class QRCodeTest {

	@Test
	public void test() throws WriterException, IOException {
		//生成二维码的路径
		String filePath = "D://";
		//图片名称
		String fileName = "qrCodeTest.png";
		
		//使用fastjson创建jsonObject对象
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("company", "www.kinglong.com");
		jsonObject.put("author", "kinglong");
		
		//将json对象转换为json格式的字符串
		String content = jsonObject.toJSONString();
		
		//设置画布大小
		int width = 200;//宽度
		int height = 200;//高度
		
		//设置字符编码集,采用utf-8的字符编码
		Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType,Object>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		
		//生成矩阵
		BitMatrix bitMatrix = new MultiFormatWriter().encode("weixin://wxpay/bizpayurl?pr=KKmqnXS", BarcodeFormat.QR_CODE, width, height, hints);
		
		//生成路径
		Path path = FileSystems.getDefault().getPath(filePath, fileName);
		
		//输出文件至指定的路径
		MatrixToImageWriter.writeToPath(bitMatrix, "png", path);
		
		
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值