业务需求要在图片上添加水印。下面粘出代码供自己和大家分享

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在图片上添加文字的更多相关文章

  1. python 图片上添加文字

    import PIL from PIL import ImageFont from PIL import Image from PIL import ImageDraw #设置字体,如果没有,也可以不 ...

  2. Office WORD如何在图片上添加文字

    如图所示,在图片格式中选择图片衬于文字下方即可,这样看起来感觉就像在图片上直接加字一样,没有生硬的感觉. 最终效果: Word如何在图片上添加文字Word如何在图片上添加文字Word如何在图片上添加文 ...

  3. python如何在图片上添加文字(中文和英文)

    Python在图片上添加文字的两种方法:OpenCV和PIL 一.OpenCV方法 1.安装cv2 pip install opencv-python 2.利用putText方法来实现在图片的指定位置 ...

  4. C#在图片上添加文字代码

    创建.NET WinForm程序,设置项目的默认命名空间为Keleyi.Com,在窗体上添加一个PictureBox控件pictureBox_keleyi_com和一个Button控件button_A ...

  5. python PIL图像处理-图片上添加文字

    首先需要安装库pillow cmd安装命令:pip install pillow 安装完后,编写脚本如下: from PIL import Image, ImageDraw, ImageFont de ...

  6. java 对图片的添加文字描述,以及两张图片合成一张

    最近公司一个需要,需要把商品的优惠卷分享链接,生成一个二维码然后和商品主图合成一张,并且在新合成的主图增加商品信息的描述,好了直接看合成后图片的样式 下面我就直接贴代码,首先是Contorller层 ...

  7. html+css 在图片上添加文字

    html <view class="container"> <image class="" src="{{book.image}}& ...

  8. 如何去除图片上的文字(PS使用教程)

    很多时候由于工作的需要,需要对我们的图片进行修改,修改的同时还想要保存我们的图片背景,所以很多人就不知道怎么弄了,小编跟大家分享一下使用PS如何简单的去掉图片上的文字,希望对大家有所帮助! 方法/步骤 ...

  9. python 图片上添加数字源代码

    最近因工作需要,需要在图片上添加数字,查询了资料,自己写了一个方法,并进行了测试,由于代码用到了PIL库,需要下载安装,下载地址:http://www.pythonware.com/products/ ...

随机推荐

  1. java基础总结——概述

      一.java语言概述 来自维基百科 https://zh.wikipedia.org/wiki/Java Java是一种计算机编程语言,拥有跨平台.面向对象.泛型编程的特性,广泛应用于企业级Web ...

  2. 转:HAR(HTTP Archive)规范

    HAR(HTTP Archive),是一个用来储存HTTP请求/响应信息的通用文件格式,基于JSON.这个格式的出现可以使HTTP监测工具以一种通用的格式导出所收集的数据,这些数据可以被其他支持HAR ...

  3. nginx 配置优化(简单)

    配置文件     正常运行的必备配置:         1.user username [groupname]:(推荐nginx)         以那个用户身份运行,以在configure指定的用户 ...

  4. 导出csv文件示例

    导出csv文件示例 csv文件默认以英文逗号,做为列分隔符换行符\n作为行分隔符,写入到一个.csv文件即可.含有英文逗号,和换行符会发生数据输出会出现混乱,下面列出一些处理方法.特殊字符处理1.含有 ...

  5. Issue 7: 网络in action

    网络运维基础 基础参数 配置:IP,子网掩码,网关,dns服务器,dhcp服务器 基础应用 在网关设置上搭建VPN组网 改host文件 单台主机原则上只能配置一个网关 协议 协议是全球都遵守的一套编码 ...

  6. spark shell

    启动spark-shell客户端 启动集群模式 本机为master节点 export MASTER=spark://`hostname`:7077bin/spark-shell 或者 bin/spar ...

  7. Python之路----------time模块

    时间模块是常用的模块 一.time模块 import time print(time.clock())#返回处理器时间,3.3开始已经屏蔽. print(time.altzone)#返回与UTC时间差 ...

  8. 如何把Excel中的某列数值如何转换成文本格式,且兼容性最好?

    假设要把下方A列数值变成文本格式.有多种方法,其中下方的方法兼容性最好: 第一步:选择 A 列,然后,运行菜单 数据/分列... 命令,如下图: 第二步:默认 下一步: 第三步:默认 下一步: 第四步 ...

  9. J2EE环境配置与工具使用

    一.Java SDK安装 J2SE最新版安装地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html,包括JDK和JR ...

  10. 偶遇到 java.util.ConcurrentModificationException 的异常

    今天在调试程序 遇到了如此问题 贴上代码来看看稍后分析 List<String> list = null;boolean isUpdate = false;try { list = JSO ...