14 changed files with 612 additions and 5 deletions
@ -0,0 +1,63 @@ |
|||||
|
package com.hxgk.lowcode.controller; |
||||
|
|
||||
|
|
||||
|
import com.hxgk.lowcode.model.entity.response.QrCodeDetailsResponseEntity; |
||||
|
import com.hxgk.lowcode.service.QrCodeService; |
||||
|
import com.hxgk.lowcode.utils.JsonData; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||
|
import org.springframework.web.bind.annotation.RequestHeader; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
import java.awt.image.BufferedImage; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.LinkedHashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@RestController |
||||
|
@RequestMapping("/lowCode") |
||||
|
public class QrCodeController { |
||||
|
|
||||
|
@Autowired |
||||
|
QrCodeService qrCodeService; |
||||
|
|
||||
|
public QrCodeController(){ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/*获取用户有权限的启用状态的表单列表树形结构*/ |
||||
|
@RequestMapping(value = "/QrCode/getQrCodeImgInside") |
||||
|
public JsonData getCustomerFormList(@RequestHeader(value = "User-Key") String key, |
||||
|
@RequestHeader(value = "User-Token") String token, |
||||
|
@RequestBody Map<String,String> requestBody) { |
||||
|
String cfid = requestBody.get("cfid"); |
||||
|
if(StringUtils.isBlank(key)||StringUtils.isBlank(token)||StringUtils.isBlank(cfid)){ |
||||
|
return JsonData.buildError("请重新登录"); |
||||
|
}else{ |
||||
|
String bufferedImage = qrCodeService.generateQrCodeByCfid(cfid); |
||||
|
return JsonData.buildSuccess(bufferedImage); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/*获取用户有权限的启用状态的表单列表树形结构*/ |
||||
|
@RequestMapping(value = "/QrCode/getDetailQrCodes") |
||||
|
public JsonData getDetailQrCodes(@RequestHeader(value = "User-Key") String key, |
||||
|
@RequestHeader(value = "User-Token") String token, |
||||
|
@RequestBody Map<String,Object> requestBody) { |
||||
|
String cfid = (String)requestBody.get("cfid"); |
||||
|
if(StringUtils.isBlank(key)||StringUtils.isBlank(token)||StringUtils.isBlank(cfid)){ |
||||
|
return JsonData.buildError("请重新登录"); |
||||
|
}else{ |
||||
|
ArrayList<String> ids = (ArrayList<String>) requestBody.get("idArray"); |
||||
|
LinkedHashMap<String,Object> settings = (LinkedHashMap<String, Object>) requestBody.get("settings"); |
||||
|
LinkedHashMap<String, QrCodeDetailsResponseEntity> result = qrCodeService.getDetailQrCodes(cfid,ids,settings); |
||||
|
return JsonData.buildSuccess(result); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
package com.hxgk.lowcode.model.entity.response; |
||||
|
|
||||
|
import org.springframework.stereotype.Repository; |
||||
|
|
||||
|
import java.util.LinkedHashMap; |
||||
|
//单个二维码图片对象及其附属信息
|
||||
|
@Repository |
||||
|
public class QrCodeDetailsResponseEntity { |
||||
|
//二维码图片
|
||||
|
private String bufferedImage; |
||||
|
//要显示在表格中的字段map
|
||||
|
private LinkedHashMap<String,String> fieldsMap; |
||||
|
|
||||
|
public String getBufferedImage() { |
||||
|
return bufferedImage; |
||||
|
} |
||||
|
|
||||
|
public void setBufferedImage(String bufferedImage) { |
||||
|
this.bufferedImage = bufferedImage; |
||||
|
} |
||||
|
|
||||
|
public LinkedHashMap<String, String> getFieldsMap() { |
||||
|
return fieldsMap; |
||||
|
} |
||||
|
|
||||
|
public void setFieldsMap(LinkedHashMap<String, String> fieldsMap) { |
||||
|
this.fieldsMap = fieldsMap; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
package com.hxgk.lowcode.service; |
||||
|
|
||||
|
import com.hxgk.lowcode.model.entity.response.QrCodeDetailsResponseEntity; |
||||
|
|
||||
|
import java.awt.image.BufferedImage; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.LinkedHashMap; |
||||
|
|
||||
|
public interface QrCodeService { |
||||
|
String generateQrCodeByCfid(String cfid); |
||||
|
|
||||
|
LinkedHashMap<String, QrCodeDetailsResponseEntity> getDetailQrCodes(String cfid, ArrayList<String> ids, LinkedHashMap<String, Object> settings); |
||||
|
} |
||||
@ -0,0 +1,215 @@ |
|||||
|
package com.hxgk.lowcode.service.impl; |
||||
|
|
||||
|
import com.hxgk.lowcode.mapper.FieldRecordMapper; |
||||
|
import com.hxgk.lowcode.model.entity.CustomerFormView; |
||||
|
import com.hxgk.lowcode.model.entity.response.QrCodeDetailsResponseEntity; |
||||
|
import com.hxgk.lowcode.service.CustomerFormViewService; |
||||
|
import com.hxgk.lowcode.service.QrCodeService; |
||||
|
import com.hxgk.lowcode.utils.QRCodeUtil; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import java.awt.image.BufferedImage; |
||||
|
import java.util.*; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Service |
||||
|
public class QrCodeServiceImpl implements QrCodeService { |
||||
|
|
||||
|
@Autowired |
||||
|
CustomerFormViewService customerFormViewService; |
||||
|
@Autowired |
||||
|
FieldRecordMapper fieldRecordMapper; |
||||
|
|
||||
|
@Override |
||||
|
public String generateQrCodeByCfid(String cfid) { |
||||
|
|
||||
|
QRCodeUtil qrCodeUtil = new QRCodeUtil(); |
||||
|
String qrCodeStr = ""; |
||||
|
//String qrCodeStr = "https://wab.hxgk.group/#/form_table/taskListPage? id=112&key=287135214646333440&formid=116&formKey=287145907965661184&title=t2&state=1";
|
||||
|
String prefixLocal = "http://localhost:9998/form_table/taskListPage?"; |
||||
|
String prefixout = "https://wab.hxgk.group/#/form_table/taskListPage?"; |
||||
|
//获取参数
|
||||
|
/* |
||||
|
* |
||||
|
id cfid |
||||
|
key groupid |
||||
|
formid id |
||||
|
formKey signCode |
||||
|
title name |
||||
|
state states |
||||
|
* */ |
||||
|
CustomerFormView c = customerFormViewService.getCustomerFormViewByCfid(cfid); |
||||
|
//上线修改
|
||||
|
qrCodeStr = prefixout+"id="+c.getCfid()+"&key="+c.getGroupid()+"&formid="+c.getId()+"&formKey="+c.getSignCode()+"&title="+c.getName()+"&state="+c.getStates(); |
||||
|
System.out.println(qrCodeStr); |
||||
|
try { |
||||
|
BufferedImage bufferedImage = qrCodeUtil.getQRCodeImage(qrCodeStr, "生成的二维码"); |
||||
|
String base64 = qrCodeUtil.bufferedImageToBase64(bufferedImage); |
||||
|
return base64; |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public LinkedHashMap<String, QrCodeDetailsResponseEntity> getDetailQrCodes(String cfid, ArrayList<String> ids, LinkedHashMap<String, Object> settings) { |
||||
|
LinkedHashMap<String, QrCodeDetailsResponseEntity> result = new LinkedHashMap<String, QrCodeDetailsResponseEntity>(); |
||||
|
QRCodeUtil qrCodeUtil = new QRCodeUtil(); |
||||
|
String basicQrCodeStr = ""; |
||||
|
//String basicQrCodeStr = "https://wab.hxgk.group/#/form_table/taskListPage? id=112&key=287135214646333440&formid=116&formKey=287145907965661184&title=t2&state=1";
|
||||
|
String prefixLocal = "http://localhost:9998/form_table/taskListPage?"; |
||||
|
String prefixout = "https://wab.hxgk.group/#/form_table/taskListPage?"; |
||||
|
CustomerFormView c = customerFormViewService.getCustomerFormViewByCfid(cfid); |
||||
|
//上线修改
|
||||
|
basicQrCodeStr = prefixout+"id="+c.getCfid()+"&key="+c.getGroupid()+"&formid="+c.getId()+"&formKey="+c.getSignCode()+"&title="+c.getName()+"&state="+c.getStates(); |
||||
|
//查询要展示在表格中的信息
|
||||
|
ArrayList<String> qrCodeShowFields = new ArrayList<>(); |
||||
|
Object qrCodePrintStyle = settings.get("qrCodePrintStyle"); |
||||
|
String qrCodePrintStyleStr = ""; |
||||
|
if(qrCodePrintStyle==null){ |
||||
|
qrCodePrintStyle = (Object) "1"; |
||||
|
} |
||||
|
if(qrCodePrintStyle!=null&&!StringUtils.isBlank((String)qrCodePrintStyle)){ |
||||
|
qrCodePrintStyleStr = (String)qrCodePrintStyle; |
||||
|
if(qrCodePrintStyleStr.equals("2")){//需要根据已选择的字段拼装表格
|
||||
|
qrCodeShowFields = (ArrayList<String>) settings.get("qrCodeShowFields"); |
||||
|
//表名
|
||||
|
String dbFormName = (String)settings.get("name"); |
||||
|
//字段List
|
||||
|
ArrayList<String> stringArrayList = keepLastPartAfterColon(qrCodeShowFields); |
||||
|
//根据list查询备注
|
||||
|
List<Map<String, String>> fieldsAndComments = fieldRecordMapper.getColumnComments(dbFormName,stringArrayList); |
||||
|
HashMap<String, String> fieldsAndComments1 = convertListToHashMap(fieldsAndComments); |
||||
|
LinkedHashMap<String, String> fieldsAndCommentsResult = sortMapByList(stringArrayList, fieldsAndComments1); |
||||
|
fieldsAndCommentsResult = updateEmptyValues(fieldsAndCommentsResult); |
||||
|
for (String id:ids) { |
||||
|
QrCodeDetailsResponseEntity qrCodeDetailsResponseEntity = new QrCodeDetailsResponseEntity(); |
||||
|
String currentQrCodeStr = basicQrCodeStr+"&qrDetailId="+id; |
||||
|
BufferedImage currentBufferedImage = null; |
||||
|
try { |
||||
|
currentBufferedImage = qrCodeUtil.getQRCodeImage(currentQrCodeStr, "生成的单条信息详情二维码"); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
//当前id的二维码base64图片
|
||||
|
String currentBase64 = qrCodeUtil.bufferedImageToBase64(currentBufferedImage); |
||||
|
LinkedHashMap<String,String> currentFieldsMap = new LinkedHashMap<>(); |
||||
|
if(qrCodeShowFields.size()>0){ |
||||
|
//用户要输出表格而且至少选择了一条要放在表格中的属性
|
||||
|
//需要根据已选择的字段拼装表格
|
||||
|
LinkedHashMap<String,String> map = fieldRecordMapper.getQrCodeTableFields(dbFormName,stringArrayList,id); |
||||
|
map = convertMap(map); |
||||
|
currentFieldsMap = mergeMaps(fieldsAndCommentsResult, map); |
||||
|
System.out.println(1); |
||||
|
} |
||||
|
qrCodeDetailsResponseEntity.setBufferedImage(currentBase64); |
||||
|
qrCodeDetailsResponseEntity.setFieldsMap(currentFieldsMap); |
||||
|
result.put(id,qrCodeDetailsResponseEntity); |
||||
|
} |
||||
|
}else{ |
||||
|
for (String id:ids) { |
||||
|
QrCodeDetailsResponseEntity qrCodeDetailsResponseEntity = new QrCodeDetailsResponseEntity(); |
||||
|
String currentQrCodeStr = basicQrCodeStr+"&qrDetailId="+id; |
||||
|
BufferedImage currentBufferedImage = null; |
||||
|
try { |
||||
|
currentBufferedImage = qrCodeUtil.getQRCodeImage(currentQrCodeStr, "生成的单条信息详情二维码"); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
//当前id的二维码base64图片
|
||||
|
String currentBase64 = qrCodeUtil.bufferedImageToBase64(currentBufferedImage); |
||||
|
//LinkedHashMap<String,String> currentFieldsMap = new LinkedHashMap<>();
|
||||
|
|
||||
|
qrCodeDetailsResponseEntity.setBufferedImage(currentBase64); |
||||
|
//qrCodeDetailsResponseEntity.setFieldsMap(currentFieldsMap);
|
||||
|
result.put(id,qrCodeDetailsResponseEntity); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public ArrayList<String> keepLastPartAfterColon(ArrayList<String> inputList) { |
||||
|
ArrayList<String> resultList = new ArrayList<>(); |
||||
|
for (String str : inputList) { |
||||
|
int lastColonIndex = str.lastIndexOf(':'); |
||||
|
if (lastColonIndex!= -1) { |
||||
|
// 截取最后一个 : 后面的部分,包括 : 本身
|
||||
|
String lastPart = str.substring(lastColonIndex+1); |
||||
|
resultList.add(lastPart); |
||||
|
} else { |
||||
|
resultList.add(str); |
||||
|
} |
||||
|
} |
||||
|
return resultList; |
||||
|
} |
||||
|
|
||||
|
public HashMap<String, String> convertListToHashMap(List<Map<String, String>> list) { |
||||
|
HashMap<String, String> resultMap = new HashMap<>(); |
||||
|
for (Map<String, String> map : list) { |
||||
|
String columnName = map.get("COLUMN_NAME"); |
||||
|
String columnComment = map.get("COLUMN_COMMENT"); |
||||
|
if (columnName!= null && columnComment!= null) { |
||||
|
resultMap.put(columnName, columnComment); |
||||
|
} |
||||
|
} |
||||
|
return resultMap; |
||||
|
} |
||||
|
|
||||
|
public LinkedHashMap<String, String> sortMapByList(ArrayList<String> a, Map<String, String> b) { |
||||
|
LinkedHashMap<String, String> c = new LinkedHashMap<>(); |
||||
|
for (String key : a) { |
||||
|
if (b.containsKey(key)) { |
||||
|
c.put(key, b.get(key)); |
||||
|
} |
||||
|
} |
||||
|
return c; |
||||
|
} |
||||
|
|
||||
|
public LinkedHashMap<String, String> updateEmptyValues(LinkedHashMap<String, String> inputMap) { |
||||
|
LinkedHashMap<String, String> resultMap = new LinkedHashMap<>(); |
||||
|
for (Map.Entry<String, String> entry : inputMap.entrySet()) { |
||||
|
String key = entry.getKey(); |
||||
|
String value = entry.getValue(); |
||||
|
if (value == null || value.isEmpty()) { |
||||
|
resultMap.put(key, key); |
||||
|
} else { |
||||
|
resultMap.put(key, value); |
||||
|
} |
||||
|
} |
||||
|
return resultMap; |
||||
|
} |
||||
|
public LinkedHashMap<String, String> mergeMaps(LinkedHashMap<String, String> a, LinkedHashMap<String, String> b) { |
||||
|
LinkedHashMap<String, String> c = new LinkedHashMap<>(); |
||||
|
for (String key : a.keySet()) { |
||||
|
if (b.containsKey(key)) { |
||||
|
String newValue = a.get(key) + "!@#@!" + b.get(key); |
||||
|
c.put(key, newValue); |
||||
|
} else { |
||||
|
// 可根据需求添加异常处理,此处暂时忽略不存在键的情况
|
||||
|
} |
||||
|
} |
||||
|
return c; |
||||
|
} |
||||
|
|
||||
|
public LinkedHashMap<String, String> convertMap(LinkedHashMap<String,?> a) { |
||||
|
LinkedHashMap<String, String> b = new LinkedHashMap<>(); |
||||
|
for (Map.Entry<String,?> entry : a.entrySet()) { |
||||
|
String key = entry.getKey(); |
||||
|
// 将值转换为字符串
|
||||
|
String value = String.valueOf(entry.getValue()); |
||||
|
b.put(key, value); |
||||
|
} |
||||
|
return b; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,223 @@ |
|||||
|
package com.hxgk.lowcode.utils; |
||||
|
|
||||
|
import com.google.zxing.BarcodeFormat; |
||||
|
import com.google.zxing.EncodeHintType; |
||||
|
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.core.io.ClassPathResource; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
import sun.font.FontDesignMetrics; |
||||
|
|
||||
|
import javax.imageio.ImageIO; |
||||
|
import java.awt.*; |
||||
|
import java.awt.font.FontRenderContext; |
||||
|
import java.awt.font.LineMetrics; |
||||
|
import java.awt.geom.RoundRectangle2D; |
||||
|
import java.awt.image.BufferedImage; |
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.util.HashMap; |
||||
|
|
||||
|
/** |
||||
|
* 二维码生成工具类 |
||||
|
* Created by chenzan on 2022/09/19 |
||||
|
*/ |
||||
|
public class QRCodeUtil { |
||||
|
private static final int QRCODE_SIZE = 320; // 二维码尺寸,宽度和高度均是320
|
||||
|
private static final String FORMAT_TYPE = "PNG"; // 二维码图片类型
|
||||
|
|
||||
|
/** |
||||
|
* 默认需要logo,无底部文字 |
||||
|
* 返回 BufferedImage 可以使用ImageIO.write(BufferedImage, "png", outputStream);输出 |
||||
|
* |
||||
|
* @param dataStr |
||||
|
* @return 返回 BufferedImage 可以使用ImageIO.write(BufferedImage, "png", outputStream);输出 |
||||
|
*/ |
||||
|
|
||||
|
public static BufferedImage getQRCodeImage(String dataStr) throws Exception { |
||||
|
BufferedImage bufferedImage = getQRCodeImage(dataStr, true, null); |
||||
|
return bufferedImage; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 默认需要logo,无底部文字 |
||||
|
* |
||||
|
* @param dataStr |
||||
|
* @return 返回字节数组 |
||||
|
*/ |
||||
|
|
||||
|
public static byte[] getQRCodeByte(String dataStr) throws Exception{ |
||||
|
BufferedImage bufferedImage = getQRCodeImage(dataStr, true, null); |
||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
||||
|
ImageIO.write(bufferedImage, FORMAT_TYPE, outputStream); |
||||
|
byte[] byteData = outputStream.toByteArray(); |
||||
|
return byteData; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 默认需要logo,包含底部文字 文字为空则不显示文字 |
||||
|
* 返回 BufferedImage 可以使用ImageIO.write(BufferedImage, "png", outputStream);输出 |
||||
|
* |
||||
|
* @param dataStr |
||||
|
* @return |
||||
|
*/ |
||||
|
|
||||
|
public static BufferedImage getQRCodeImage(String dataStr, String bottomText) throws Exception { |
||||
|
BufferedImage bufferedImage = getQRCodeImage(dataStr, true, bottomText); |
||||
|
return bufferedImage; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 默认需要logo,包含底部文字 文字为空则不显示文字 |
||||
|
* |
||||
|
* @param dataStr |
||||
|
* @return 返回字节数组 |
||||
|
*/ |
||||
|
|
||||
|
public static byte[] getQRCodeByte(String dataStr, String bottomText) throws Exception { |
||||
|
BufferedImage bufferedImage = getQRCodeImage(dataStr, true, bottomText); |
||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
||||
|
ImageIO.write(bufferedImage, FORMAT_TYPE, outputStream); |
||||
|
byte[] byteData = outputStream.toByteArray(); |
||||
|
return byteData; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取二维码图片 |
||||
|
* |
||||
|
* @param dataStr 二维码内容 |
||||
|
* @param needLogo 是否需要添加logo |
||||
|
* @param bottomText 底部文字 为空则不显示 |
||||
|
* @return |
||||
|
*/ |
||||
|
|
||||
|
public static BufferedImage getQRCodeImage(String dataStr, boolean needLogo, String bottomText) throws Exception { |
||||
|
needLogo = false; |
||||
|
if (dataStr == null) { |
||||
|
throw new RuntimeException("未包含任何信息"); |
||||
|
} |
||||
|
HashMap<EncodeHintType, Object> hints = new HashMap<>(); |
||||
|
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //定义内容字符集的编码 GB2312 GBK utf-8
|
||||
|
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); //定义纠错等级
|
||||
|
hints.put(EncodeHintType.MARGIN, 1); |
||||
|
QRCodeWriter qrCodeWriter = new QRCodeWriter(); |
||||
|
BitMatrix bitMatrix = qrCodeWriter.encode(dataStr, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); |
||||
|
|
||||
|
int width = bitMatrix.getWidth(); |
||||
|
int height = bitMatrix.getHeight(); |
||||
|
int tempHeight = height; |
||||
|
if (StringUtils.hasText(bottomText)) { |
||||
|
tempHeight = tempHeight + 12; |
||||
|
} |
||||
|
BufferedImage image = new BufferedImage(width, tempHeight, 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); |
||||
|
} |
||||
|
} |
||||
|
// 判断是否添加logo
|
||||
|
if (needLogo) { |
||||
|
insertLogoImage(image); |
||||
|
} |
||||
|
// 判断是否添加底部文字
|
||||
|
if (StringUtils.hasText(bottomText)) { |
||||
|
addFontImage(image, bottomText); |
||||
|
} |
||||
|
return image; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 插入logo图片 |
||||
|
* |
||||
|
* @param source 二维码图片 |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
private static void insertLogoImage(BufferedImage source) throws Exception { |
||||
|
// 默认logo放于resource/static/image目录下
|
||||
|
ClassPathResource classPathResource = new ClassPathResource("static/image/logo.png"); |
||||
|
InputStream inputStream = classPathResource.getInputStream(); |
||||
|
if (inputStream == null || inputStream.available() == 0) { |
||||
|
return; |
||||
|
} |
||||
|
Image src = ImageIO.read(inputStream); |
||||
|
int width = 30; |
||||
|
int height = 30; |
||||
|
|
||||
|
Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH); |
||||
|
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
||||
|
Graphics g = tag.getGraphics(); |
||||
|
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
|
||||
|
g.dispose(); |
||||
|
src = image; |
||||
|
|
||||
|
// 插入LOGO
|
||||
|
Graphics2D graph = source.createGraphics(); |
||||
|
int x = (QRCODE_SIZE - width) / 2; |
||||
|
int y = (QRCODE_SIZE - height) / 2; |
||||
|
graph.drawImage(src, x, y, width, height, null); |
||||
|
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6); |
||||
|
graph.setStroke(new BasicStroke(3f)); |
||||
|
graph.draw(shape); |
||||
|
graph.dispose(); |
||||
|
} |
||||
|
|
||||
|
private static void addFontImage(BufferedImage source, String declareText) throws Exception{ |
||||
|
//生成image
|
||||
|
int defineWidth = QRCODE_SIZE; |
||||
|
int defineHeight = 20; |
||||
|
BufferedImage textImage = new BufferedImage(defineWidth, defineHeight, BufferedImage.TYPE_INT_RGB); |
||||
|
Graphics2D g2 = (Graphics2D) textImage.getGraphics(); |
||||
|
//开启文字抗锯齿
|
||||
|
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
||||
|
g2.setBackground(Color.WHITE); |
||||
|
g2.clearRect(0, 0, defineWidth, defineHeight); |
||||
|
g2.setPaint(Color.BLACK); |
||||
|
FontRenderContext context = g2.getFontRenderContext(); |
||||
|
//部署linux需要注意 linux无此字体会显示方块
|
||||
|
Font font = new Font("宋体", Font.BOLD, 15); |
||||
|
g2.setFont(font); |
||||
|
LineMetrics lineMetrics = font.getLineMetrics(declareText, context); |
||||
|
FontMetrics fontMetrics = FontDesignMetrics.getMetrics(font); |
||||
|
float offset = (defineWidth - fontMetrics.stringWidth(declareText)) / 2; |
||||
|
float y = (defineHeight + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading()) / 2; |
||||
|
g2.drawString(declareText, (int) offset, (int) y); |
||||
|
|
||||
|
Graphics2D graph = source.createGraphics(); |
||||
|
//开启文字抗锯齿
|
||||
|
graph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
||||
|
//添加image
|
||||
|
int width = textImage.getWidth(null); |
||||
|
int height = textImage.getHeight(null); |
||||
|
|
||||
|
Image src = textImage; |
||||
|
graph.drawImage(src, 0, QRCODE_SIZE - 8, width, height, Color.WHITE, null); |
||||
|
graph.dispose(); |
||||
|
} |
||||
|
/* |
||||
|
|
||||
|
|
||||
|
* */ |
||||
|
|
||||
|
/** |
||||
|
* BufferedImage转base64 |
||||
|
* @param bufferedImage |
||||
|
* @return |
||||
|
*/ |
||||
|
public String bufferedImageToBase64(BufferedImage bufferedImage) { |
||||
|
ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
||||
|
try { |
||||
|
// 设置图片格式
|
||||
|
ImageIO.write(bufferedImage, "jpg", stream); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
byte[] bytes = Base64.encodeBase64(stream.toByteArray()); |
||||
|
String base64 = new String(bytes); |
||||
|
return "data:image/jpeg;base64," + base64; |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue