Hutool生成图片二维码 输出到前端
生活随笔
收集整理的這篇文章主要介紹了
Hutool生成图片二维码 输出到前端
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文檔地址
https://hutool.cn/docs/#/extra/%E4%BA%8C%E7%BB%B4%E7%A0%81%E5%B7%A5%E5%85%B7-QrCodeUtil
?
pom引入
?
hutool jar <dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.0.7</version> </dependency>考慮到Hutool的非強(qiáng)制依賴(lài)性,因此zxing需要用戶(hù)自行引入 <dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version> </dependency>編譯需要 <build><resources><resource><directory>src/main/resources</directory><includes><include>**/*</include></includes></resource></resources> </build>后端代碼
/*** 生成二維碼* @param content 內(nèi)容* @param logourl logo*/public void createQrCodeN(String content, String logourl, int width, int height,HttpServletResponse response){QrConfig config = new QrConfig(width, height);File file = null;try {file = ResourceUtils.getFile(logourl);} catch (FileNotFoundException e) {e.printStackTrace();}//附帶logoconfig.setImg(file);// 設(shè)置邊距,既二維碼和背景之間的邊距config.setMargin(3);// 高糾錯(cuò)級(jí)別config.setErrorCorrection(ErrorCorrectionLevel.H);// 設(shè)置前景色,既二維碼顏色(青色)config.setForeColor(new Color(0,60,130).getRGB()); // 設(shè)置背景色(灰色)config.setBackColor(new Color(242,242,242).getRGB());/* try {OutputStream out = new FileOutputStream("");} catch (FileNotFoundException e) {e.printStackTrace();}*/BufferedImage bufferedImage = QrCodeUtil.generate(//content, //二維碼內(nèi)容config);try {//以JPEG格式向客戶(hù)端發(fā)送ServletOutputStream os = response.getOutputStream();ImageIO.write(bufferedImage, "PNG",os);os.flush();os.close();} catch (IOException e) {e.printStackTrace();}}controller
?
?
postman測(cè)試
?如果打成jar需要這樣讀取file
InputStream initialStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(logourl); byte[] buffer = new byte[initialStream.available()]; initialStream.read(buffer); File file = new File("src/main/resources/targetFile.tmp");總結(jié)
以上是生活随笔為你收集整理的Hutool生成图片二维码 输出到前端的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: torch DQN算法
- 下一篇: linux内核驱动使用hugepages