java如何生成二维码

来源:互联网 发布:ug汽车大模具编程刀路 编辑:程序博客网 时间:2024/06/10 01:24

1,去百度QR-code下一个安装包,解压

2,把 F:\zxing-master\core\src\main\java\路径下的com包和F:\zxing-master\javase\src\main\java包导入一个新的项目下,导出zixing.jar

3,把zixing.jar导入项目中

4,实现代码

 //定义生成二维码的大小

  int width=300;
  int height=300;

//生成的格式
  String format="png";

//扫码时所得到的网址并打开
  String content="http://baidu.com";
  //定义二维码数组
  HashMap hints=new HashMap();
  hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
  hints.put(EncodeHintType.MARGIN, 2);
  
  //生成二维码
  try {
  BitMatrix bitMatrix=  new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
   Path file=new File("e:/test/img.png").toPath();
   MatrixToImageWriter.writeToPath(bitMatrix, format, file);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

0 0