From 88e98b41cf76dba45a434a5d745dead053660103 Mon Sep 17 00:00:00 2001 From: Jackwang Date: Tue, 7 Jun 2022 11:15:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=B6=E5=BA=AD=E6=88=90=E5=91=98=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=B7=BB=E5=8A=A0=E7=AD=9B=E9=80=89=E7=8A=B6=E6=80=81?= =?UTF-8?q?=EF=BC=9B=E4=B8=80=E6=88=B7=E4=B8=80=E7=A0=81=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/tools/utils/HouseQRcodeUtils.java | 170 ++++++++++++++++++ .../com/epmet/controller/HouseController.java | 4 +- .../epmet/service/impl/HouseServiceImpl.java | 9 +- .../service/impl/IcResiUserServiceImpl.java | 1 + 4 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HouseQRcodeUtils.java diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HouseQRcodeUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HouseQRcodeUtils.java new file mode 100644 index 0000000000..88faf497e3 --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HouseQRcodeUtils.java @@ -0,0 +1,170 @@ +package com.epmet.commons.tools.utils; + + +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 lombok.extern.slf4j.Slf4j; + +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; + +/** + * Date: 2022/06/7 10:01 + * Description:房屋一户一码生成工具类 + */ +@Slf4j +public class HouseQRcodeUtils { + /** + * 二维码颜色 默认是黑色 + */ + private static final Color QRCOLOR = Color.black; + /** + * 背景颜色 + */ + private static final Color BGWHITE = Color.white; + public static final int WIDTH = 800; + public static final int HEIGHT = 800; + public static final int MARGIN = 2; + public static final int FONTSIZE = 20; + + + + /** + * 房屋一户一码生成-指定像素 + * + * @param contents 说明 + * @return BufferedImage + * @throws Exception + */ + public static BufferedImage drawHouseQRImage(String pressText, String contents) throws Exception { + BufferedImage qRImage = null; + if (contents == null || "".equals(contents)) { + throw new Exception("content说明不能为空"); + } + // 二维码参数设置 + HashMap 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) { + log.error("method exception", e); + } + + // 设置矩阵转为图片的参数 + MatrixToImageConfig toImageConfig = new MatrixToImageConfig(QRCOLOR.getRGB(), BGWHITE.getRGB()); + + // 矩阵转换图像 + qRImage = MatrixToImageWriter.toBufferedImage(matrix, toImageConfig); + return pressHouseText(pressText, qRImage); + } + + /** + * @param pressText 二维码下方插入文字 + * @param image 需要添加文字的图片 + * @为图片添加文字 + */ + private static BufferedImage pressHouseText(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 + FONTSIZE; + + 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("Noto Sans SC Light", 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 + 35; + } + 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 = drawHouseQRImage(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"); + } + } + +} + diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/HouseController.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/HouseController.java index 6099d6e147..da6101ff99 100644 --- a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/HouseController.java +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/HouseController.java @@ -39,9 +39,9 @@ import com.epmet.commons.tools.feign.ResultDataResolver; import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.redis.common.bean.HouseInfoCache; import com.epmet.commons.tools.security.dto.TokenDto; -import com.epmet.commons.tools.utils.BarcodeUtils; import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.commons.tools.utils.ExcelUtils; +import com.epmet.commons.tools.utils.HouseQRcodeUtils; import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.utils.poi.excel.handler.ExcelFillRowMergeStrategy; import com.epmet.commons.tools.utils.poi.excel.handler.FreezeAndFilter; @@ -443,7 +443,7 @@ public class HouseController implements ResultDataResolver { } //url组成:小程序地址?房屋编码 String url = HouseQrcodeEnum.PREFIX.getCode() + house.getHouseCode(); - BufferedImage image = BarcodeUtils.drawQRImage(house.getBuildingName() + house.getUnitNum() + house.getDoorName(), url); + BufferedImage image = HouseQRcodeUtils.drawHouseQRImage(house.getBuildingName() + house.getUnitNum() + house.getDoorName(), url); //BufferedImage 转 InputStream ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream); diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/HouseServiceImpl.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/HouseServiceImpl.java index 62d25cad3e..f621c136dd 100644 --- a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/HouseServiceImpl.java +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/HouseServiceImpl.java @@ -22,10 +22,7 @@ import com.epmet.commons.tools.redis.common.CustomerStaffRedis; import com.epmet.commons.tools.redis.common.bean.AgencyInfoCache; import com.epmet.commons.tools.redis.common.bean.HouseInfoCache; import com.epmet.commons.tools.security.dto.TokenDto; -import com.epmet.commons.tools.utils.BarcodeUtils; -import com.epmet.commons.tools.utils.ConvertUtils; -import com.epmet.commons.tools.utils.FileUtils; -import com.epmet.commons.tools.utils.Result; +import com.epmet.commons.tools.utils.*; import com.epmet.constant.CustomerGridConstant; import com.epmet.constants.ImportTaskConstants; import com.epmet.dao.*; @@ -832,7 +829,7 @@ public class HouseServiceImpl implements HouseService, ResultDataResolver { for (IcHouseListResultDTO house : houseList) { - BufferedImage image = BarcodeUtils.drawQRImage(house.getBuildingName() + house.getUnitNum() + house.getDoorName(), + BufferedImage image = HouseQRcodeUtils.drawHouseQRImage(house.getBuildingName() + house.getUnitNum() + house.getDoorName(), HouseQrcodeEnum.PREFIX.getCode() + house.getHouseCode()); try { @@ -923,7 +920,7 @@ public class HouseServiceImpl implements HouseService, ResultDataResolver { //url组成:小程序地址?房屋编码 String url = HouseQrcodeEnum.PREFIX.getCode() + house.getHouseCode(); String fileName = house.getBuildingName() + house.getUnitNum() + house.getDoorName() + ".png"; - BufferedImage image = BarcodeUtils.drawQRImage(house.getBuildingName() + house.getUnitNum() + house.getDoorName(), url); + BufferedImage image = HouseQRcodeUtils.drawHouseQRImage(house.getBuildingName() + house.getUnitNum() + house.getDoorName(), url); FileItem fileItem = new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, FileUtils.getAndCreateDirUnderEpmetFilesDir("temp").toFile()) diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/IcResiUserServiceImpl.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/IcResiUserServiceImpl.java index 873c90c39a..f00be57253 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/IcResiUserServiceImpl.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/IcResiUserServiceImpl.java @@ -1872,6 +1872,7 @@ public class IcResiUserServiceImpl extends BaseServiceImpl lqw = new LambdaQueryWrapper<>(); lqw.eq(IcResiUserEntity::getCustomerId, customerId) .eq(IcResiUserEntity::getHomeId, houseId) + .eq(IcResiUserEntity::getSubStatus,"0") .select( IcResiUserEntity::getId, IcResiUserEntity::getHomeId,