zxing生成二维码

来源:互联网 发布:mysql如何删除主键 编辑:程序博客网 时间:2024/05/16 17:33

下面讲述使用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();
    }
    
}

最后生成效果图:


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

短短几行代码就可以实现名片二维码了,感兴趣的可以自己做下哦微笑


原创粉丝点击