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

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. HTML/Elements/base

    https://www.w3.org/wiki/HTML/Elements/base HTML/Elements/base < HTML‎ | Elements   List of Elemen ...

  2. Ref 与 Out 的使用方法及区别

    C# 中我们有的时候会遇到这样的情况,一个方法需要返回多个参数的时候.怎么办呐? 我们可以给返回的参数加上 Out  或  Ref 类型标识  就可以实现返回多个参数了. 具体他们两个的用法有什么区别 ...

  3. Zookeeper学习笔记

    ZK ID存放路径 /tmp/zookeeper/myid

  4. iOS10新特性

    1.Siri API 的开放自然是 iOS 10 SDK 中最激动人心也是亮眼的特性.Apple 加入了一套全新的框架 Intents.framework 来表示 Siri 获取并解析的结果. 在 i ...

  5. zookeeper+jstorm的集群搭建

    zookeeper的配置: zookeeper有三种配置方式:单机式/伪分布式/集群式 其中伪分布式是在一台电脑上通过不同的端口来模拟分布式情形,需要N份配置文件和启动程序,而集群式是多个zookee ...

  6. laravel 在windows中使用一键安装包步骤

    安装 PHP 注意一:Laravel 5.0 开始对 PHP 版本的要求是 >=5.4,Laravel 5.1 要求 PHP 版本 >=5.5.9,所以,建议大家尽量安装 5.5.x 的最 ...

  7. JMeter学习-038-JMeter Linux 环境配置

    1.1.下载 Download URL:http://mirrors.tuna.tsinghua.edu.cn/apache//jmeter/binaries/apache-jmeter-3.0.tg ...

  8. ipxe引导远程的windows

    使用ipxe解决本地引导远程系统 本地安装的centos7,然后修改grub.cfg来使用ipxe技术引导远程windows,实现双系统 os-->centos7 修改grub.cfg 在文件最 ...

  9. placeholder文字颜色与是否显示兼容性

    1.ie显示问题 <script type="text/javascript"> $(document).ready(function(){ var doc=docum ...

  10. [SharePoint 2010] Copy list item with version history and attachment

    private void MoveItem(SPListItem sourceItem, SPListItem destinationItem) { if (sourceItem == null || ...