Java中生成二维码的方法

1.zxing

1).生成二维码

需要引入zxing core,zxing javase extensions 两个jar包

//设置图片宽度,高度,格式,内容
        int width=300;
        int height=300;
        String format="png";
        String content="老哥稳";
        content=new String(content.getBytes("utf-8"),"iso-8859-1");
        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 encode = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
            //写入文件
            Path file=new File("D:\\嘻嘻.png").toPath();
            MatrixToImageWriter.writeToPath(encode, format, file);


        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
2).解析二维码
        BufferedImage bi=ImageIO.read(new File("D://嘻嘻.png"));
        LuminanceSource ls=new BufferedImageLuminanceSource(bi);
        BinaryBitmap bb=new BinaryBitmap(new HybridBinarizer(ls) );
        HashMap hints=new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        MultiFormatReader mfw=new MultiFormatReader();
        Result result = mfw.decode(bb,hints);

        System.out.println("二维码读取结果:"+result.toString());

2.jquery.qrcode 生成二维码

感觉这个还是比较方便的,后台传入前台一个字符串或者URL 然后生成一个二维码。方法呢很简单。引入jquery文件一样 引入 jquery.qrcode.min.js ,然后写入如下代码,就生成一个百度的二维码吧。

<div id="code"></div>
<script type="text/javascript" src="${basePath }/resource/js/jquery.min.js"></script>
<script type="text/javascript" src="${basePath }/resource/js/jquery.qrcode.min.js"></script>
<script type="text/javascript">
$(function(){
    $("#code").qrcode("https://www.baidu.com");
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值