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

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

以上为全部内容!
end ↑

java实现图片上插入文字并保存相关推荐

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

    这两天通过在网上查阅资料,了解了在图片上插入文字并保存的功能,下面记录一下. 工具类:PrintImage. package com.learning.www.utils; import java.a ...

  2. java图片叠加_[原创]JAVA中图片上叠加文字的方法

    --sunfruit JDK:1.3.x以上 功能:下面是一个简单在图片上面叠加文字的方法,有朋友如果有这方面的问题,就起个了解的作用 代码如下: import java.io.IOException ...

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

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

  4. java在图片上写文字

    网上很多需要用到codec.jpeg.JPEGImageEncoder;类而这个类是过时的类,经常失败加载不成功,遂换成ImageIO.write测试可用 public class WaterMark ...

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

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

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

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

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

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

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

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

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

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

最新文章

  1. 从Inception v1,v2,v3,v4,RexNeXt到Xception再到MobileNets,ShuffleNet,MobileNetV2
  2. win下修改mysql默认的字符集以防止乱码出现
  3. python123编写函数求和_Python基础之函数
  4. voltdb mysql_MySQL/HandlerSocket和VoltDB:NoSQL的竞争者
  5. 前端性能优化之图像优化原理
  6. LoadRunner
  7. uva 12558 Egyptian Fractions (HARD version)
  8. CVTE 2017 秋季校招笔试题回忆(C++后台)
  9. 银行业应用系统监控的维度与目标
  10. RingBuffer的快速上手使用方法
  11. css交集选择器的使用
  12. UT000010 Session is Invalid
  13. JS实现页面快捷键功能
  14. php数据结构 链表,php数据结构-单链表
  15. 《操作系统原理》 记录 (41)
  16. 【嵌入式--伺服电机】(11)MIT无刷电机FOC硬件电路分析
  17. 焊工证怎么考取需要什么条件?焊工证考试时间是什么时候?
  18. nginx_tcp转发
  19. Push notification - Caused by java.io.IOException toDerInputStream rejects tag
  20. 【数论】同余(一):同余的基本概念与性质

热门文章

  1. 1.巴特沃斯模拟滤波器(低通,高通,带通,带阻)设计-MATLAB实现
  2. 教你怎么0基础学编程
  3. 有关RO2模拟器与开私服的一点联想
  4. 最新最全论文合集——FOCS 历年最佳论文汇总
  5. php avi格式播放,Linux_网页播放器代码全集,1.avi格式代码片断如下:obje - phpStudy...
  6. 一键快速构建 vue-electron 项目
  7. 三星+android+u盘模式,三星手机usb存储器如何连接电脑的【详解】
  8. echarts-legend-自定义图例的图标,未激活图例置灰
  9. 前端JS常用工具方法 , 收藏
  10. 用户增长-高价值行为模型