package com.qxueyou.scc.base.util;
|
|
import java.awt.BasicStroke;
|
import java.awt.Color;
|
import java.awt.Graphics2D;
|
import java.awt.image.BufferedImage;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
import javax.imageio.ImageIO;
|
|
import org.apache.commons.lang3.StringUtils;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpResponse;
|
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClients;
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
|
//import com.qxueyou.scc.base.service.IConfigService;
|
import com.qxueyou.scc.shorturl.util.ShortUrlUtils;
|
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.EncodeHintType;
|
import com.google.zxing.MultiFormatWriter;
|
import com.google.zxing.WriterException;
|
import com.google.zxing.client.j2se.MatrixToImageConfig;
|
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
import com.google.zxing.common.BitMatrix;
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
/**
|
* 二维码生成工具类
|
*
|
* @author 德虎
|
*
|
*/
|
public final class QrCodeUtils {
|
|
private static Logger log = LogManager.getLogger(QrCodeUtils.class);
|
|
/**
|
* 生成二维码图片并输出到流
|
* @param msg 需要生成的二维码信息
|
* @param os 二维码图片输出流,输出后会自动关闭
|
* @param logoFlag 是否添加logo
|
*
|
*/
|
public static void createQRCodeImgAndSend(String msg, OutputStream os, boolean logoFlag) {
|
try {
|
//如果是网址,则转换为短网址
|
/*if(ShortUrlUtils.IsUrl(msg)){
|
IShortUrlService shortUrlService = SpringUtil.getBean(IShortUrlService.class);
|
ShortUrl oldShortUrl = shortUrlService.queryShortUrl(msg);
|
if(oldShortUrl != null){//判断是否已经存在数据
|
msg = oldShortUrl.getShortUrl();
|
}else{
|
//获取域
|
String domain = SpringUtil.getBean(IConfigService.class).getConfigByEnv("domain-name");
|
//获取短链接编码
|
String strShortUrlCode = ShortUrlUtils.shortUrl(msg);
|
//拼接成短链接
|
String shortUrl = domain+(domain.indexOf("/qxueyou", 10)>=0?"/s/":"/qxueyou/s/")+strShortUrlCode;
|
ShortUrl objShortUrl = new ShortUrl();
|
//保存短链接的值
|
objShortUrl.setShortUrl(shortUrl);
|
objShortUrl.setLongUrl(msg);
|
objShortUrl.setShortUrlCode(strShortUrlCode);
|
objShortUrl.setDeleteFlag(false);
|
TraceUtils.setCreateTrace(objShortUrl);
|
msg = shortUrl;
|
shortUrlService.saveShortUrl(objShortUrl);
|
}
|
}*/
|
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
|
|
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(
|
2);
|
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
|
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
|
|
BitMatrix bitMatrix = multiFormatWriter.encode(
|
msg,
|
BarcodeFormat.QR_CODE, 300, 300, hints);
|
MatrixToImageConfig config = new MatrixToImageConfig(0xFF000000, -1);
|
//添加机构图标
|
if(StringUtils.isNoneBlank(ClientUtils.getOrgLogoPath()) && ShortUrlUtils.IsUrl(msg) && logoFlag){
|
int width = bitMatrix.getWidth();
|
int height = bitMatrix.getHeight();
|
BufferedImage image = new BufferedImage(width, height,
|
BufferedImage.TYPE_INT_RGB);
|
for (int x = 0; x < width; x++) {
|
for (int y = 0; y < height; y++) {
|
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
|
: 0xFFFFFFFF);
|
}
|
}
|
addLogo_QRCode(image, os);
|
}else{
|
MatrixToImageWriter.writeToStream(bitMatrix, "PNG",
|
os, config);
|
}
|
|
} catch (WriterException e) {
|
log.error(e, e);
|
} catch (IOException e) {
|
log.error(e, e);
|
} catch (Exception e) {
|
log.error(e, e);
|
}
|
}
|
|
/**
|
* 添加机构logo
|
*
|
* @param buf 生成的二维码
|
* @return
|
* @throws IOException
|
*/
|
public static void addLogo_QRCode(BufferedImage buf, OutputStream os) throws IOException{
|
CloseableHttpClient httpClient = null;
|
HttpGet httpget = null;
|
try{
|
// String domain = SpringUtil.getBean(IConfigService.class).getConfigByEnv("oss-domain");
|
String domain =null;
|
httpClient = HttpClients.createDefault();
|
//获取机构logo
|
httpget = new HttpGet(domain+ClientUtils.getOrgLogoPath());
|
HttpResponse response = httpClient.execute(httpget);
|
HttpEntity entity = response.getEntity();
|
/**
|
* 读取Logo图片
|
*/
|
BufferedImage logo = ImageIO.read(entity.getContent());
|
/**
|
* 读取二维码图片,并构建绘图对象
|
*/
|
Graphics2D g = buf.createGraphics();
|
int widthLogo = 65, heightLogo = 65;
|
|
// 计算图片放置位置
|
int x = (buf.getWidth() - widthLogo) / 2;
|
int y = (buf.getHeight() - heightLogo) / 2;
|
|
//开始绘制图片
|
g.setColor(Color.WHITE);
|
g.drawImage(logo, x, y, widthLogo, heightLogo, null);
|
g.setStroke(new BasicStroke(1));
|
g.dispose();
|
ImageIO.write(buf, "PNG", os);
|
}catch (Exception e) {
|
ImageIO.write(buf, "PNG", os);
|
log.error("二维码添加logo失败"+e);
|
}finally {
|
httpClient.close();
|
}
|
}
|
}
|