package com.zxl.test;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
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.IM4JavaException;
import org.im4java.core.IMOperation;
public class ImageUtil {
private static final String graphicsMagickPath = "C:\\Program Files\\GraphicsMagick-1.3.25-Q16";
/**
* 按九宫格位置添加水印
* @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 = getImageSize(watermarkImg);
int[] srcImgSide = getImageSize(srcPath);
// int[] xy = getXY(srcImgSide, watermarkImgSide, position, x, y);
int[] xy = getXY(x,y);
addWatermarkToImg(srcPath,distPath,watermarkImg,watermarkImgSide[0],watermarkImgSide[1],xy[0],xy[1],alpha);
}
/**
* 获取图片尺寸
* @param imgPath
* @return
* @throws IOException
*/
private int[] getImageSize(String imgPath) throws IOException {
int[] size = new int[2];
Image img = ImageIO.read(new File(imgPath));
size[0] = img.getWidth(null);
size[1] =img.getHeight(null);
return size;
}
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;
}
private int[] getXY(int x, int y){
int[] xy = new int[2];
xy[0] = x;
xy[1] = y;
return xy;
}
/**
* 添加图片水印
* @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 void addWatermarkToImg(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);
cmd.setSearchPath(graphicsMagickPath);
IMOperation op = new IMOperation();
op.dissolve(alpha);
op.geometry(width, height, x, y);
op.addImage(watermarkImg,srcPath,distPath);
cmd.run(op);
}
/**
* 把文字转化为一张背景透明的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("Default",Font.BOLD,fontSize);
//获取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());
File file=new File(outfile);
ImageIO.write(image, "png", file);//输出png图片
}
/**
* 根据坐标裁剪图片
*
* @param srcPath 要裁剪图片的路径
* @param newPath 裁剪图片后的路径
* @param x 起始横坐标
* @param y 起始纵坐标
* @param x1 结束横坐标
* @param y1 结束纵坐标
*/
public void cutImage(String srcPath, String newPath, int x, int y, int x1,int y1) throws Exception {
int width = x1 - x;
int height = y1 - y;
IMOperation op = new IMOperation();
op.addImage(srcPath);
/** width:裁剪的宽度 * height:裁剪的高度 * x:裁剪的横坐标 * y:裁剪纵坐标 */
op.crop(width, height, x, y);
op.addImage(newPath);
ConvertCmd convert = new ConvertCmd(true);
convert.setSearchPath(graphicsMagickPath);
convert.run(op);
}
/**
* 根据尺寸缩放图片
* @param width 缩放后的图片宽度