springboot动态生成二维码并解决乱码问题
解决乱码问题
response.setHeader("Content-Type","image/JPG");
1 | QrCodeController |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package com.lw.project.wx.controller; import com.lw.common.utils.QRCodeUtil; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.File; /** * @author NR * @version 1.0 * @Description TODO * @date 2020/7/14 10:53 */ @RestController @RequestMapping ( "/wx" ) public class QrCodeController { /** * 根据 url 生成 普通二维码 */ @RequestMapping (value = "/createCommonQRCode" ) public void createCommonQRCode(HttpServletResponse response, String url) throws Exception { ServletOutputStream stream = null ; try { response.setHeader( "Content-Type" , "image/JPG" ); stream = response.getOutputStream(); //使用工具类生成二维码 QRCodeUtil.encode(url, stream); } catch (Exception e) { e.getStackTrace(); } finally { if (stream != null ) { stream.flush(); stream.close(); } } } /** * 根据 url 生成 带有logo二维码 */ @RequestMapping (value = "/createLogoQRCode" ) public void createLogoQRCode( HttpServletResponse response, String url) throws Exception { ServletOutputStream stream = null ; try { stream = response.getOutputStream(); String logoPath = Thread.currentThread().getContextClassLoader().getResource( "" ).getPath() + "templates" + File.separator + "logo.jpg" ; //使用工具类生成二维码 QRCodeUtil.encode(url, logoPath, stream, true ); } catch (Exception e) { e.getStackTrace(); } finally { if (stream != null ) { stream.flush(); stream.close(); } } } } |
工具类QRCodeUtil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | package com.lw.common.utils; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.OutputStream; import java.util.Hashtable; /** * QRCodeUtil 生成二维码工具类 */ public class QRCodeUtil { private static final String CHARSET = "utf-8" ; private static final String FORMAT_NAME = "JPG" ; // 二维码尺寸 private static final int QRCODE_SIZE = 300 ; // LOGO宽度 private static final int WIDTH = 60 ; // LOGO高度 private static final int HEIGHT = 60 ; private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception { Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, CHARSET); hints.put(EncodeHintType.MARGIN, 1 ); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); 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 ); } } if (imgPath == null || "" .equals(imgPath)) { return image; } // 插入图片 QRCodeUtil.insertImage(image, imgPath, needCompress); return image; } private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception { File file = new File(imgPath); if (!file.exists()) { System.err.println( "" + imgPath + " 该文件不存在!" ); return ; } Image src = ImageIO.read( new File(imgPath)); int width = src.getWidth( null ); int height = src.getHeight( null ); if (needCompress) { // 压缩LOGO if (width > WIDTH) { width = WIDTH; } if (height > HEIGHT) { height = HEIGHT; } 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(); } public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception { BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress); mkdirs(destPath); ImageIO.write(image, FORMAT_NAME, new File(destPath)); } public static BufferedImage encode(String content, String imgPath, boolean needCompress) throws Exception { BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress); return image; } public static void mkdirs(String destPath) { File file = new File(destPath); // 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常) if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } } public static void encode(String content, String imgPath, OutputStream output, boolean needCompress) throws Exception { BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress); ImageIO.write(image, FORMAT_NAME, output); } public static void encode(String content, OutputStream output) throws Exception { QRCodeUtil.encode(content, null , output, false ); } } |
nnjk
分类:
springboot
标签:
springboot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类