自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java如何實(shí)現(xiàn)長圖文生成

開發(fā) 后端
很久很久以前,就覺得微博的長圖文實(shí)現(xiàn)得非常有意思,將排版直接以最終的圖片輸出,收藏查看分享都很方便,現(xiàn)在則自己動(dòng)手實(shí)現(xiàn)一個(gè)簡單版本的,實(shí)現(xiàn)一個(gè)用于生成微博長圖文樣式的包裝類。

[[200733]]

長圖文生成

很久很久以前,就覺得微博的長圖文實(shí)現(xiàn)得非常有意思,將排版直接以最終的圖片輸出,收藏查看分享都很方便,現(xiàn)在則自己動(dòng)手實(shí)現(xiàn)一個(gè)簡單版本的

目標(biāo)

首先定義下我們預(yù)期達(dá)到的目標(biāo):根據(jù)文字 + 圖片生成長圖文

目標(biāo)拆解

  • 支持大段文字生成圖片
  • 支持插入圖片
  • 支持上下左右邊距設(shè)置
  • 支持字體選擇
  • 支持字體顏色
  • 支持左對(duì)齊,居中,右對(duì)齊

預(yù)期結(jié)果

我們將通過spring-boot搭建一個(gè)生成長圖文的http接口,通過傳入?yún)?shù)來指定各種配置信息,下面是一個(gè)最終調(diào)用的示意圖

 

 

設(shè)計(jì)&實(shí)現(xiàn)

長圖文的生成,采用awt進(jìn)行文字繪制和圖片繪制

1. 參數(shù)選項(xiàng) ImgCreateOptions

根據(jù)我們的預(yù)期目標(biāo),設(shè)定配置參數(shù),基本上會(huì)包含以下參數(shù)

  1. @Getter 
  2. @Setter 
  3. @ToString 
  4. public class ImgCreateOptions { 
  5.  
  6.     /** 
  7.      * 繪制的背景圖 
  8.      */ 
  9.     private BufferedImage bgImg; 
  10.  
  11.  
  12.     /** 
  13.      * 生成圖片的寬 
  14.      */ 
  15.     private Integer imgW; 
  16.  
  17.  
  18.     private Font font = new Font("宋體", Font.PLAIN, 18); 
  19.  
  20.     /** 
  21.      * 字體色 
  22.      */ 
  23.     private Color fontColor = Color.BLACK; 
  24.  
  25.  
  26.     /** 
  27.      * 兩邊邊距 
  28.      */ 
  29.     private int leftPadding; 
  30.  
  31.     /** 
  32.      * 上邊距 
  33.      */ 
  34.     private int topPadding; 
  35.  
  36.     /** 
  37.      * 底邊距 
  38.      */ 
  39.     private int bottomPadding; 
  40.  
  41.     /** 
  42.      * 行距 
  43.      */ 
  44.     private int linePadding; 
  45.  
  46.  
  47.     private AlignStyle alignStyle; 
  48.  
  49.     /** 
  50.      * 對(duì)齊方式 
  51.      */ 
  52.     public enum AlignStyle { 
  53.         LEFT
  54.         CENTER, 
  55.         RIGHT
  56.  
  57.  
  58.         private static Map<String, AlignStyle> map = new HashMap<>(); 
  59.  
  60.         static { 
  61.             for(AlignStyle style: AlignStyle.values()) { 
  62.                 map.put(style.name(), style); 
  63.             } 
  64.         } 
  65.  
  66.  
  67.         public static AlignStyle getStyle(String name) { 
  68.             name = name.toUpperCase(); 
  69.             if (map.containsKey(name)) { 
  70.                 return map.get(name); 
  71.             } 
  72.  
  73.             return LEFT
  74.         } 
  75.     } 

 

2. 封裝類 ImageCreateWrapper

封裝配置參數(shù)的設(shè)置,繪制文本,繪制圖片的操作方式,輸出樣式等接口

  1. public class ImgCreateWrapper { 
  2.  
  3.  
  4.     public static Builder build() { 
  5.         return new Builder(); 
  6.     } 
  7.  
  8.  
  9.     public static class Builder { 
  10.         /** 
  11.          * 生成的圖片創(chuàng)建參數(shù) 
  12.          */ 
  13.         private ImgCreateOptions options = new ImgCreateOptions(); 
  14.  
  15.  
  16.         /** 
  17.          * 輸出的結(jié)果 
  18.          */ 
  19.         private BufferedImage result; 
  20.  
  21.  
  22.         private final int addH = 1000; 
  23.  
  24.  
  25.         /** 
  26.          * 實(shí)際填充的內(nèi)容高度 
  27.          */ 
  28.         private int contentH; 
  29.  
  30.  
  31.         private Color bgColor; 
  32.  
  33.         public Builder setBgColor(int color) { 
  34.             return setBgColor(ColorUtil.int2color(color)); 
  35.         } 
  36.  
  37.         /** 
  38.          * 設(shè)置背景圖 
  39.          * 
  40.          * @param bgColor 
  41.          * @return 
  42.          */ 
  43.         public Builder setBgColor(Color bgColor) { 
  44.             this.bgColor = bgColor; 
  45.             return this; 
  46.         } 
  47.  
  48.  
  49.         public Builder setBgImg(BufferedImage bgImg) { 
  50.             options.setBgImg(bgImg); 
  51.             return this; 
  52.         } 
  53.  
  54.  
  55.         public Builder setImgW(int w) { 
  56.             options.setImgW(w); 
  57.             return this; 
  58.         } 
  59.  
  60.         public Builder setFont(Font font) { 
  61.             options.setFont(font); 
  62.             return this; 
  63.         } 
  64.  
  65.         public Builder setFontName(String fontName) { 
  66.             Font font = options.getFont(); 
  67.             options.setFont(new Font(fontName, font.getStyle(), font.getSize())); 
  68.             return this; 
  69.         } 
  70.  
  71.  
  72.         public Builder setFontColor(int fontColor) { 
  73.             return setFontColor(ColorUtil.int2color(fontColor)); 
  74.         } 
  75.  
  76.         public Builder setFontColor(Color fontColor) { 
  77.             options.setFontColor(fontColor); 
  78.             return this; 
  79.         } 
  80.  
  81.         public Builder setFontSize(Integer fontSize) { 
  82.             Font font = options.getFont(); 
  83.             options.setFont(new Font(font.getName(), font.getStyle(), fontSize)); 
  84.             return this; 
  85.         } 
  86.  
  87.         public Builder setLeftPadding(int leftPadding) { 
  88.             options.setLeftPadding(leftPadding); 
  89.             return this; 
  90.         } 
  91.  
  92.         public Builder setTopPadding(int topPadding) { 
  93.             options.setTopPadding(topPadding); 
  94.             contentH = topPadding; 
  95.             return this; 
  96.         } 
  97.  
  98.         public Builder setBottomPadding(int bottomPadding) { 
  99.             options.setBottomPadding(bottomPadding); 
  100.             return this; 
  101.         } 
  102.  
  103.         public Builder setLinePadding(int linePadding) { 
  104.             options.setLinePadding(linePadding); 
  105.             return this; 
  106.         } 
  107.  
  108.         public Builder setAlignStyle(String style) { 
  109.             return setAlignStyle(ImgCreateOptions.AlignStyle.getStyle(style)); 
  110.         } 
  111.  
  112.         public Builder setAlignStyle(ImgCreateOptions.AlignStyle alignStyle) { 
  113.             options.setAlignStyle(alignStyle); 
  114.             return this; 
  115.         } 
  116.  
  117.  
  118.         public Builder drawContent(String content) { 
  119.             // xxx 
  120.             return this; 
  121.         } 
  122.  
  123.  
  124.         public Builder drawImage(String img) { 
  125.             BufferedImage bfImg; 
  126.             try { 
  127.                  bfImg = ImageUtil.getImageByPath(img); 
  128.             } catch (IOException e) { 
  129.                 log.error("load draw img error! img: {}, e:{}", img, e); 
  130.                 throw new IllegalStateException("load draw img error! img: " + img, e); 
  131.             } 
  132.  
  133.             return drawImage(bfImg); 
  134.         } 
  135.  
  136.  
  137.         public Builder drawImage(BufferedImage bufferedImage) { 
  138.  
  139.            // xxx 
  140.            return this; 
  141.         } 
  142.  
  143.  
  144.         public BufferedImage asImage() { 
  145.             int realH = contentH + options.getBottomPadding(); 
  146.  
  147.             BufferedImage bf = new BufferedImage(options.getImgW(), realH, BufferedImage.TYPE_INT_ARGB); 
  148.             Graphics2D g2d = bf.createGraphics(); 
  149.  
  150.             if (options.getBgImg() == null) { 
  151.                 g2d.setColor(bgColor == null ? Color.WHITE : bgColor); 
  152.                 g2d.fillRect(0, 0, options.getImgW(), realH); 
  153.             } else { 
  154.                 g2d.drawImage(options.getBgImg(), 0, 0, options.getImgW(), realH, null); 
  155.             } 
  156.  
  157.             g2d.drawImage(result, 0, 0, null); 
  158.             g2d.dispose(); 
  159.             return bf; 
  160.         } 
  161.  
  162.  
  163.         public String asString() throws IOException { 
  164.             BufferedImage img = asImage(); 
  165.             return Base64Util.encode(img, "png"); 
  166.         } 

 

上面具體的文本和圖片繪制實(shí)現(xiàn)沒有,后面詳細(xì)講解,這里主要關(guān)注的是一個(gè)參數(shù) contentH, 表示實(shí)際繪制的內(nèi)容高度(包括上邊距),因此最終生成圖片的高度應(yīng)該是

int realH = contentH + options.getBottomPadding();

其次簡單說一下上面的圖片輸出方法:com.hust.hui.quickmedia.common.image.ImgCreateWrapper.Builder#asImage

  • 計(jì)算最終生成圖片的高度(寬度由輸入?yún)?shù)指定)
  • 繪制背景(如果沒有背景圖片,則用純色填充)
  • 繪制實(shí)體內(nèi)容(即繪制的文本,圖片)

3. 內(nèi)容填充 GraphicUtil

具體的內(nèi)容填充,區(qū)分為文本繪制和圖片繪制

設(shè)計(jì)

  • 考慮到在填充的過程中,可以自由設(shè)置字體,顏色等,所以在我們的繪制方法中,直接實(shí)現(xiàn)掉內(nèi)容的繪制填充,即 drawXXX 方法真正的實(shí)現(xiàn)了內(nèi)容填充,執(zhí)行完之后,內(nèi)容已經(jīng)填充到畫布上了
  • 圖片繪制,考慮到圖片本身大小和最終結(jié)果的大小可能有沖突,采用下面的規(guī)則
    • 繪制圖片寬度 <=(指定生成圖片寬 - 邊距),全部填充
    • 繪制圖片寬度 >(指定生成圖片寬 - 邊距),等比例縮放繪制圖片
  • 文本繪制,換行的問題
    • 每一行允許的文本長度有限,超過時(shí),需要自動(dòng)換行處理

文本繪制

考慮基本的文本繪制,流程如下

  • 創(chuàng)建BufferImage對(duì)象
  • 獲取Graphic2d對(duì)象,操作繪制
  • 設(shè)置基本配置信息
  • 文本按換行進(jìn)行拆分為字符串?dāng)?shù)組, 循環(huán)繪制單行內(nèi)容
    • 計(jì)算當(dāng)行字符串,實(shí)際繪制的行數(shù),然后進(jìn)行拆分
    • 依次繪制文本(需要注意y坐標(biāo)的變化)

下面是具體的實(shí)現(xiàn)

  1. public static int drawContent(Graphics2D g2d, 
  2.                                   String content, 
  3.                                   int y, 
  4.                                   ImgCreateOptions options) { 
  5.  
  6.     int w = options.getImgW(); 
  7.     int leftPadding = options.getLeftPadding(); 
  8.     int linePadding = options.getLinePadding(); 
  9.     Font font = options.getFont(); 
  10.  
  11.  
  12.     // 一行容納的字符個(gè)數(shù) 
  13.     int lineNum = (int) Math.floor((w - (leftPadding << 1)) / (double) font.getSize()); 
  14.  
  15.     // 對(duì)長串字符串進(jìn)行分割成多行進(jìn)行繪制 
  16.     String[] strs = splitStr(content, lineNum); 
  17.  
  18.     g2d.setFont(font); 
  19.  
  20.     g2d.setColor(options.getFontColor()); 
  21.     int index = 0; 
  22.     int x; 
  23.     for (String tmp : strs) { 
  24.         x = calOffsetX(leftPadding, w, tmp.length() * font.getSize(), options.getAlignStyle()); 
  25.         g2d.drawString(tmp, x, y + (linePadding + font.getSize()) * index); 
  26.         index++; 
  27.     } 
  28.  
  29.  
  30.     return y + (linePadding + font.getSize()) * (index); 
  31.  
  32. /** 
  33.  * 計(jì)算不同對(duì)其方式時(shí),對(duì)應(yīng)的x坐標(biāo) 
  34.  * 
  35.  * @param padding 左右邊距 
  36.  * @param width   圖片總寬 
  37.  * @param strSize 字符串總長 
  38.  * @param style   對(duì)其方式 
  39.  * @return 返回計(jì)算后的x坐標(biāo) 
  40.  */ 
  41. private static int calOffsetX(int padding, 
  42.                               int width, 
  43.                               int strSize, 
  44.                               ImgCreateOptions.AlignStyle style) { 
  45.     if (style == ImgCreateOptions.AlignStyle.LEFT) { 
  46.         return padding; 
  47.     } else if (style == ImgCreateOptions.AlignStyle.RIGHT) { 
  48.         return width - padding - strSize; 
  49.     } else { 
  50.         return (width - strSize) >> 1; 
  51.     } 
  52.  
  53.  
  54. /** 
  55.  * 按照長度對(duì)字符串進(jìn)行分割 
  56.  * <p> 
  57.  * fixme 包含emoj表情時(shí),兼容一把 
  58.  * 
  59.  * @param str      原始字符串 
  60.  * @param splitLen 分割的長度 
  61.  * @return 
  62.  */ 
  63. public static String[] splitStr(String str, int splitLen) { 
  64.     int len = str.length(); 
  65.     int size = (int) Math.ceil(len / (float) splitLen); 
  66.  
  67.     String[] ans = new String[size]; 
  68.     int start = 0; 
  69.     int end = splitLen; 
  70.     for (int i = 0; i < size; i++) { 
  71.         ans[i] = str.substring(start, end > len ? len : end); 
  72.         start = end
  73.         end += splitLen; 
  74.     } 
  75.  
  76.     return ans; 

 

上面的實(shí)現(xiàn)比較清晰了,圖片的繪制則更加簡單

圖片繪制

只需要重新計(jì)算下待繪制圖片的寬高即可,具體實(shí)現(xiàn)如下

  1. /** 
  2.  * 在原圖上繪制圖片 
  3.  * 
  4.  * @param source  原圖 
  5.  * @param dest    待繪制圖片 
  6.  * @param y       待繪制的y坐標(biāo) 
  7.  * @param options 
  8.  * @return 繪制圖片的高度 
  9.  */ 
  10. public static int drawImage(BufferedImage source, 
  11.                             BufferedImage dest, 
  12.                             int y, 
  13.                             ImgCreateOptions options) { 
  14.     Graphics2D g2d = getG2d(source); 
  15.     int w = Math.min(dest.getWidth(), options.getImgW() - (options.getLeftPadding() << 1)); 
  16.     int h = w * dest.getHeight() / dest.getWidth(); 
  17.  
  18.     int x = calOffsetX(options.getLeftPadding(), 
  19.             options.getImgW(), w, options.getAlignStyle()); 
  20.  
  21.     // 繪制圖片 
  22.     g2d.drawImage(dest, 
  23.             x, 
  24.             y + options.getLinePadding(), 
  25.             w, 
  26.             h, 
  27.             null); 
  28.     g2d.dispose(); 
  29.  
  30.     return h; 
  31.  
  32. public static Graphics2D getG2d(BufferedImage bf) { 
  33.         Graphics2D g2d = bf.createGraphics(); 
  34.  
  35.     g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 
  36.     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
  37.     g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); 
  38.     g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); 
  39.     g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 
  40.     g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
  41.     g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
  42.     g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); 
  43.  
  44.     return g2d; 

 

4. 內(nèi)容渲染

前面只是給出了單塊內(nèi)容(如一段文字,一張圖片)的渲染,存在一些問題

  • 繪制的內(nèi)容超過畫布的高度如何處理
  • 文本繪制要求傳入的文本沒有換行符,否則換行不生效
  • 交叉繪制的場景,如何重新計(jì)算y坐標(biāo)

解決這些問題則是在 ImgCreateWrapper 的具體繪制中進(jìn)行了實(shí)現(xiàn),先看文本的繪制

  • 根據(jù)換行符對(duì)字符串進(jìn)行拆分
  • 計(jì)算繪制內(nèi)容最終轉(zhuǎn)換為圖片時(shí),所占用的高度
  • 重新生成畫布 BufferedImage result
    • 如果result為空,則直接生成
    • 如果最終生成的高度,超過已有畫布的高度,則生成一個(gè)更高的畫布,并將原來的內(nèi)容繪制上去
  • 迭代繪制單行內(nèi)容
  1. public Builder drawContent(String content) { 
  2.     String[] strs = StringUtils.split(content, "\n"); 
  3.     if (strs.length == 0) { // empty line 
  4.         strs = new String[1]; 
  5.         strs[0] = " "
  6.     } 
  7.  
  8.     int fontSize = options.getFont().getSize(); 
  9.     int lineNum = calLineNum(strs, options.getImgW(), options.getLeftPadding(), fontSize); 
  10.     // 填寫內(nèi)容需要占用的高度 
  11.     int height = lineNum * (fontSize + options.getLinePadding()); 
  12.  
  13.     if (result == null) { 
  14.         result = GraphicUtil.createImg(options.getImgW(), 
  15.                 Math.max(height + options.getTopPadding() + options.getBottomPadding(), BASE_ADD_H), 
  16.                 null); 
  17.     } else if (result.getHeight() < contentH + height + options.getBottomPadding()) { 
  18.         // 超過原來圖片高度的上限, 則需要擴(kuò)充圖片長度 
  19.         result = GraphicUtil.createImg(options.getImgW(), 
  20.                 result.getHeight() + Math.max(height + options.getBottomPadding(), BASE_ADD_H), 
  21.                 result); 
  22.     } 
  23.  
  24.  
  25.     // 繪制文字 
  26.     Graphics2D g2d = GraphicUtil.getG2d(result); 
  27.     int index = 0; 
  28.     for (String str : strs) { 
  29.         GraphicUtil.drawContent(g2d, str, 
  30.                 contentH + (fontSize + options.getLinePadding()) * (++index
  31.                 , options); 
  32.     } 
  33.     g2d.dispose(); 
  34.  
  35.     contentH += height; 
  36.     return this; 
  37.  
  38.  
  39. /** 
  40.  * 計(jì)算總行數(shù) 
  41.  * 
  42.  * @param strs     字符串列表 
  43.  * @param w        生成圖片的寬 
  44.  * @param padding  渲染內(nèi)容的左右邊距 
  45.  * @param fontSize 字體大小 
  46.  * @return 
  47.  */ 
  48. private int calLineNum(String[] strs, int w, int padding, int fontSize) { 
  49.     // 每行的字符數(shù) 
  50.     double lineFontLen = Math.floor((w - (padding << 1)) / (double) fontSize); 
  51.  
  52.  
  53.     int totalLine = 0; 
  54.     for (String str : strs) { 
  55.         totalLine += Math.ceil(str.length() / lineFontLen); 
  56.     } 
  57.  
  58.     return totalLine; 

 

上面需要注意的是畫布的生成規(guī)則,特別是高度超過上限之后,重新計(jì)算圖片高度時(shí),需要額外注意新增的高度,應(yīng)該為基本的增量與(繪制內(nèi)容高度+下邊距)的較大值

  1. int realAddH = Math.max(bufferedImage.getHeight() + options.getBottomPadding() + options.getTopPadding(), BASE_ADD_H) 

重新生成畫布實(shí)現(xiàn) com.hust.hui.quickmedia.common.util.GraphicUtil#createImg

  1. public static BufferedImage createImg(int w, int h, BufferedImage img) { 
  2.     BufferedImage bf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 
  3.     Graphics2D g2d = bf.createGraphics(); 
  4.  
  5.     if (img != null) { 
  6.         g2d.setComposite(AlphaComposite.Src); 
  7.         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
  8.         g2d.drawImage(img, 0, 0, null); 
  9.     } 
  10.     g2d.dispose(); 
  11.     return bf; 

 

上面理解之后,繪制圖片就比較簡單了,基本上行沒什么差別

  1. public Builder drawImage(String img) { 
  2.     BufferedImage bfImg; 
  3.     try { 
  4.         bfImg = ImageUtil.getImageByPath(img); 
  5.     } catch (IOException e) { 
  6.         log.error("load draw img error! img: {}, e:{}", img, e); 
  7.         throw new IllegalStateException("load draw img error! img: " + img, e); 
  8.     } 
  9.  
  10.     return drawImage(bfImg); 
  11.  
  12.  
  13. public Builder drawImage(BufferedImage bufferedImage) { 
  14.  
  15.     if (result == null) { 
  16.         result = GraphicUtil.createImg(options.getImgW(), 
  17.                 Math.max(bufferedImage.getHeight() + options.getBottomPadding() + options.getTopPadding(), BASE_ADD_H), 
  18.                 null); 
  19.     } else if (result.getHeight() < contentH + bufferedImage.getHeight() + options.getBottomPadding()) { 
  20.         // 超過閥值 
  21.         result = GraphicUtil.createImg(options.getImgW(), 
  22.                 result.getHeight() + Math.max(bufferedImage.getHeight() + options.getBottomPadding() + options.getTopPadding(), BASE_ADD_H), 
  23.                 result); 
  24.     } 
  25.  
  26.     // 更新實(shí)際高度 
  27.     int h = GraphicUtil.drawImage(result, 
  28.             bufferedImage, 
  29.             contentH, 
  30.             options); 
  31.     contentH += h + options.getLinePadding(); 
  32.     return this; 

 

5. http接口

上面實(shí)現(xiàn)的生成圖片的公共方法,在 quick-media 工程中,利用spring-boot搭建了一個(gè)web服務(wù),提供了一個(gè)http接口,用于生成長圖文,最終的成果就是我們開頭的那個(gè)gif圖的效果,相關(guān)代碼就沒啥好說的,有興趣的可以直接查看工程源碼,鏈接看最后

測(cè)試驗(yàn)證

上面基本上完成了我們預(yù)期的目標(biāo),接下來則是進(jìn)行驗(yàn)證,測(cè)試代碼比較簡單,先準(zhǔn)備一段文本,這里拉了一首詩

招魂酹翁賓旸

鄭起

君之在世帝敕下,君之謝世帝敕回。

魂之為變性原返,氣之為物情本開。

於戲龍兮鳳兮神氣盛,噫嘻鬼兮歸兮大塊埃。

身可朽名不可朽,骨可灰神不可灰。

采石捉月李白非醉,耒陽避水子美非災(zāi)。

長孫王吉命不夭,玉川老子詩不徘。

新城羅隱在奇特,錢塘潘閬終崔嵬。

陰兮魄兮曷往,陽兮魄兮曷來。

君其歸來,故交寥落更散漫。

君來歸來,帝城絢爛可徘徊。

君其歸來,東西南北不可去。

君其歸來。

春秋霜露令人哀。

花之明吾無與笑,葉之隕吾實(shí)若摧。

曉猿嘯吾聞淚墮,宵鶴立吾見心猜。

玉泉其清可鑒,西湖其甘可杯。

孤山暖梅香可嗅,花翁葬薦菊之隈。

君其歸來,可伴逋仙之梅,去此又奚之哉。

測(cè)試代碼

  1. @Test 
  2. public void testGenImg() throws IOException { 
  3.     int w = 400; 
  4.     int leftPadding = 10; 
  5.     int topPadding = 40; 
  6.     int bottomPadding = 40; 
  7.     int linePadding = 10; 
  8.     Font font = new Font("宋體", Font.PLAIN, 18); 
  9.  
  10.     ImgCreateWrapper.Builder build = ImgCreateWrapper.build() 
  11.             .setImgW(w) 
  12.             .setLeftPadding(leftPadding) 
  13.             .setTopPadding(topPadding) 
  14.             .setBottomPadding(bottomPadding) 
  15.             .setLinePadding(linePadding) 
  16.             .setFont(font) 
  17.             .setAlignStyle(ImgCreateOptions.AlignStyle.CENTER) 
  18. //                .setBgImg(ImageUtil.getImageByPath("qrbg.jpg")) 
  19.             .setBgColor(0xFFF7EED6) 
  20.             ; 
  21.  
  22.  
  23.     BufferedReader reader = FileReadUtil.createLineRead("text/poem.txt"); 
  24.     String line; 
  25.     int index = 0; 
  26.     while ((line = reader.readLine()) != null) { 
  27.         build.drawContent(line); 
  28.  
  29.         if (++index == 5) { 
  30.             build.drawImage(ImageUtil.getImageByPath("https://static.oschina.net/uploads/img/201708/12175633_sOfz.png")); 
  31.         } 
  32.  
  33.         if (index == 7) { 
  34.             build.setFontSize(25); 
  35.         } 
  36.  
  37.         if (index == 10) { 
  38.             build.setFontSize(20); 
  39.             build.setFontColor(Color.RED); 
  40.         } 
  41.     } 
  42.  
  43.     BufferedImage img = build.asImage(); 
  44.     String out = Base64Util.encode(img, "png"); 
  45.     System.out.println("<img src=\"data:image/png;base64," + out + "\" />"); 

 

輸出圖片

責(zé)任編輯:龐桂玉 來源: 六月依的博客
相關(guān)推薦

2023-03-13 15:56:00

模型框架

2017-10-12 15:34:17

2023-11-20 12:49:01

2024-06-14 16:24:42

2012-04-11 15:41:48

JavaNIO

2022-07-18 14:33:05

PythonPDF報(bào)告

2024-01-06 16:40:47

視頻模型

2021-02-26 12:37:39

WebSocketOkHttp連接

2013-03-15 10:57:13

AJAXDotNet

2024-05-10 07:58:03

2025-01-17 13:53:11

AI大模型檢測(cè)工具

2023-02-26 10:16:19

JavaPDF文檔

2015-10-26 15:48:51

安裝Ubuntu 15.1Linux

2020-09-15 10:45:06

PythonPyQt5Matplotlib

2024-02-19 07:58:01

OpenAI模型GPT

2021-04-25 06:12:19

Java內(nèi)存布局JVM

2024-01-08 13:49:00

2025-03-05 00:00:55

2024-07-04 10:13:18

2011-01-19 17:30:21

Postfix郵件投遞
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)