最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)圖片上插入文字并保存

 更新時間:2019年06月13日 08:38:03   作者:Rm-r  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖片上插入文字并保存,具有一定的參考價值,感興趣的小伙伴們可以參考一下

這兩天通過在網(wǎng)上查閱資料,了解了在圖片上插入文字并保存的功能,下面記錄一下。

工具類:PrintImage。

package com.learning.www.utils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.GlyphVector;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
 
import javax.imageio.ImageIO;
 
public class PrintImage {
 
  private Font    font   = new Font("黑體", Font.PLAIN, 25); // 添加字體的屬性設(shè)置
 
  private Graphics2D g    = null;
 
  private int    fontsize = 0;
 
  /**
   * 導(dǎo)入本地圖片到緩沖區(qū)
   */
  public BufferedImage loadImageLocal(String imgName) {
    try {
      return ImageIO.read(new File(imgName));
    } catch (IOException e) {
      System.out.println(e.getMessage());
    }
    return null;
  }
 
  /**
   * 導(dǎo)入網(wǎng)絡(luò)圖片到緩沖區(qū)
   */
  public BufferedImage loadImageUrl(String imgName) {
    try {
      URL url = new URL(imgName);
      return ImageIO.read(url);
    } catch (IOException e) {
      System.out.println(e.getMessage());
    }
    return null;
  }
 
  /**
   * 生成新圖片到本地
   */
  public void writeImageLocal(String newImage, BufferedImage img) {
    if (newImage != null && img != null) {
      try {
        File outputfile = new File(newImage);
        ImageIO.write(img, "jpg", outputfile);
      } catch (IOException e) {
        System.out.println(e.getMessage());
      }
    }
  }
 
  /**
   * 設(shè)定文字的字體等
   */
  public void setFont(Font font) {
    
    this.font = font;
  }
 
  /**
   * 修改圖片,返回修改后的圖片緩沖區(qū)(只輸出一行文本)
   */
  public BufferedImage modifyImage(BufferedImage img, Object content, int x, int y,Color color) {
    try {
      int w = img.getWidth();
      int h = img.getHeight();
      g = img.createGraphics();
      g.setBackground(Color.BLUE);
      
      
      //g.setColor(new Color(120, 120, 110));//設(shè)置字體顏色
      g.setColor(color);//設(shè)置字體顏色
      g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
      g.setStroke(new BasicStroke(3));
      if (this.font != null)
        g.setFont(this.font);
      if (content != null) {
        g.translate(w / 2, h / 2);
        //g.rotate(8 * Math.PI / 180);
        g.drawString(content.toString(), x, y);
      }
      g.dispose();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
 
    return img;
  }
 
  
  /**
   * 修改圖片,返回修改后的圖片緩沖區(qū)(只輸出一行文本)
   *
   * 時間:2007-10-8
   *
   * @param img
   * @return
   */
  public BufferedImage modifyImageYe(BufferedImage img) {
 
    try {
      int w = img.getWidth();
      int h = img.getHeight();
      g = img.createGraphics();
      g.setBackground(Color.WHITE);
      g.setColor(Color.blue);//設(shè)置字體顏色
      if (this.font != null)
        g.setFont(this.font);
      g.drawString("www.hi.baidu.com?xia_mingjian", w - 85, h - 5);
      g.dispose();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
 
    return img;
  }
 
  public BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d) {
 
    try {
      int w = b.getWidth();
      int h = b.getHeight();
      g = d.createGraphics();
      g.drawImage(b, 100, 10, w, h, null);
      g.dispose();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
 
    return d;
  }
  /***
   * 插入描邊的字體
   * @param img
   * @param content
   * @param w
   * @param h
   * @return
   */
  public BufferedImage modifyShapImg(BufferedImage img, String content, int w, int h) {
//    int w = img.getWidth();
//    int h = img.getHeight();
    g = img.createGraphics();
   
    //Font f = new Font("Courier New", Font.BOLD, 140);
    GlyphVector v = font.createGlyphVector(g.getFontMetrics(font).getFontRenderContext(), content);
    Shape shape = v.getOutline();
    if (content != null) {
      g.translate(w, h);
      //g.rotate(8 * Math.PI / 180);
      //g.drawString(content.toString(), x, y);
    }
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    g.setColor(new Color(0,90,160));
    g.fill(shape);
    g.setColor(new Color(248,248,255));
    g.setStroke(new BasicStroke(2));
    g.draw(shape);
    
   return img;
  }
 
}

插入多條的格式相同的文字:

package com.learning.www.utils;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
 
public class PrintJobToImg {
 
 public static void printJobToImg(PrintImage tt,BufferedImage d,String job1,String need1,String amount1,String salary1,int y) {
 
  
    if(null != job1 && !job1.equals("")) {
   need1 = "崗位職責(zé):"+need1;
   int strleth = need1.length()+5;
   int num = strleth/40;
   int subindex = 0;
   int j = 40;
   //y = -350;
   String[] s1 = new String[num+1];
   tt.setFont(new Font("黑體",Font.BOLD, 28));
   tt.modifyImage(d, "職位:"+job1, -50, y, new Color(0,191,255));
   tt.modifyImage(d, "人數(shù):"+amount1, -30+(30*(3+job1.length())), y, new Color(210,105,30));
   tt.modifyImage(d, "月薪:"+salary1, -10+(30*(6+job1.length()+amount1.length())), y, new Color(178,34,34));
   y = y+25;
   tt.setFont(new Font("黑體",Font.PLAIN, 24));
    if(num < 1 ) {
     System.out.println(num);
     tt.modifyImage(d, need1, -50, y+25,new Color(0,0,0));
    }else {
     for(int i = 0;i<num;i++) {
     s1[i] = need1.substring(subindex, j);
     tt.modifyImage(d, s1[i], -50, y,new Color(0,0,0));
     subindex=j;
     j+=40;
     y+=25;
     }
     if(strleth%40 != 0) {
     //System.out.println("不等于0");
     s1[num] = need1.substring(num * 40);
     tt.modifyImage(d, s1[num], -50, y,new Color(0,0,0));
     }
    }
   //tt.modifyImage(d, "崗位要求:"+need1, -50, y+25, new Color(0,0,0));
    }
 
 
 
 
 }
 
}

啟動類:

package com.learning.www;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
 
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
 
import com.learning.www.utils.PrintImage;
import com.learning.www.utils.PrintJobToImg;
 
@SpringBootApplication
@EnableAutoConfiguration
@EnableCaching
@MapperScan("com.learning.www.mapper")
public class LearningApplication {
 
 public static void main(String[] args) {
 SpringApplication.run(LearningApplication.class, args);
    PrintImage tt = new PrintImage();
    BufferedImage d = tt.loadImageLocal("D:\\test\\muban.jpg");
    String title = "撒大大是多少有限公司";
    int x = title.length() * 96;
    // 公司標(biāo)題 72號字體==96px
    tt.setFont(new Font("造字工房力黑(非商用)常規(guī)體", Font.BOLD, 76));
    //tt.modifyImage(d, title, (1920-x)/2-960, -420, new Color(65,105,225));
    //tt.modifyShapImg(d, title, (1920-x)/2-960, -420);
    tt.modifyShapImg(d, title, (1920-x)/2, 130);
    
    //公司簡介,限定字?jǐn)?shù)
    tt.setFont(new Font("黑體",Font.PLAIN, 30));
    String str = "功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"
    +"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"
    +"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"
    +"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存"+"功能:可以實(shí)現(xiàn)在圖片模板上寫內(nèi)容并保存";
    System.out.println(str.length());
    //計算字符串長度
    int strleth=str.length();
    //計算循環(huán)次數(shù)
    int num = strleth/20;
    //字符串截取第一位
    int subindex = 0;
    //字符串截取第二位
    int j = 20;
    //距離y軸的位置
    int y = -350;
    String[] s = new String[num+1];
    if(num < 1 ) {
    System.out.println(num);
    tt.modifyImage(d, str, -830, y,new Color(0,0,0));
    }else {
    for(int i = 0;i<num;i++) {
     s[i] = str.substring(subindex, j);
     tt.modifyImage(d, s[i], -830, y,new Color(0,0,0));
     subindex=j;
     j+=20;
     y+=35;
    }
    if(strleth%20 != 0) {
     //System.out.println("不等于0");
     s[num] = str.substring(num * 20);
     tt.modifyImage(d, s[num], -830, y,new Color(0,0,0));
    }
    }
    // 公司崗位6個
    String job1 = "普工";
    String amount1 = "3人";
    String salary1 = "4000元/月";
    String need1 = "吃苦耐勞,具有專業(yè)的技術(shù)能力。吃苦耐勞,具有專業(yè)的技術(shù)能力。吃苦耐勞,具有專業(yè)的技術(shù)能力。吃苦耐勞,具有專業(yè)的技術(shù)能力。吃苦耐勞,具有專業(yè)的技術(shù)能力。"
     + "吃苦耐勞,具有專業(yè)的技術(shù)能力。";
    y = -350;
    PrintJobToImg.printJobToImg(tt, d, job1, need1, amount1, salary1, y);
    PrintJobToImg.printJobToImg(tt, d, job1, need1, amount1, salary1, y+110);
    PrintJobToImg.printJobToImg(tt, d, job1, need1, amount1, salary1, y+220);
    PrintJobToImg.printJobToImg(tt, d, job1, need1, amount1, salary1, y+330);
    PrintJobToImg.printJobToImg(tt, d, job1, need1, amount1, salary1, y+440);
    PrintJobToImg.printJobToImg(tt, d, job1, need1, amount1, salary1, y+550);
//    tt.setFont(new Font("黑體",Font.PLAIN, 24));
//    if(null != job1 && !job1.equals("")) {
//   need1 = "崗位職責(zé):"+need1;
//   strleth = need1.length()+5;
//   num = strleth/40;
//   subindex = 0;
//   j = 40;
//   y = -350;
//   String[] s1 = new String[num+1];
//   tt.modifyImage(d, "職位:"+job1, -50, y, new Color(0,191,255));
//   tt.modifyImage(d, "人數(shù):"+amount1, -30+(30*(3+job1.length())), y, new Color(210,105,30));
//   tt.modifyImage(d, "月薪:"+salary1, -10+(30*(6+job1.length()+amount1.length())), y, new Color(178,34,34));
//   y = y+25;
//    if(num < 1 ) {
//     System.out.println(num);
//     tt.modifyImage(d, need1, -50, y+25,new Color(0,0,0));
//    }else {
//     for(int i = 0;i<num;i++) {
//     s1[i] = need1.substring(subindex, j);
//     tt.modifyImage(d, s1[i], -50, y,new Color(0,0,0));
//     subindex=j;
//     j+=40;
//     y+=25;
//     }
//     if(strleth%40 != 0) {
//     //System.out.println("不等于0");
//     s1[num] = need1.substring(num * 40);
//     tt.modifyImage(d, s1[num], -50, y,new Color(0,0,0));
//     }
//    }
//   //tt.modifyImage(d, "崗位要求:"+need1, -50, y+25, new Color(0,0,0));
//    }
    
    // 聯(lián)系方式和抵地址
    String name = "張先生";
    String tel = "12334343443";
    String company = "鹽都區(qū)高新區(qū)振興路匯鑫大廈";
    tt.setFont(new Font("黑體",Font.BOLD, 40));
    tt.modifyImage(d, name, -650, 360,new Color(0,0,0));
    tt.modifyImage(d, tel, -450, 360,new Color(0,0,0));
    tt.modifyImage(d, company, -650, 440,new Color(0,0,0));
    
    
    //tt.modifyImage(d, str, -830, -100);
    tt.writeImageLocal("D:\\test\\cc.jpg", d);
    System.out.println("success");
    System.out.println(s[0]);
    System.out.println(s[0].length());
    
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
     String[] fontList = ge.getAvailableFontFamilyNames();
     for(int i=0;i<fontList.length;i++)
     {
        System.out.println("字體:"+fontList[i]);
     }
 
 }
}

實(shí)現(xiàn)效果:模板圖片為:1920 x 1080 px。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決mybatis批量更新(update foreach)失敗的問題

    解決mybatis批量更新(update foreach)失敗的問題

    這篇文章主要介紹了解決mybatis批量更新(update foreach)失敗的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Java的volatile和sychronized底層實(shí)現(xiàn)原理解析

    Java的volatile和sychronized底層實(shí)現(xiàn)原理解析

    文章詳細(xì)介紹了Java中的synchronized和volatile關(guān)鍵字的底層實(shí)現(xiàn)原理,包括字節(jié)碼層面、JVM層面的實(shí)現(xiàn)細(xì)節(jié),以及鎖的類型和MESI協(xié)議在多核處理器中的作用,文章還探討了synchronized和volatile的區(qū)別,以及如何通過Atomic類來實(shí)現(xiàn)更細(xì)粒度的原子操作,感興趣的朋友一起看看吧
    2025-03-03
  • 在啟動后臺 jar包時,使用指定的 application.yml操作

    在啟動后臺 jar包時,使用指定的 application.yml操作

    這篇文章主要介紹了在啟動后臺 jar包時,使用指定的 application.yml操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Spring事務(wù)管理方法步驟解析

    Spring事務(wù)管理方法步驟解析

    這篇文章主要介紹了Spring事務(wù)管理方法步驟解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • 淺談java Properties類的使用基礎(chǔ)

    淺談java Properties類的使用基礎(chǔ)

    下面小編就為大家分享一篇淺談java Properties類的使用基礎(chǔ),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • spring boot容器啟動流程

    spring boot容器啟動流程

    spring cloud是基于spring boot快速搭建的,今天咱們就看看spring boot容器啟動流程,需要的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-01-01
  • JDBC數(shù)據(jù)庫連接步驟解析

    JDBC數(shù)據(jù)庫連接步驟解析

    這篇文章主要介紹了JDBC數(shù)據(jù)庫連接步驟解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • JAVA實(shí)現(xiàn)監(jiān)測tomcat是否宕機(jī)及控制重啟的方法

    JAVA實(shí)現(xiàn)監(jiān)測tomcat是否宕機(jī)及控制重啟的方法

    這篇文章主要介紹了JAVA實(shí)現(xiàn)監(jiān)測tomcat是否宕機(jī)及控制重啟的方法,可實(shí)現(xiàn)有效的檢測及控制tomcat服務(wù)器運(yùn)行,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • Java Stax解析XML示例

    Java Stax解析XML示例

    這篇文章主要介紹了Java Stax解析XML示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-09-09
  • java后臺調(diào)用HttpURLConnection類模擬瀏覽器請求實(shí)例(可用于接口調(diào)用)

    java后臺調(diào)用HttpURLConnection類模擬瀏覽器請求實(shí)例(可用于接口調(diào)用)

    這篇文章主要介紹了java后臺調(diào)用HttpURLConnection類模擬瀏覽器請求實(shí)例,該實(shí)例可用于接口調(diào)用,具有一定的實(shí)用價值,需要的朋友可以參考下
    2014-10-10

最新評論

宽城| 东辽县| 南京市| 遂昌县| 巴中市| 江阴市| 灵武市| 朝阳区| 凤翔县| 福清市| 郯城县| 安平县| 淮安市| 曲周县| 饶河县| 安丘市| 海原县| 康乐县| 方山县| 墨江| 安远县| 澄江县| 涿鹿县| 定南县| 长宁区| 四平市| 丰原市| 昌图县| 饶河县| 安康市| 江孜县| 阳江市| 襄樊市| 静安区| 余庆县| 金溪县| 新乐市| 榆中县| 濮阳县| 文成县| 佛学|