淺談java如何生成分享海報(bào)工具類
# 前言 例如:生成分享海報(bào),比如注冊(cè)掃二維碼登錄.分享商品海報(bào)等!本博文是基于springboot工程得!
一、使用步驟
1.導(dǎo)入pom依賴和上傳圖片到工程
代碼如下(示例):在自己得通用工具類模塊中導(dǎo)入坐標(biāo)!(這需要根據(jù)自己得工程來(lái))
<!--谷歌圖片壓縮--> <dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> </dependency> <!--谷歌圖片壓縮--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> <!--生成二維碼--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-extra</artifactId> <version>5.4.3</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> <!--生成二維碼-->
代碼如下(示例):
這里是要生成海報(bào)的模板和二維碼的logo
2.創(chuàng)建生成接口
代碼如下(示例):
@LoginUser:是自定義注解獲取jwt的用戶id(根據(jù)自己的需求和工程來(lái))
@NoLogin:是項(xiàng)目種的白名單,不需要攜帶token的注解
/** * 生成用戶的邀請(qǐng)二維碼 * * @param userId 用戶id */ @GetMapping("qRCode") @NoLogin public Object qRCode(@LoginUser Integer userId) { //判斷用戶id是否為空 if (userId == null) { return ResponseUtil.fail("請(qǐng)選擇用戶"); } //獲取生成海報(bào)的圖片路徑 String filePath = wxUserService.qRCode(userId); return ResponseUtil.ok(filePath); }
3.創(chuàng)建service層
代碼如下(示例):
這是一個(gè)接口!需要自己實(shí)現(xiàn)該接口!實(shí)現(xiàn)接口代碼在下面!
/** * 根據(jù)用戶的邀請(qǐng)碼生成分享海報(bào) * * @param userId * @return */ String qRCode(Integer userId);
代碼如下(示例):
上面接口的實(shí)現(xiàn)類
/** * 根據(jù)用戶的邀請(qǐng)碼生成分享海報(bào) * * @param userId * @return */ @Override public String qRCode(Integer userId) { try { // 根據(jù)用戶id查詢驗(yàn)證碼 UserInfo userInfo = userService.selectById(userId); //判斷是否庫(kù)是否存在海報(bào)地址 if (!StringUtils.isEmpty(userInfo.getPoster())) { return userInfo.getPoster(); } //要生成海報(bào)的模板(一般在springboot工程的 resources下 我的工程路徑:templates/poster/xjcq.png 可以改成自己工程需要的路徑) File hbPath = ResourceUtils.getFile("classpath:templates/poster/xjcq.png"); //要生成二維碼的logo(一般在springboot工程的 resources下 我的工程路徑:templates/poster/xjcqLogo.png 可以改成自己工程需要的路徑) File logoPath = ResourceUtils.getFile("classpath:templates/poster/xjcqLogo.png"); // 獲取上傳后的路徑 String filePath = imageUtil.drawString(qRCodeInviteScheme + userInfo.getInviteCode(), userInfo.getInviteCode(), userInfo.getInviteCode(), hbPath, logoPath); //File轉(zhuǎn)MultipartFile(因?yàn)槲覀兊膐ss是轉(zhuǎn)MultipartFile) File file = new File(filePath); InputStream inputStream = new FileInputStream(file); MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); //上轉(zhuǎn)至阿里云 String s = storageUtil.uploadOssFile(multipartFile); //更改數(shù)據(jù)庫(kù) UserInfo updateUserInfo = new UserInfo(); updateUserInfo.setId(userInfo.getId()); updateUserInfo.setPoster(s); userService.updateById(updateUserInfo); return updateUserInfo.getPoster(); } catch (FileNotFoundException e) { log.error("文件找不到:{}", e); } catch (IOException e) { log.error("io異常:{}", e); } return null; }
4.生成海報(bào)的工具類
代碼如下(示例):
wordPath類的代碼,可以參考一下java獲取yml配置文件內(nèi)容
package com.legend.core.config; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * 獲取全局配置path * @author admin */ @Component @Data public class WordPath { //生成電子合同的路徑 @Value("${word.path}") private String wordPath; //生成海報(bào)的路徑 @Value("${poster.path}") private String posterPath; }
代碼如下(示例):
wordPath: 這個(gè)是我把要生成畫報(bào)地址的路徑配置到了yml中了,因?yàn)闇y(cè)試的使用用的是winodows,上了生產(chǎn)就用linux服務(wù)器了。所以配置到了yml中了
package com.legend.core.util; import cn.hutool.extra.qrcode.QrCodeUtil; import cn.hutool.extra.qrcode.QrConfig; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.legend.core.config.WordPath; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; /** * 生成分享好友 * * @author 生成分享好友 */ @Service @Slf4j public class ImageUtil { //我把生成海報(bào)地址的路徑配置到了springboot的yml配置文件中了 @Resource private WordPath wordPath; /** * 生成海報(bào) * * @param content 二維碼內(nèi)容 * @param written 文字內(nèi)容 * @param filePath 保存文件 例:1.png (d:/1.png) * @param hbPath 海報(bào)圖片地址 例:1.png (d:/1.png) * @param logoPath 二維碼logo * @return * @author Uncle * @Description 在一張背景圖上添加二維碼 * @Date 2020-09-28 23:59 */ public String drawString(String content, String written, String filePath, File hbPath, File logoPath) { try { BufferedImage image = addWater(content, hbPath, logoPath); Graphics2D gd = image.createGraphics(); // 3、設(shè)置對(duì)線段的鋸齒狀邊緣處理 gd.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); // 5、設(shè)置水印文字顏色 gd.setColor(Color.darkGray); // 6、設(shè)置水印文字Font gd.setFont(new Font("蘋方", Font.PLAIN, 32)); // 8、第一參數(shù)->設(shè)置的內(nèi)容,后面兩個(gè)參數(shù)->文字在圖片上的坐標(biāo)位置(x,y) gd.drawString(written, 440, 1122); gd.dispose(); ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream imOut = ImageIO.createImageOutputStream(bs); ImageIO.write(image, "png", imOut); InputStream inputStream = new ByteArrayInputStream(bs.toByteArray()); // 獲取yml海報(bào)的配置 String file = wordPath.getPosterPath() + filePath + ".png"; if (!new File(wordPath.getPosterPath()).exists()) { new File(wordPath.getPosterPath()).mkdirs(); } OutputStream outStream = new FileOutputStream(file); IOUtils.copy(inputStream, outStream); inputStream.close(); outStream.close(); // 返回文件地址 return file; } catch (Exception e) { log.error("海報(bào)生成失敗:", e); } return null; } /*** * 在一張背景圖上添加二維碼 */ public BufferedImage addWater(String content, File hbPath, File logoPath) throws Exception { // 讀取原圖片信息 //得到文件 //File file = new File(hbPath); //文件轉(zhuǎn)化為圖片 Image srcImg = ImageIO.read(hbPath); //獲取圖片的寬 int srcImgWidth = srcImg.getWidth(null); //獲取圖片的高 int srcImgHeight = srcImg.getHeight(null); // 加水印 BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g = bufImg.createGraphics(); g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null); //使用工具類生成二維碼 Image image = createQrCode(content, 230, 230, logoPath); //將小圖片繪到大圖片上,500,300 .表示你的小圖片在大圖片上的位置。 g.drawImage(image, 25, 1070, null); //設(shè)置顏色。 g.setColor(Color.WHITE); g.dispose(); return bufImg; } private BufferedImage createQrCode(String content, int width, int height, File logoPath) throws IOException { QrConfig config = new QrConfig(width, height); if (logoPath != null) { Image image = ImageIO.read(new FileInputStream(logoPath)); config.setImg(image); } config.setErrorCorrection(ErrorCorrectionLevel.H); return QrCodeUtil.generate( content, config); } public InputStream resourceLoader(String fileFullPath) throws IOException { ResourceLoader resourceLoader = new DefaultResourceLoader(); return resourceLoader.getResource(fileFullPath).getInputStream(); } }
總結(jié)
提示:這里對(duì)文章進(jìn)行總結(jié):
例如:以上就是今天要講的內(nèi)容,本文僅僅簡(jiǎn)單介紹了如何生成分享海報(bào)。例如:以上就是今天要講的內(nèi)容,本文僅僅簡(jiǎn)單介紹了如何生成分享海報(bào)。
以上就是java 生成分享海報(bào)工具類的詳細(xì)內(nèi)容,更多關(guān)于java 生成分享海報(bào)工具類的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Java中利用Alibaba開源技術(shù)EasyExcel來(lái)操作Excel表的示例代碼
- Java使用easyExcel導(dǎo)出excel數(shù)據(jù)案例
- 深入淺析Java常用的格式化Json工具類
- Java服務(wù)器主機(jī)信息監(jiān)控工具類的示例代碼
- Java基礎(chǔ)之顏色工具類(超詳細(xì)注釋)
- 教你如何使用Java多線程編程LockSupport工具類
- Java常用工具類庫(kù)——Hutool的使用簡(jiǎn)介
- java中封裝JDBC工具類的實(shí)例分析
- Java JDBC自定義封裝工具類的步驟和完整代碼
- Java8優(yōu)雅的字符串拼接工具類StringJoiner實(shí)例代碼
- Java應(yīng)用EasyExcel工具類
相關(guān)文章
使用System.exit()來(lái)優(yōu)雅地終止SpringBoot項(xiàng)目的代碼示例
System.exit() 方法是 Java 中用于退出程序的方法,它接受一個(gè)整數(shù)參數(shù),通常被用來(lái)指示程序的退出狀態(tài),本文給大家介紹了如何使用System.exit()來(lái)優(yōu)雅地終止SpringBoot項(xiàng)目,需要的朋友可以參考下2024-08-08idea左下角的Git(Version Control)中顯示Local Changes窗口方式
在IDEA中,通過使用快捷鍵Alt+9(Windows)或Cmd+9(Mac)可以快速打開LocalChanges窗口,查看當(dāng)前Git倉(cāng)庫(kù)的本地變更,若此方法不可用,可嘗試進(jìn)入settings,點(diǎn)擊VersionControl,選擇Commit,并取消Use interface的勾選2024-10-10Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08