资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java中怎么在图片中插入文字并保存

这篇文章给大家介绍java中怎么在图片中插入文字并保存,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

成都创新互联是专业的常山网站建设公司,常山接单;提供成都网站设计、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行常山网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

工具类: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

启动类:

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

关于java中怎么在图片中插入文字并保存就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网页名称:java中怎么在图片中插入文字并保存
网站地址:http://cdkjz.cn/article/iesjpp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220