java在图片上添加文字
业务需求要在图片上添加水印。下面粘出代码供自己和大家分享
package com.pro.drawTextOnImg; import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import javax.imageio.ImageIO;
import javax.swing.ImageIcon; import org.apache.commons.lang3.StringUtils; public class TestImg { public static void main(String[] args) {
String filePath = "d:\\inimg03.png";
String outPath = "d:\\2.jpg";
drawTextInImg(filePath, outPath, new FontText("中国", 1, "#CC2BAC", 40, "黑体")); } public static void drawTextInImg(String filePath,String outPath, FontText text) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image img = imgIcon.getImage();
int width = img.getWidth(null);
int height = img.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB); Graphics2D g = bimage.createGraphics();
g.setColor(getColor(text.getWm_text_color()));
g.setBackground(Color.white);
g.drawImage(img, 0, 0, null);
Font font = null;
if (StringUtils.isEmpty(text.getWm_text_font())
&& text.getWm_text_size() != null) {
font = new Font(text.getWm_text_font(), Font.BOLD,
text.getWm_text_size());
} else {
font = new Font(null, Font.BOLD, 15);
} g.setFont(font);
FontMetrics metrics = new FontMetrics(font){};
Rectangle2D bounds = metrics.getStringBounds(text.getText(), null);
int textWidth = (int) bounds.getWidth();
int textHeight = (int) bounds.getHeight();
int left = 0;
int top = textHeight; //九宫格控制位置
if(text.getWm_text_pos()==2){
left = width/2;
}
if(text.getWm_text_pos()==3){
left = width -textWidth;
}
if(text.getWm_text_pos()==4){
top = height/2;
}
if(text.getWm_text_pos()==5){
left = width/2;
top = height/2;
}
if(text.getWm_text_pos()==6){
left = width -textWidth;
top = height/2;
}
if(text.getWm_text_pos()==7){
top = height - textHeight;
}
if(text.getWm_text_pos()==8){
left = width/2;
top = height - textHeight;
}
if(text.getWm_text_pos()==9){
left = width -textWidth;
top = height - textHeight;
}
g.drawString(text.getText(), left, top);
g.dispose(); try {
FileOutputStream out = new FileOutputStream(outPath);
ImageIO.write(bimage, "JPEG", out);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // color #2395439
public static Color getColor(String color) {
if (color.charAt(0) == '#') {
color = color.substring(1);
}
if (color.length() != 6) {
return null;
}
try {
int r = Integer.parseInt(color.substring(0, 2), 16);
int g = Integer.parseInt(color.substring(2, 4), 16);
int b = Integer.parseInt(color.substring(4), 16);
return new Color(r, g, b);
} catch (NumberFormatException nfe) {
return null;
}
}
}
下面是 FontText里面主要是水印的基本信息
package com.pro.drawTextOnImg;
public class FontText {
private String text;
private int wm_text_pos;
private String wm_text_color;
private Integer wm_text_size;
private String wm_text_font;//字体 “黑体,Arial”
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getWm_text_pos() {
return wm_text_pos;
}
public void setWm_text_pos(int wm_text_pos) {
this.wm_text_pos = wm_text_pos;
}
public String getWm_text_color() {
return wm_text_color;
}
public void setWm_text_color(String wm_text_color) {
this.wm_text_color = wm_text_color;
}
public Integer getWm_text_size() {
return wm_text_size;
}
public void setWm_text_size(Integer wm_text_size) {
this.wm_text_size = wm_text_size;
}
public String getWm_text_font() {
return wm_text_font;
}
public void setWm_text_font(String wm_text_font) {
this.wm_text_font = wm_text_font;
}
public FontText(String text, int wm_text_pos, String wm_text_color,
Integer wm_text_size, String wm_text_font) {
super();
this.text = text;
this.wm_text_pos = wm_text_pos;
this.wm_text_color = wm_text_color;
this.wm_text_size = wm_text_size;
this.wm_text_font = wm_text_font;
}
public FontText(){}
}
java在图片上添加文字的更多相关文章
- python 图片上添加文字
import PIL from PIL import ImageFont from PIL import Image from PIL import ImageDraw #设置字体,如果没有,也可以不 ...
- Office WORD如何在图片上添加文字
如图所示,在图片格式中选择图片衬于文字下方即可,这样看起来感觉就像在图片上直接加字一样,没有生硬的感觉. 最终效果: Word如何在图片上添加文字Word如何在图片上添加文字Word如何在图片上添加文 ...
- python如何在图片上添加文字(中文和英文)
Python在图片上添加文字的两种方法:OpenCV和PIL 一.OpenCV方法 1.安装cv2 pip install opencv-python 2.利用putText方法来实现在图片的指定位置 ...
- C#在图片上添加文字代码
创建.NET WinForm程序,设置项目的默认命名空间为Keleyi.Com,在窗体上添加一个PictureBox控件pictureBox_keleyi_com和一个Button控件button_A ...
- python PIL图像处理-图片上添加文字
首先需要安装库pillow cmd安装命令:pip install pillow 安装完后,编写脚本如下: from PIL import Image, ImageDraw, ImageFont de ...
- java 对图片的添加文字描述,以及两张图片合成一张
最近公司一个需要,需要把商品的优惠卷分享链接,生成一个二维码然后和商品主图合成一张,并且在新合成的主图增加商品信息的描述,好了直接看合成后图片的样式 下面我就直接贴代码,首先是Contorller层 ...
- html+css 在图片上添加文字
html <view class="container"> <image class="" src="{{book.image}}& ...
- 如何去除图片上的文字(PS使用教程)
很多时候由于工作的需要,需要对我们的图片进行修改,修改的同时还想要保存我们的图片背景,所以很多人就不知道怎么弄了,小编跟大家分享一下使用PS如何简单的去掉图片上的文字,希望对大家有所帮助! 方法/步骤 ...
- python 图片上添加数字源代码
最近因工作需要,需要在图片上添加数字,查询了资料,自己写了一个方法,并进行了测试,由于代码用到了PIL库,需要下载安装,下载地址:http://www.pythonware.com/products/ ...
随机推荐
- android studio 2.0 GPU Debugger使用说明
GPU Debugger GPU Debugging Tools The GPU debugging tools are an experimental feature intended to hel ...
- 6.Linux的文件权限与目录配置
Linux文件属性:(- rwx r-x r-- 1 root root 2800 feb 1 11:55 test.txt 顺序按着空格排序,除了时间) 第一组字符代表这个文件是目录.文件或链接文件 ...
- ACM集训的1B。。。。黑色星期五。。。。2333333
题目: 印象中有好多个13号是星期五,13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至1900 ...
- OC中协议, 类目, 时间, 延展, 属性
只有继承和协议需要引IMPORT "头文件"; 必须接受marryprotocol协议, id<marryprotocol>基于类型的限定, 才能给实例变量赋值 @pr ...
- Java中的夏令时问题
因为在用C#做项目的时候被夏令时坑过一回,所以这次将在java中的时区转换信息做一下记录,很简单 SimpleDateFormat inputFormat = new SimpleDateFormat ...
- OC ---- 字典集合 iOS学习-----细碎知识点总结
实例方法的创建 NSDictionary *wukong = [[NSDictionary alloc] initWithObjectsAndKeys:", @"age" ...
- ThinkPHP3.1快速入门(1)基础
学习网址:http://www.thinkphp.cn/document/60.html
- GPS部标监控平台的架构设计(十一)-基于Memcached的分布式Gps监控平台
部标gps监控平台的架构,随着平台接入的车辆越来越多,架构也面临越来越大的负载挑战,我们当然希望软件尽可能的优化并能够接入更多的车辆,减少在硬件上的投资.但是当车辆增多到某一个临界点的时候,仍然要面临 ...
- 网页3D引擎“Babylon.JS”入门教程翻译总结
使用三个月的业余时间把官方教程的入门部分译为中文并上传到github,在下一步编程前做一个总结. 历程: 最早接触游戏编程是在大三下学期,用汇编语言和实验室里的单片机.触摸屏.电机(提供声效)编的打地 ...
- WebForm Repeater使用
Repeater: HeaderTemplate: 在加载开始执行一遍 ItemTemplate : 有多少条数据,执行多少遍 FooterTemplate :在加载最后执行一遍 Alternatin ...