javascript和java两种方式实现生成二维码功能

java实现过程

使用maven工程实现生成二维码功能

pom.xml文件引入依赖

     <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.2.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>

 

java类文件

package com.ltj.Controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

//生成二维码

public class GetQrCode{


    //二维码存放地址
    private static final String filePath = "C:\\Users\\Administrator\\Desktop\\";


    public static void main(String[] arg){

        try{

            //二维码内容
            String contents = "别说话,我爱你!";
            try{
                // 解决中文汉字的字符编码
                contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1");
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            getQrCodeByInfo(contents,400,400,filePath + "img.png");
        }catch (Exception e1){
            System.out.println("Can not generate Qr Code:" + e1.getMessage());
        }
    }


    //实现二维码方法

    /**
     *
     * @param text          二维码内容
     * @param width         二维码宽度
     * @param height        二维码高度
     * @param winPath       二维码存放路径
     * @throws WriterException
     * @throws IOException
     */
    public static void getQrCodeByInfo(String text,int width,int height,String winPath) throws WriterException,IOException{
        QRCodeWriter writer = new QRCodeWriter();

        BitMatrix bitMatrix = writer.encode(text, BarcodeFormat.QR_CODE,width,height);

        //二维码存放路径
        Path path = FileSystems.getDefault().getPath(winPath);

        //将需要的信息转化为图片
        MatrixToImageWriter.writeToPath(bitMatrix,"PNG",path);

    }

}

 

 

 

------------------------------------------------------------------------------------ 

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>二维码入门Demo</title>
</head>
<body>
<img id="qrious">
<script type="text/javascript" src="../doc/static/js/qrious.js"></script>
<script>
    var qr = new QRious({
        element:document.getElementById('qrious'),
        size:150,
        level:'H',
        value:'hello'
    });
</script>
</body>
</html>

qrious.js文件附送:

链接:https://pan.baidu.com/s/1YrcFt-Mkzg0mHTuM3CVi4Q 
提取码:9q6v 
复制这段内容后打开百度网盘手机App,操作更方便哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值