欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现图片上插入文字并保存

程序员文章站 2024-02-16 18:11:34
这两天通过在网上查阅资料,了解了在图片上插入文字并保存的功能,下面记录一下。 工具类:printimage。 package com.learning.www....

这两天通过在网上查阅资料,了解了在图片上插入文字并保存的功能,下面记录一下。

工具类: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); // 添加字体的属性设置
 
  private graphics2d g    = null;
 
  private int    fontsize = 0;
 
  /**
   * 导入本地图片到缓冲区
   */
  public bufferedimage loadimagelocal(string imgname) {
    try {
      return imageio.read(new file(imgname));
    } catch (ioexception e) {
      system.out.println(e.getmessage());
    }
    return null;
  }
 
  /**
   * 导入网络图片到缓冲区
   */
  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());
      }
    }
  }
 
  /**
   * 设定文字的字体等
   */
  public void setfont(font font) {
    
    this.font = font;
  }
 
  /**
   * 修改图片,返回修改后的图片缓冲区(只输出一行文本)
   */
  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));//设置字体颜色
      g.setcolor(color);//设置字体颜色
      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;
  }
 
  
  /**
   * 修改图片,返回修改后的图片缓冲区(只输出一行文本)
   *
   * 时间: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);//设置字体颜色
      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 = "岗位职责:"+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, "人数:"+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;
    // 公司标题 72号字体==96px
    tt.setfont(new font("造字工房力黑(非商用)常规体", 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);
    
    //公司简介,限定字数
    tt.setfont(new font("黑体",font.plain, 30));
    string str = "功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"
    +"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"
    +"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"
    +"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存"+"功能:可以实现在图片模板上写内容并保存";
    system.out.println(str.length());
    //计算字符串长度
    int strleth=str.length();
    //计算循环次数
    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 = -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 = "岗位职责:"+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, "人数:"+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));
//    }
    
    // 联系方式和抵地址
    string name = "张先生";
    string tel = "12334343443";
    string company = "盐都区高新区振兴路汇鑫大厦";
    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]);
     }
 
 }
}

实现效果:模板图片为:1920 x 1080 px。

java实现图片上插入文字并保存

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。