生成二维码

  以下是生成二维码的具体代码:

   int width=300;//设置二维码宽度
        int height=300;//设置二维码高度
        String format="png";//图片格式
        String content="这里是要扫出来的内容";//内容
        HashMap hints=new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //字体格式
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//纠错等级
        hints.put(EncodeHintType.MARGIN, 3);//边距
        try {
            BitMatrix bitmatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
            //建立一个新的路径
            Path file=new File("指定路径").toPath();
            MatrixToImageWriter.writeToPath(bitmatrix, format, file);
        } catch (Exception e) {
            // TODO: handle exception
        }

  ps:这里需要导一个zxing3.3.0.jar包

posted @ 2017-02-18 21:19  ʚ一只咩ɞ  阅读(193)  评论(0编辑  收藏  举报