4 changed files with 176 additions and 8 deletions
			
			
		@ -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<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) { | 
				
			||||
 | 
					            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"); | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					} | 
				
			||||
 | 
					
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue