24 changed files with 464 additions and 69 deletions
@ -0,0 +1,177 @@ |
|||
package com.epmet.commons.tools.utils; |
|||
|
|||
/** |
|||
* desc: |
|||
* |
|||
* @author: LiuJanJun |
|||
* @date: 2022/3/18 11:57 上午 |
|||
* @version: 1.0 |
|||
*/ |
|||
|
|||
|
|||
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.common.CharacterSetECI; |
|||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; |
|||
|
|||
import javax.imageio.ImageIO; |
|||
import java.awt.*; |
|||
import java.awt.image.BufferedImage; |
|||
import java.io.ByteArrayOutputStream; |
|||
import java.io.File; |
|||
import java.io.IOException; |
|||
import java.util.HashMap; |
|||
|
|||
/** |
|||
* Author 程鹏 |
|||
* Date: 2021/08/27 16:01 |
|||
* Description:二维码生成工具类 |
|||
*/ |
|||
public class BarcodeUtils { |
|||
/** |
|||
* 二维码颜色 默认是黑色 |
|||
*/ |
|||
private static final Color QRCOLOR = Color.black; |
|||
/** |
|||
* 背景颜色 |
|||
*/ |
|||
private static final Color BGWHITE = Color.white; |
|||
public static final int WIDTH = 360; |
|||
public static final int HEIGHT = 512; |
|||
public static final int MARGIN = 2; |
|||
public static final int FONTSIZE = 20; |
|||
|
|||
|
|||
/** |
|||
* // 二维码生成
|
|||
* |
|||
* @param contents 说明 |
|||
* @return BufferedImage |
|||
* @throws Exception |
|||
*/ |
|||
public static BufferedImage drawQRImage(String pressText, String contents) throws Exception { |
|||
BufferedImage qRImage = null; |
|||
if (contents == null || "".equals(contents)) { |
|||
throw new Exception("content说明不能为空"); |
|||
} |
|||
// 二维码参数设置
|
|||
HashMap<EncodeHintType, Object> hints = new HashMap<>(); |
|||
hints.put(EncodeHintType.CHARACTER_SET, CharacterSetECI.UTF8); // 编码设置
|
|||
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 安全等级,最高h
|
|||
hints.put(EncodeHintType.MARGIN, MARGIN); // 设置margin=0-10
|
|||
|
|||
// 二维码图片的生成
|
|||
BarcodeFormat format = BarcodeFormat.QR_CODE; |
|||
// 创建矩阵容器
|
|||
BitMatrix matrix = null; |
|||
try { |
|||
matrix = new MultiFormatWriter().encode(contents, format, WIDTH, HEIGHT, hints); |
|||
} catch (WriterException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
// 设置矩阵转为图片的参数
|
|||
MatrixToImageConfig toImageConfig = new MatrixToImageConfig(QRCOLOR.getRGB(), BGWHITE.getRGB()); |
|||
|
|||
// 矩阵转换图像
|
|||
qRImage = MatrixToImageWriter.toBufferedImage(matrix, toImageConfig); |
|||
return pressText(pressText, qRImage); |
|||
} |
|||
|
|||
/** |
|||
* @param pressText 二维码下方插入文字 |
|||
* @param image 需要添加文字的图片 |
|||
* @为图片添加文字 |
|||
*/ |
|||
private static BufferedImage pressText(String pressText, BufferedImage image) throws Exception { |
|||
|
|||
BufferedImage outImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); |
|||
//计算文字开始的位置
|
|||
//x开始的位置:(图片宽度-字体大小*字的个数)/2
|
|||
int startX = (WIDTH - (FONTSIZE * pressText.length())) / 2; |
|||
//y开始的位置:图片高度-(图片高度-图片宽度)/2
|
|||
int startY = HEIGHT - (HEIGHT - WIDTH) / 2; |
|||
|
|||
int imageW = outImage.getWidth(); |
|||
int imageH = outImage.getHeight(); |
|||
Graphics2D g = outImage.createGraphics(); |
|||
g.drawImage(image, 0, 0, imageW, imageH, null); |
|||
g.setColor(QRCOLOR); |
|||
g.setFont(new Font("微软雅黑", Font.BOLD, FONTSIZE)); |
|||
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
|||
g.setBackground(Color.white); |
|||
// 获取文字数量 按照字节展示
|
|||
int size = pressText.getBytes("GBK").length; |
|||
// 获取一行最多能容纳多少文字 按照文字字节展示
|
|||
int maxSize = (WIDTH / FONTSIZE - 2) * 2; |
|||
if (size > maxSize) { |
|||
int v = size % maxSize; |
|||
for (int a = 0; a < (size / maxSize); a++) { |
|||
String s = outStringByByte(pressText, maxSize); |
|||
g.drawString(s, (WIDTH - (FONTSIZE * (WIDTH / FONTSIZE - 2))) / 2, startY); |
|||
pressText = pressText.substring(s.length(), pressText.length()); |
|||
startY = startY + 30; |
|||
} |
|||
if (v != 0) { |
|||
g.drawString(pressText, (WIDTH - (FONTSIZE * v)) / 2, startY); |
|||
} |
|||
} else { |
|||
g.drawString(pressText, (WIDTH - ((pressText.getBytes("GBK").length) / 2) * FONTSIZE) / 2, startY); |
|||
} |
|||
|
|||
g.dispose(); |
|||
return outImage; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 保存二维码图片到本地 |
|||
* |
|||
* @param contents |
|||
* @throws Exception |
|||
*/ |
|||
public static void createImg(String pressText, String contents, String filename, String filePath) throws Exception { |
|||
BufferedImage qRImageWithLogo = drawQRImage(pressText, contents); |
|||
// 写入返回
|
|||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|||
ImageIO.write(qRImageWithLogo, "jpg", baos); |
|||
//图片类型
|
|||
String imageType = "jpg"; |
|||
//生成二维码存放文件
|
|||
File file = new File(filePath + filename + ".jpg"); |
|||
if (!file.exists()) { |
|||
file.mkdirs(); |
|||
} |
|||
ImageIO.write(qRImageWithLogo, imageType, file); |
|||
baos.close(); |
|||
} |
|||
|
|||
|
|||
private static String outStringByByte(String str, int len) throws IOException { |
|||
byte[] btf = str.getBytes("gbk"); |
|||
int count = 0; |
|||
|
|||
for (int j = len - 1; j >= 0; j--) { |
|||
if (btf[j] < 0) { |
|||
count++; |
|||
} else { |
|||
break; |
|||
} |
|||
} |
|||
|
|||
if (count % 2 == 0) { |
|||
return new String(btf, 0, len, "gbk"); |
|||
} else { |
|||
return new String(btf, 0, len - 1, "gbk"); |
|||
} |
|||
|
|||
} |
|||
|
|||
|
|||
} |
|||
|
@ -0,0 +1,16 @@ |
|||
package com.epmet.dao; |
|||
|
|||
import com.epmet.commons.mybatis.dao.BaseDao; |
|||
import com.epmet.entity.UserRegGridChangeRecEntity; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
|
|||
/** |
|||
* 用户注册网格变更记录表 |
|||
* |
|||
* @author generator generator@elink-cn.com |
|||
* @since v1.0.0 2022-03-17 |
|||
*/ |
|||
@Mapper |
|||
public interface UserRegGridChangeRecDao extends BaseDao<UserRegGridChangeRecEntity> { |
|||
|
|||
} |
@ -0,0 +1,41 @@ |
|||
package com.epmet.entity; |
|||
|
|||
import com.baomidou.mybatisplus.annotation.TableName; |
|||
import com.epmet.commons.mybatis.entity.BaseEpmetEntity; |
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
|
|||
/** |
|||
* 用户注册网格变更记录表 |
|||
* |
|||
* @author generator generator@elink-cn.com |
|||
* @since v1.0.0 2022-03-17 |
|||
*/ |
|||
@Data |
|||
@EqualsAndHashCode(callSuper=false) |
|||
@TableName("user_reg_grid_change_rec") |
|||
public class UserRegGridChangeRecEntity extends BaseEpmetEntity { |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
/** |
|||
* 客户id |
|||
*/ |
|||
private String customerId; |
|||
|
|||
/** |
|||
* 居民端用户id |
|||
*/ |
|||
private String userId; |
|||
|
|||
/** |
|||
* 原始网格id |
|||
*/ |
|||
private String originGridId; |
|||
|
|||
/** |
|||
* 当前所选择的注册网格 |
|||
*/ |
|||
private String gridId; |
|||
|
|||
} |
@ -0,0 +1,14 @@ |
|||
CREATE TABLE `user_reg_grid_change_rec` ( |
|||
`ID` varchar(64) NOT NULL COMMENT '主键', |
|||
`CUSTOMER_ID` varchar(64) NOT NULL COMMENT '客户id', |
|||
`USER_ID` varchar(64) NOT NULL COMMENT '居民端用户id', |
|||
`ORIGIN_GRID_ID` varchar(64) NOT NULL COMMENT '原始网格id', |
|||
`GRID_ID` varchar(64) NOT NULL COMMENT '当前所选择的注册网格', |
|||
`DEL_FLAG` varchar(1) NOT NULL DEFAULT '0' COMMENT '删除标识 1删除;0未删除', |
|||
`REVISION` int(11) NOT NULL COMMENT '乐观锁', |
|||
`CREATED_BY` varchar(32) NOT NULL COMMENT '创建人', |
|||
`CREATED_TIME` datetime NOT NULL COMMENT '创建时间', |
|||
`UPDATED_BY` varchar(32) NOT NULL COMMENT '更新人', |
|||
`UPDATED_TIME` datetime NOT NULL COMMENT '更新时间', |
|||
PRIMARY KEY (`ID`) |
|||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户注册网格变更记录表'; |
@ -0,0 +1,8 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
|||
|
|||
<mapper namespace="com.epmet.dao.UserRegGridChangeRecDao"> |
|||
|
|||
|
|||
|
|||
</mapper> |
Loading…
Reference in new issue