You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package com.ccsens.util;
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
import net.glxn.qrgen.core.image.ImageType;
|
|
|
|
import net.glxn.qrgen.javase.QRCode;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 二维码工具类
|
|
|
|
* @author: wuHuiJuan
|
|
|
|
* @create: 2019/12/25 10:36
|
|
|
|
*/
|
|
|
|
public class QrCodeUtil {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 根据文件路径生成二维码
|
|
|
|
* @param url
|
|
|
|
* @param parentPath
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public static String urlToQRCode(String url, String parentPath) throws IOException {
|
|
|
|
String fileName = "qrCode/" + DateUtil.today() + "/" + System.currentTimeMillis() + ".png";
|
|
|
|
ByteArrayOutputStream stream = QRCode.from(url).to(ImageType.BMP).stream();
|
|
|
|
byte[] codeByte = stream.toByteArray();
|
|
|
|
File file = new File(parentPath, fileName);
|
|
|
|
if (!file.getParentFile().exists()) {
|
|
|
|
file.getParentFile().mkdirs();
|
|
|
|
}
|
|
|
|
OutputStream out = null;
|
|
|
|
try {
|
|
|
|
out = new FileOutputStream(file);
|
|
|
|
out.write(codeByte);
|
|
|
|
out.flush();
|
|
|
|
} finally {
|
|
|
|
if ( out!= null) {
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 根据文件路径生成二维码(可以调整大小)
|
|
|
|
* @param url
|
|
|
|
* @param parentPath
|
|
|
|
* @param type 图片类型。0缩略图 1打印图
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public static String urlToQRCodeWithSize(String url, String parentPath,int type) throws IOException {
|
|
|
|
String name = System.currentTimeMillis()+"";
|
|
|
|
int size = 200;
|
|
|
|
if(type == 0){
|
|
|
|
name = "缩略图";
|
|
|
|
size = 200;
|
|
|
|
}else if(type == 1){
|
|
|
|
name = "打印图";
|
|
|
|
size = 1000;
|
|
|
|
}
|
|
|
|
String fileName = name + ".png";
|
|
|
|
ByteArrayOutputStream stream = QRCode.from(url).to(ImageType.BMP).withSize(size,size).stream();
|
|
|
|
byte[] codeByte = stream.toByteArray();
|
|
|
|
File file = new File(parentPath, fileName);
|
|
|
|
if (!file.getParentFile().exists()) {
|
|
|
|
file.getParentFile().mkdirs();
|
|
|
|
}
|
|
|
|
OutputStream out = null;
|
|
|
|
try {
|
|
|
|
out = new FileOutputStream(file);
|
|
|
|
out.write(codeByte);
|
|
|
|
out.flush();
|
|
|
|
} finally {
|
|
|
|
if ( out!= null) {
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|