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,操作更方便哦