亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺談java如何生成分享海報(bào)工具類

 更新時(shí)間:2021年04月08日 11:54:38   作者:田三水  
這篇文章主要介紹了淺談java如何生成分享海報(bào)工具類,想了解分享海報(bào)知識(shí)的同學(xué)不要錯(cuò)過哦

# 前言 例如:生成分享海報(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)文章!

相關(guān)文章

  • 老生常談java中的數(shù)組初始化

    老生常談java中的數(shù)組初始化

    下面小編就為大家?guī)?lái)一篇老生常談java中的數(shù)組初始化。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2017-04-04
  • 高效的java版排列組合算法

    高效的java版排列組合算法

    這篇文章主要為大家詳細(xì)介紹了高效的java版排列組合算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • MyBatis動(dòng)態(tài)SQL特性詳解

    MyBatis動(dòng)態(tài)SQL特性詳解

    動(dòng)態(tài)SQL可以省略很多拼接SQL的步驟,使用類似于JSTL方式,下面這篇文章主要給大家介紹了關(guān)于Mybatis動(dòng)態(tài)SQL特性的相關(guān)資料,文字通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • java高并發(fā)之線程組詳解

    java高并發(fā)之線程組詳解

    這篇文章主要介紹了java高并發(fā)之線程組,Java提供了ThreadGroup類來(lái)控制一個(gè)線程組,一個(gè)線程組可以通過線程對(duì)象來(lái)創(chuàng)建,也可以由其他線程組來(lái)創(chuàng)建,生成一個(gè)樹形結(jié)構(gòu)的線程,需要的朋友可以參考下
    2021-10-10
  • 使用System.exit()來(lái)優(yōu)雅地終止SpringBoot項(xiàng)目的代碼示例

    使用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-08
  • idea左下角的Git(Version Control)中顯示Local Changes窗口方式

    idea左下角的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-10
  • Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫(kù)

    Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫(kù)

    這篇文章主要為大家詳細(xì)介紹了Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 教你用JAVA寫文本編輯器(四)

    教你用JAVA寫文本編輯器(四)

    這篇文章主要給大家介紹了關(guān)于用JAVA寫文本編輯器的相關(guān)資料,通過這篇文章你可以完整的知道利用JAVA寫文本編輯器的完整過程,需要的朋友可以參考下
    2021-11-11
  • Java?泛型考古?泛型擦除?包裝類詳細(xì)解析

    Java?泛型考古?泛型擦除?包裝類詳細(xì)解析

    泛型是在Java?SE?1.5引入的的新特性,本質(zhì)是參數(shù)化類型,也就是說(shuō)所操作的數(shù)據(jù)類型被指定為一個(gè)參數(shù)。這種參數(shù)類型可以用在類、接口和方法的創(chuàng)建中,分別稱為泛型類、泛型接口、泛型方法,本篇我們一起來(lái)學(xué)習(xí)泛型考古、泛型擦除、包裝類
    2022-03-03
  • Mybatis 中的<![CDATA[ ]]>淺析

    Mybatis 中的<![CDATA[ ]]>淺析

    本文給大家解析使用<![CDATA[ ]]>解決xml文件不被轉(zhuǎn)義的問題, 對(duì)mybatis 中的<![CDATA[ ]]>相關(guān)知識(shí)感興趣的朋友一起看看吧
    2017-09-09

最新評(píng)論