package com.sinata.common.util;
|
|
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.EncodeHintType;
|
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
import com.google.zxing.common.BitMatrix;
|
import com.google.zxing.qrcode.QRCodeWriter;
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
import org.apache.commons.codec.binary.Base64;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.StringUtils;
|
|
import javax.imageio.ImageIO;
|
import java.awt.image.BufferedImage;
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.HashMap;
|
|
/**
|
* 二维码工具
|
*/
|
@Component
|
public class QRCodeUtils {
|
|
public static String crateQRCode(String content, int width, int height) throws IOException {
|
if (!StringUtils.isEmpty(content)) {
|
OutputStream stream = null;
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
@SuppressWarnings("rawtypes")
|
HashMap<EncodeHintType, Comparable> hints = new HashMap<>();
|
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 指定字符编码为“utf-8”
|
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 指定二维码的纠错等级为中级
|
hints.put(EncodeHintType.MARGIN, 2); // 设置图片的边距
|
|
try {
|
QRCodeWriter writer = new QRCodeWriter();
|
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
|
|
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
|
ImageIO.write(bufferedImage, "png", os);
|
/**
|
* 原生转码前面没有 data:image/png;base64 这些字段,返回给前端是无法被解析,可以让前端加,也可以在下面加上
|
*/
|
return getBase64Image(Base64.encodeBase64String(os.toByteArray()));
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (stream != null) {
|
stream.flush();
|
stream.close();
|
}
|
}
|
}
|
return null;
|
}
|
|
/**
|
* hash 转换
|
*
|
* @param base64 String 图片流
|
* @return String
|
* @author Mr.Zhang
|
* @since 2020-06-03
|
*/
|
public static String getBase64Image(String base64) {
|
return "data:image/png;base64," + base64;
|
}
|
|
|
/**
|
* 得到文件流
|
* @param url 图片地址
|
* @return
|
*/
|
public static byte[] getFileStream(String url){
|
try {
|
URL httpUrl = new URL(url);
|
HttpURLConnection conn = (HttpURLConnection)httpUrl.openConnection();
|
conn.setRequestMethod("GET");
|
conn.setConnectTimeout(5 * 1000);
|
InputStream inStream = conn.getInputStream();//通过输入流获取图片数据
|
byte[] btImg = readInputStream(inStream);//得到图片的二进制数据
|
return btImg;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
/**
|
* 从输入流中获取数据
|
* @param inStream 输入流
|
* @return
|
* @throws Exception
|
*/
|
public static byte[] readInputStream(InputStream inStream) throws Exception{
|
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
byte[] buffer = new byte[1024];
|
int len = 0;
|
while( (len=inStream.read(buffer)) != -1 ){
|
outStream.write(buffer, 0, len);
|
}
|
inStream.close();
|
return outStream.toByteArray();
|
}
|
}
|