java 二维码生成(vcard)

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

 4. 微信扫描结果
 
 
 





posted on 2017-05-26 14:29  荣锋亮  阅读(1047)  评论(0编辑  收藏  举报

导航