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

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. jquery toggle方法

    $("#myDiv").toggle(function () { alert(1); }, function () { alert(2); }); 某种需求下可以替代click事件 ...

  2. LeetCode Plus One Linked List

    原题链接在这里:https://leetcode.com/problems/plus-one-linked-list/ 题目: Given a non-negative number represen ...

  3. cordova使用Gradle构建下载maven太慢,使用阿里云镜像

    修改build.gradle: buildscript { repositories { maven{ url 'http://maven.aliyun.com/nexus/content/group ...

  4. windows2008吃尽内存的解决办法

    最近才用上windows2008,之前一直用的是windows2003,发现系统运行一段时间后,内存吃紧,赶紧打开资源查看器,发现当前运行的程序占有内存都很小,后经查资料,原来是被windows200 ...

  5. VS2010 LINK1123:failure during conversion to COFF:file invalid or corrupt

    今天用Visual Studio 2010编译一个C工程时突然遇到下面这个编译错误.fatal error LINK1123:failure during conversion to COFF:fil ...

  6. 经历alidns在国外的严重延时

    有个域名,是在国外1und1申请的,但dns的解析,国外的空间的功能弱爆了. 之前是放在dnspod,后来又试过dnspod的海外, 最后放回alidns,之前一直都很好的. 这2天国内没问题,在德国 ...

  7. 前端forEach在Array、map、set中的使用

    数组: var s = ['a','b','c']; s.forEach(function(ele,index,array){ console.log(ele); }); Map: var map = ...

  8. iOS原生APP与H5+JS交互////////////////////zzzz

    原生代码中直接加载页面 1.    具体案例 加载本地/网络HTML5作为功能介绍页 2.    代码示例 //本地 -(void)loadLocalPage:(UIWebView*)webView ...

  9. UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  10. Asp.net上传文件后台通过二进制流发送到其他Url保存

    实际情况一般有单独的站点存放静态文件,比如图片.office文档等.A站点的操作需要上传文件到B站点, 下面介绍一种方法通过System.Net.WebClient类的UploadData方法 . u ...