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

工具类: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号字体==96pxtt.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实现在图片上插入文字并保存。相关推荐

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

    记录一下之前项目中用到过的工具类,可以在图片中写入文字并且生成新的图片,记录一下. 文章转载自:https://www.jb51.net/article/162956.htm start ↓ 工具类: ...

  2. Java实现在图片上添加文字(水印)

    今天分享一个:通过Java代码,给图片添加文字. 比如下面这个图片,我们在左下角就添加了一个文字版的水印,那么这是如何实现的呢 ? 目录 [1]获取原图片对象 (1.1)读取本地图片 (1.2)读取网 ...

  3. matlab2018在图片上添加文字并保存且图片没有白边

    文章目录 图片添加文字并保存 去除白边 添加文字使用 text函数,难点在于保存,使用 getframe来捕获,然后使用 imwrite来保存,类似的教程网上很多,但是有的添加文字不是使用text函数 ...

  4. java读取图片、PDF中图片上的文字

    读取图片上的文字 提示:本文是基于tess4j 文章目录 读取图片上的文字 前言 一.tess4j是什么? 二.使用步骤 1.POM引入库 2.实例代码 总结 前言 总是有一些与众不同的需求在等着研发 ...

  5. java获取远程图片并在图片上写文字

    本编文章主要是分享一下,从远程获取图片文件,用java在图片上写文字并合成图片的示例.一下代码完全拷贝后是可以正常运行的. 主要有三个类: DrawPicFromUrlToOSS:核心类,获取图片并在 ...

  6. java绘制海报,使用BufferedImage,Graphics2D,drawString方法在图片上写文字,中文不显示;drawString写文字为空问题

    项目场景: 项目场景:公司需要制作一张海报.通过java后台制作海报,给图片拼接图片,添加水印添加文字,定义字体为"宋体",给海报添加头像.姓名.性别.个人简介.二维码等信息.把代 ...

  7. java生成二维码(在图片上生成二维码(二维码带logo)并且在图片上添加文字标签)

    1pom.xml <!--生成二维码--> <dependency><groupId>cn.hutool</groupId><artifactId ...

  8. python3识别图中的文字_Python3.x:如何识别图片上的文字

    Python3.x:如何识别图片上的文字 安装pytesseract库,必须先安装其依赖的PIL及tesseract-ocr,其中PIL为图像处理库,而后面的tesseract-ocr则为google ...

  9. 爬虫requests如何提取图片_如何提取图片上的文字(办公技巧)

    是夜,公寓里,一道黑影闪过窗前,对面楼顶传来一声凄厉的猫叫声. 王美丽一个机灵,目中闪过一丝恐惧. 她面前的电脑里,正在播放<午夜凶铃>. 阴森恐怖的配音,让她全身发冷,感觉后脊梁有一股凉 ...

最新文章

  1. java当前路径和相对路径相关的疑惑
  2. SSO 单点登录会话管理
  3. 低压差降压稳压电路设计
  4. VMTK学习——02.基本的PYPES教程
  5. Mysql 获取当月和上个月第一天和最后一天的SQL
  6. 深入理解Java虚拟机(一):Java内存模型
  7. 一行代码实现strlen
  8. XCTF-Reverse:game(涉及异或脚本编写)
  9. matplotlib figure转为numpy array或者PIL图像进行显示
  10. 2015蓝桥杯省赛---java---B---8(饮料换购)
  11. Python+django网页设计入门(7):常用ORM操作
  12. 详细介绍Linux shell脚本基础学习(一)
  13. 如果有因果报应和轮回,又是怎么管理的?
  14. 只需 1 分钟,这个网站用 AI 分离歌曲的人声、伴奏和乐器声
  15. HP MSL6030 磁带库异常fault code:3106
  16. 制作win7 u盘启动盘
  17. 二甲苯酚的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  18. Java基于JSP+Servlet的校友论坛管理系统
  19. Icarus iverilog中PLI使用范例
  20. 什么叫黑名单?黑名单还能贷款吗?

热门文章

  1. 人物志 | MIT科技创新“远见者”:美团NLP负责人王仲远
  2. php cakephp like,CakePHP
  3. 最新最全论文合集——FOCS 历年最佳论文汇总
  4. 三分钟读懂:云计算与虚拟化的关系
  5. [附源码]Python计算机毕业设计爱宝贝影楼管理系统Django(程序+LW)
  6. 采用CAnimateCtrl的AVI播放器,没有什么应用的,纯当练技术
  7. linux检查包是否已经安装,Linux下怎样检查、如何查看某软件包是否已经安装
  8. 校验码中码距与纠错能力的关系
  9. 微软飞行模拟服务器,《微软飞行模拟2020》已推出首个公共SDK
  10. 怎么在电脑上打开epub格式电子书