您的位置:首页 > 其它

zxing生成二维码

2017-07-14 09:12 225 查看
下面讲述使用zxing制作名片二维码。

首先导入zxing3.2.1.jar架包,大家可以自己下载

名片的模板大家可以登录https://en.wikipedia.org/wiki/VCard自己选择,我选择的是2.1的版本

废话不多说,直接上代码

public class CreateQRCode {

    public void test(){

    int a=(int) (Math.random()*10);

//path是二维码生成后存在的位置

    String path="F:/image/"+a+".png";

    int width=300;

    int height=300;

    String format="png";

//content是扫码之后的内容,我写的这个是名片的模板

    String content="BEGIN:VCARD"+"\n"

            + "VERSION:2.1"+"\n"

            +"FN:girl"+"\n"

            +"NICKNAME:可爱多"+"\n"

            +"ORG:******有限公司"+"\n"

            +"TITLE:java开发"+"\n"

            +"TEL;WORK;VOICE:1815659****"+"\n"

            +"TEL;CELL;VOICE:1815659****"+"\n"

            +"ADR;WORK:;;马鞍山青网电商园"+"\n"

            +"ADR;HOME:;;六安市叶集区*******;"+"\n"

            +"URL:http://blog.csdn.net/qq_34357835"+"\n"

            +"EMAIL:1197641407@qq.com"+"\n"

            +"END:VCARD";

    HashMap map=new HashMap();

    map.put(EncodeHintType.CHARACTER_SET, "utf-8");

    map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

    map.put(EncodeHintType.MARGIN, 2);

    try {

        BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, map);

        Path file=new File(path).toPath();

        MatrixToImageWriter.writeToPath(bitMatrix, format, file);

    } catch (Exception e) {

        e.printStackTrace();

    }

    }

    public static void main(String args[]){

        CreateQRCode qq=new CreateQRCode();

        qq.test();

    }

    

}

最后生成效果图:



大家可以扫下,效果图如下:



短短几行代码就可以实现名片二维码了,感兴趣的可以自己做下哦
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: