java 二维码生成(vcard)
1. maven 依赖
<dependency>
<groupId>com.googlecode.ez-vcard</groupId>
<artifactId>ez-vcard</artifactId>
<version>0.10.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
2. 代码编写
生成
publicstaticvoid qrCode()throwsWriterException,IOException{
String filePath ="D://";
String fileName ="zxing.png";
String content=handlerEvent();
int width =200;// 图像宽度
int height =200;// 图像高度
String format ="png";// 图像类型
Map<EncodeHintType,Object> hints =newHashMap<EncodeHintType,Object>();
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
BitMatrix bitMatrix =newMultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
Path path =FileSystems.getDefault().getPath(filePath, fileName);
MatrixToImageWriter.toBufferedImage(bitMatrix);
MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像
}
vcard 生成
publicstaticString handlerEvent(){
VCard card =newVCard();
card.addEmail(newEmail("1141591465@qq.com"));
Address orgial =newAddress();
orgial.addParameter("name","dalong");
card.addAddress(newAddress(orgial));
card.addExpertise("软件开发");
Nickname nickname =newNickname();
nickname.addParameter("姓名","荣锋亮");
card.addNickname(newNickname(nickname));
card.setNickname("大龙");
card.setCategories("北京");
card.setMailer("男");
card.setGeo(116.419791,39.936404);
card.setOrganization("北京公司");
card.setProductId("beijing");
card.setClassification("开发");
Agent agent =newAgent("beijing");
card.setAgent(agent);
card.addExtendedProperty("TEL","XXXXXXXXXXXXX");
Gson json =newGson();
return card.write();
}
3. 结果
4. 微信扫描结果