在app的后台中,有时候为了标示版权,需要给图片加上水印。

在liunx中,IM4JAVA+GraphicsMagick是个高效处理图片的方案,图片的裁剪是使用了这个技术方案,为了减少不必要的开发成本和运维成本,对应水印,我们是打算继续采用这个方案。

但在开发的过程中,发现这个方案对中文水印支持得不好。

根据网上的搜索结果,就算采用了im4java的GMOperation,并将水印的字符串转成GBK的编码,添加中文水印时,对于奇数个数的中文,没问题;但对于偶数个数的中文,就出现乱码了。

试了多次后,发现这个问题没法解决,于是试了JMagick+ ImageMagick。

但是,在网上找到一份资料,http://javantsky.iteye.com/blog/747807, 里面提到JMagick+ImageMagick作为应用服务的缺点,并建议可以使用IM4JAVA:

The "JNI hazard" here is that if something you use (f.ex libtiff for reading
TIFF files) has a memory bug then it can make your whole JVM crash. Thats of
course frustrating and therefore its great to have im4java around, which
starts IM as an external process, so JVM crashes are avoided.
* *
Coolest thing would be if JMagick and im4java could have the same API so it
was easy to switch depending on luckyness. Ive asked the author of im4java
to attemt to be as compatible as possible, but as im4java is very much
different internally its limited how much can be done in that direction. If you don't like the risk, stick to im4java. If your want optimal
performance give JMagick a try. And, its not JMagick that is buggy, its what it depends on (hereunder IM)
that is not always (memory) bug free on long running processes.
I also have never seen a mismatch between JMagick binary and ImageMagick
binaries leading to crashes.

最后,采用了以下的方案视线图片的中文水印:

1.先把中文生成背景透明的png图片

2.把这些png图片作为图片水印贴在图上

代码如下:

package test;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException; import javax.imageio.ImageIO; import org.im4java.core.CompositeCmd;
import org.im4java.core.ConvertCmd;
import org.im4java.core.GMOperation;
import org.im4java.core.GraphicsMagickCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation; public class Watermark { /**
* 按九宫格位置添加水印
* @param srcPath 原图片路径
* @param distPath 新图片路径
* @param watermarkImg 水印图片路径
* @param position 九宫格位置[1-9],从上往下,从左到右排序
* @param x 横向边距
* @param y 纵向边距
* @param alpha 透明度
* @throws IOException
* @throws InterruptedException
* @throws IM4JavaException
*/
public void WatermarkImg(String srcPath,String distPath,String watermarkImg, int position, int x, int y, int alpha) throws IOException, InterruptedException, IM4JavaException{
int[] watermarkImgSide = getImageSide(watermarkImg);
int[] srcImgSide = getImageSide(srcPath);
int[] xy = getXY(srcImgSide, watermarkImgSide, position, y, x);
watermarkImg(srcPath,distPath,watermarkImg,watermarkImgSide[0],watermarkImgSide[1],xy[0],xy[1],alpha);
} private int[] getImageSide(String imgPath) throws IOException {
int[] side = new int[2];
Image img = ImageIO.read(new File(imgPath));
side[0] = img.getWidth(null);
side[1] =img.getHeight(null);
return side;
} /**
* 添加图片水印
* @param srcPath 原图片路径
* @param distPath 新图片路径
* @param watermarkImg 水印图片路径
* @param width 水印宽度(可以于水印图片大小不同)
* @param height 水印高度(可以于水印图片大小不同)
* @param x 水印开始X坐标
* @param y 水印开始Y坐标
* @param alpha 透明度[0-100]
* @throws IOException
* @throws InterruptedException
* @throws IM4JavaException
*/
private synchronized void watermarkImg(String srcPath,String distPath,String watermarkImg, int width, int height, int x, int y, int alpha) throws IOException, InterruptedException, IM4JavaException{
CompositeCmd cmd = new CompositeCmd(true);
String path = "C://Program Files//GraphicsMagick-1.3.19-Q8";
cmd.setSearchPath(path);
IMOperation op = new IMOperation();
op.dissolve(alpha);
op.geometry(width, height, x, y);
op.addImage(watermarkImg);
op.addImage(srcPath);
op.addImage(distPath);
cmd.run(op);
} private int[] getXY(int[] image, int[] watermark, int position, int x, int y) {
int[] xy = new int[2];
if(position==1){
xy[0] = x;
xy[1] = y;
}else if(position==2){
xy[0] = (image[0]-watermark[0])/2; //横向边距
xy[1] = y; //纵向边距
}else if(position==3){
xy[0] = image[0]-watermark[0]-x;
xy[1] = y;
}else if(position==4){
xy[0] = x;
xy[1] = (image[1]-watermark[1])/2;
}else if(position==5){
xy[0] = (image[0]-watermark[0])/2;
xy[1] = (image[1]-watermark[1])/2;
}else if(position==6){
xy[0] = image[0]-watermark[0]-x;
xy[1] = (image[1] - watermark[1])/2;
}else if(position==7){
xy[0] = x;
xy[1] = image[1] - watermark[1] - y;
}else if(position==8){
xy[0] = (image[0]-watermark[0])/2;
xy[1] = image[1] - watermark[1] - y;
}else{
xy[0] = image[0]-watermark[0]-x;
xy[1] = image[1] - watermark[1] - y;
}
return xy;
} /**
* 把文字转化为一张背景透明的png图片
* @param str 文字的内容
* @param fontType 字体,例如宋体
* @param fontSize 字体大小
* @param colorStr 字体颜色,不带#号,例如"990033"
* @param outfile png图片的路径
* @throws Exception
*/
public void converFontToImage(String str,String fontType,int fontSize,String colorStr, String outfile) throws Exception{ Font font=new Font(fontType,Font.BOLD,fontSize);
File file=new File(outfile);
//获取font的样式应用在str上的整个矩形
Rectangle2D r=font.getStringBounds(str, new FontRenderContext(AffineTransform.getScaleInstance(1, 1),false,false));
int unitHeight=(int)Math.floor(r.getHeight());//获取单个字符的高度
//获取整个str用了font样式的宽度这里用四舍五入后+1保证宽度绝对能容纳这个字符串作为图片的宽度
int width=(int)Math.round(r.getWidth())+1;
int height=unitHeight+3;//把单个字符的高度+3保证高度绝对能容纳字符串作为图片的高度
//创建图片 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g2d.dispose();
g2d = image.createGraphics();
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(1));
g2d.setColor(new Color(Integer.parseInt(colorStr, 16)));//在换成所需要的字体颜色
g2d.setFont(font);
g2d.drawString(str, 0,font.getSize()); ImageIO.write(image, "png", file);//输出png图片
} }

测试实例:

package test;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File; import javax.imageio.ImageIO; import org.im4java.core.CompositeCmd;
import org.im4java.core.ConvertCmd;
import org.im4java.core.GMOperation;
import org.im4java.core.IMOperation; public class test { public static void main(String[] args) { try { String src="D://src.jpg"; //需要加水印的源图片
String desc="D://desc.jpg"; //生成的水印图片的路径
String water="D://water.png"; //用中文转换成的背景透明的png图片
String fontType="C:\\Windows\\Fonts\\simsun.ttc"; //指定字体文件为宋体
String colorStr="990033"; //颜色
int fontSize=18; Watermark watermark=new Watermark(); /*
* 把文字转化为一张背景透明的png图片
* @param str 文字的内容
* @param fontType 字体,例如宋体
* @param fontSize 字体大小
* @param colorStr 字体颜色,不带#号,例如"990033"
* @param outfile png图片的路径
* @throws Exception
*/
watermark.converFontToImage("中华人们", fontType, fontSize, colorStr, water); /*
* 把文字的png图片贴在原图上,生成水印
* @param srcPath 原图片路径
* @param distPath 新图片路径
* @param watermarkImg 水印图片路径
* @param position 九宫格位置[1-9],从上往下,从左到右排序
* @param x 横向边距
* @param y 纵向边距
* @param alpha 透明度
*/
watermark.WatermarkImg(src, desc, water, 1, 20,20, 100); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

源码已放在github:https://github.com/newjueqi/ChineseWaterMark#

app后端系列文章总目录

如果您觉得这系列的文章对你有所帮助,欢迎打赏。

支付宝账号:190678908@qq.com 收款人:曾健生

新建了“app后端技术” 交流qq群:254659220

[文章作者]曾健生

[作者邮箱]h6k65@126.com

[作者QQ]190678908

[新浪微博] @newjueqi

[博客]http://blog.csdn.net/newjueqi

版权声明:本文为博主原创文章,未经博主允许不得转载。

app后端设计(13)--IM4JAVA+GraphicsMagick实现中文水印的更多相关文章

  1. app后端设计--总目录 (转)

    特此说明,我转载的!!! app后端设计(1)--api app后端设计(2)--xmpp的使用 app后端设计(3)--短信,邮件,推送服务 app后端设计(4)-- 通讯的安全性 app后端设计( ...

  2. app后端设计--总目录

    做了3年app相关的系统架构,api设计,先后在3个创业公司中工作,经历过手机网页端,android客户端,iphone客户端,现就职于app云后端平台bmob(想了解bmob点击这里).其中的乐与苦 ...

  3. [置顶] app后端设计--总目录

    版权声明:本文为博主原创文章,未经博主允许不得转载. 做了3年app相关的系统架构,api设计,先后在3个创业公司中工作,经历过手机网页端,Android客户端,iphone客户端,现就职于app云后 ...

  4. app后端设计(php)

    来源:http://blog.csdn.net/column/details/mobilebackend.html?page=1 做了3年app相关的系统架构,api设计,先后在3个创业公司中工作,经 ...

  5. app后端设计(12)--图片的处理

    app上线后,不断接受用户的反馈,于是,反馈非常差的情况下,都会有app的改版. 一旦app的改版,都会有比较大的UI改动,一改动UI,那么图片的尺寸也就必须要改变. 在app后端设计(1)—api( ...

  6. app后端设计(0)--总文件夹

    原文:http://blog.csdn.net/newjueqi/article/details/19003775 做了接近两年app相关的系统架构,api设计,先后在两个创业公司中工作,经历过手机网 ...

  7. app后端设计(0)--总目录(转)

    原文:http://blog.csdn.net/newjueqi/article/details/19003775 做了接近两年app相关的系统架构,api设计,先后在两个创业公司中工作,经历过手机网 ...

  8. gm(GraphicsMagick)图片中文水印乱码问题

    1.GraphicsMagick图片中文水印乱码问题处理方式 如出现乱码是由于服务器中缺少中文字库所致,为避免系统中存在多个中文字库冲突, 所以没有必要在安装GraphicsMagick时就将字库文件 ...

  9. app后端设计(2)--xmpp的使用(2014.01.14更新)

    在app中有时候是需要添加聊天服务,在这里谈谈曾经开发聊天服务的经验: (1)聊天服务端选的openfire,这是一个基于xmpp协议的聊天服务器(XMPP是一种基于XML的协议,它继承了在XML环境 ...

随机推荐

  1. C++之继承

    #include <iostream> using namespace std ; class Animal { private: int age ; protected: int id ...

  2. 杭电ACM 1000题

    import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin=n ...

  3. 使用Puppeteer抓取受限网站

    不要相信前端是安全的,今天简单验证一下,但是希望大家支持正版,支持原作者,毕竟写书不易. 安装Puppteer npm install --save puppeteer 选择目标网站 我们这里选择胡子 ...

  4. 前端工程师的修真秘籍(css、javascript和其它)

    以我的经验,大部分技术,熟读下列四类书籍即可. 入门,用浅显的语言和方式讲述正确的道理和方法,如head first系列 全面,巨细无遗地探讨每个细节,遇到疑难问题时往往可以在这里得到理论解答,如De ...

  5. javax.mail

    摘抄 example: public static void sendEmail(ConfBean cBean, String filename, String filepath) { try { P ...

  6. window.open open new window?

    when ever i use window.location.href=//some url it always open a new window, this only happens when ...

  7. Web安全测试工具小集

    从此文开始搬家CSDN的博客 本文内容全部节选自<Ajax Security>第14章内容推荐工具: 模糊黑盒测试工具(Fuzzer): Popular free fuzzers incl ...

  8. Nginx SSL功能支持的一些资料。

    http://wiki.nginx.org/HttpSslModulehttp://zou.lu/nginx-https-ssl-module/http://www.21andy.com/blog/2 ...

  9. AngularJS之备忘与诀窍

    译自:<angularjs> 备忘与诀窍 目前为止,之前的章节已经覆盖了Angular所有功能结构中的大多数,包括指令,服务,控制器,资源以及其它内容.但是我们知道有时候仅仅阅读是不够的. ...

  10. GlitchBot -HZNU寒假集训

    One of our delivery robots is malfunctioning! The job of the robot is simple; it should follow a lis ...