import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageUtil {
/**
* 对图片裁剪,并把裁剪新图片保存
* @param srcPath 读取源图片路径
* @param toPath 写入图片路径
* @param x 剪切起始点x坐标
* @param y 剪切起始点y坐标
* @param width 剪切宽度
* @param height 剪切高度
* @param readImageFormat 读取图片格式
* @param writeImageFormat 写入图片格式
* @throws IOException
*/
public static void cutOutImage(String srcPath,String toPath,
int x,int y,int width,int height,
String readImageFormat,String writeImageFormat) throws IOException{
FileInputStream fis = null;
ImageInputStream iis =null;
try{
//读取图片文件
fis = new FileInputStream(srcPath);
Iterator it = ImageIO.getImageReadersByFormatName(readImageFormat);
ImageReader reader = (ImageReader) it.next();
//获取图片流
iis = ImageIO.createImageInputStream(fis);
reader.setInput(iis,true);
ImageReadParam param = reader.getDefaultReadParam();
//定义一个矩形
Rectangle rect = new Rectangle(x, y, width, height);
//提供一个 BufferedImage,将其用作解码像素数据的目标。
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0,param);
//保存新图片
ImageIO.write(bi, writeImageFormat, new File(toPath));
}finally{
if(fis!=null){
fis.close();
}
if(iis!=null){
iis.close();
}
}
}
/**
* 生成缩小放大后的图片
* @param toImagePath 写入图片路径
* @param tag 缩小放大后的图片
* @throws IOException
*/
public static void zoomOutInSaveImage (String toImagePath,BufferedImage tag){
FileOutputStream out = null;
try{
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 按倍率缩小放大图片
* @param srcImagePath 读取图片路径
* @param toImagePath 写入图片路径
* @param widthRatio 宽度缩小放大比例
* @param heightRatio 高度缩小放大比例
* @param zoomFlag 缩小放大标识 =true:缩小;=false:放大
*/
public static void zoomOutInImageByRatio(String srcImagePath,String toImagePath,int widthRatio,int heightRatio,boolean zoomFlag){
try{
//读入文件
File file = new File(srcImagePath);
//构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
BufferedImage tag = null;
if (zoomFlag){//缩小
//缩小边长
tag = new BufferedImage(width / widthRatio, height / heightRatio, BufferedImage.TYPE_INT_RGB);
//绘制 缩小 后的图片
tag.getGraphics().drawImage(src, 0, 0, width / widthRatio, height / heightRatio, null);
}else{
//放大边长
tag = new BufferedImage(width * widthRatio, height * heightRatio, BufferedImage.TYPE_INT_RGB);
//绘制放大后的图片
tag.getGraphics().drawImage(src, 0, 0, width * widthRatio, height * heightRatio, null);
}
ImageUtil.zoomOutInSaveImage(toImagePath, tag);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 宽高等比例缩小放大图片
* @param srcImagePath 读取图片路径
* @param toImagePath 写入图片路径
* @param ratio 缩小比例
* @param zoomFlag 缩小放大标识 =true:缩小;=false:放大
*/
public static void zoomOutInImageEqualProportion(String srcImagePath,String toImagePath,int ratio,boolean zoomFlag){
try{
//读入文件
File file = new File(srcImagePath);
//构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
BufferedImage tag = null;
if (zoomFlag){//缩小
//缩小边长
tag = new BufferedImage(width / ratio, height / ratio, BufferedImage.TYPE_INT_RGB);
//绘制 缩小 后的图片
tag.getGraphics().drawImage(src, 0, 0, width / ratio, height / ratio, null);
}else{
//放大边长
tag = new BufferedImage(width * ratio, height * ratio, BufferedImage.TYPE_INT_RGB);
//绘制放大后的图片
tag.getGraphics().drawImage(src, 0, 0, width * ratio, height * ratio, null);
}
ImageUtil.zoomOutInSaveImage(toImagePath, tag);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 指定宽高缩小放大图片
* @param srcImagePath 读取图片路径
* @param toImagePath 写入图片路径
* @param width 指定宽度
* @param height 指定高度
*/
public static void zoomOutInImageBySize(String srcImagePath,String toImagePath,int width,int height){
try{
//读入文件
File file = new File(srcImagePath);
//构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
//放大边长
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//绘制放大后的图片
tag.getGraphics().drawImage(src, 0, 0, width, height, null);
ImageUtil.zoomOutInSaveImage(toImagePath, tag);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 按倍率缩小图片
* @param srcImagePath 读取图片路径
* @param toImagePath 写入图片路径
* @param widthRatio 宽度缩小比例
* @param heightRatio 高度缩小比例
* @throws IOException
*/
public static void reduceImageByRatio(String srcImagePath,String toImagePath,int widthRatio,int heightRatio) throws IOException{
FileOutputStream out = null;
try{
//读入文件
File file = new File(srcImagePath);
//构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
// 缩小边长
BufferedImage tag = new BufferedImage(width / widthRatio, height / heightRatio, BufferedImage.TYPE_INT_RGB);
// 绘制 缩小 后的图片
tag.getGraphics().drawImage(src, 0, 0, width / widthRatio, height / heightRatio, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
/**
* 长高等比例缩小图片
* @param srcImagePath 读取图片路径
* @param toImagePath 写入图片路径
* @param ratio 缩小比例
* @throws IOException
*/
public static void reduceImageEqualProportion(String srcImagePath,String toImagePath,int ratio) throws IOException{
FileOutputStream out = null;
try{
//读入文件
File file = new File(srcImagePath);
// 构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
// 缩小边长
BufferedImage tag = new BufferedImage(width / ratio, height / ratio, BufferedImage.TYPE_INT_RGB);
// 绘制 缩小 后的图片
tag.getGraphics().drawImage(src, 0, 0, width / ratio, height / ratio, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
/**
* 按倍率放大图片
* @param srcImagePath 读取图形路径
* @param toImagePath 写入入行路径
* @param widthRatio 宽度放大比例
* @param heightRatio 高度放大比例
* @throws IOException
*/
public static void enlargementImageByRatio(String srcImagePath,String toImagePath,int widthRatio,int heightRatio) throws IOException{
FileOutputStream out = null;
try{
//读入文件
File file = new File(srcImagePath);
// 构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
//放大边长
BufferedImage tag = new BufferedImage(width * widthRatio, height * heightRatio, BufferedImage.TYPE_INT_RGB);
//绘制放大后的图片
tag.getGraphics().drawImage(src, 0, 0, width * widthRatio, height * heightRatio, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
/**
* 长高等比例放大图片
* @param srcImagePath 读取图形路径
* @param toImagePath 写入入行路径
* @param ratio 放大比例
* @throws IOException
*/
public static void enlargementImageEqualProportion(String srcImagePath,String toImagePath,int ratio) throws IOException{
FileOutputStream out = null;
try{
//读入文件
File file = new File(srcImagePath);
//构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
//放大边长
BufferedImage tag = new BufferedImage(width * ratio, height * ratio, BufferedImage.TYPE_INT_RGB);
//绘制放大后的图片
tag.getGraphics().drawImage(src, 0, 0, width * ratio, height * ratio, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
/**
* 重置图形的边长大小
* @param srcImagePath
* @param toImagePath
* @param width
* @param height
* @throws IOException
*/
public static void resizeImage(String srcImagePath,String toImagePath,int width,int height) throws IOException{
FileOutputStream out = null;
try{
//读入文件
File file = new File(srcImagePath);
//构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
//放大边长
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//绘制放大后的图片
tag.getGraphics().drawImage(src, 0, 0, width, height, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
/**
* 横向拼接图片(两张)
* @param firstSrcImagePath 第一张图片的路径
* @param secondSrcImagePath 第二张图片的路径
* @param imageFormat 拼接生成图片的格式
* @param toPath 拼接生成图片的路径
*/
public static void joinImagesHorizontal(String firstSrcImagePath, String secondSrcImagePath,String imageFormat, String toPath){
//可以改进为将两张图缩放到同等高度之后进行拼接,等后续改进吧
try {
//读取第一张图片
File firstFile = new File(firstSrcImagePath);
BufferedImage firstImg = ImageIO.read(firstFile);
int firstWidth = firstImg.getWidth();
int firstHeight = firstImg.getHeight();
//从图片中读取RGB
int[] firstImgAry = new int[firstWidth*firstHeight];
firstImgAry = firstImg.getRGB(0,0,firstWidth,firstHeight,firstImgAry,0,firstWidth);
//对第二张图片做相同的处理
File secondFile = new File(secondSrcImagePath);
BufferedImage secondImg = ImageIO.read(secondFile);
int secondWidth = secondImg.getWidth();
int secondHeight = secondImg.getHeight();
//从图片中读取RGB
int[] secondImgAry = new int[secondWidth*secondHeight];
//secondImgAry = secondImg.getRGB(0,0,firstWidth,firstHeight,secondImgAry,0,firstWidth);
secondImgAry = secondImg.getRGB(0,0,secondWidth,secondHeight,secondImgAry,0,secondWidth);
//生成新图片
int thirdHeight = (firstHeight>secondHeight || firstHeight==secondHeight)?firstHeight:secondHeight;
//BufferedImage imageNew = new BufferedImage(firstWidth*2,firstHeight,BufferedImage.TYPE_INT_RGB);
BufferedImage imageNew = new BufferedImage(firstWidth+secondWidth,thirdHeight,BufferedImage.TYPE_INT_RGB);
imageNew.setRGB(0,0,firstWidth,firstHeight,firstImgAry,0,firstWidth);//设置左半部分的RGB
//imageNew.setRGB(firstWidth,0,firstWidth,firstHeight,secondImgAry,0,firstWidth);//设置右半部分的RGB
imageNew.setRGB(firstWidth,0,secondWidth,secondHeight,secondImgAry,0,secondWidth);//设置右半部分的RGB
File outFile = new File(toPath);
ImageIO.write(imageNew, imageFormat, outFile);//写图片
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 横向拼接一组(多张)图像
* @param pics 将要拼接的图像
* @param type 图像写入格式
* @param dst_pic 图像写入路径
* @return
*/
public static boolean joinImageListHorizontal(String[] pics, String type, String dst_pic) {
//可以改进为将该组图缩放到同等高度之后进行拼接,等后续改进吧
try{
int len = pics.length;
if (len < 1) {
System.out.println("pics len < 1");
return false;
}
File[] src = new File[len];
BufferedImage[] images = new BufferedImage[len];
int[][] imageArrays = new int[len][];
for (int i = 0; i < len; i++) {
src[i] = new File(pics[i]);
images[i] = ImageIO.read(src[i]);
int width = images[i].getWidth();
int height = images[i].getHeight();
imageArrays[i] = new int[width * height];// 从图片中读取RGB
imageArrays[i] = images[i].getRGB(0, 0, width, height, imageArrays[i], 0, width);
}
int dst_width = 0;
int dst_height = images[0].getHeight();
for (int i = 0; i < images.length; i++) {
dst_height = dst_height > images[i].getHeight() ? dst_height : images[i].getHeight();
dst_width += images[i].getWidth();
}
//System.out.println(dst_width);
//System.out.println(dst_height);
if (dst_height < 1) {
System.out.println("dst_height < 1");
return false;
}
//生成新图片
BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB);
int width_i = 0;
for (int i = 0; i < images.length; i++) {
ImageNew.setRGB(width_i, 0, images[i].getWidth(), dst_height, imageArrays[i], 0, images[i].getWidth());
width_i += images[i].getWidth();
}
File outFile = new File(dst_pic);
ImageIO.write(ImageNew, type, outFile);// 写图片
}catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 纵向拼接图片(两张)
* @param firstSrcImagePath 读取的第一张图片
* @param secondSrcImagePath 读取的第二张图片
* @param imageFormat 图片写入格式
* @param toPath 图片写入路径
*/
public static void joinImagesVertical(String firstSrcImagePath, String secondSrcImagePath,String imageFormat, String toPath){
//可以改进为将两张图缩放到同等宽度之后进行拼接,等后续改进吧
try {
//读取第一张图片
File fileOne = new File(firstSrcImagePath);
BufferedImage imageOne = ImageIO.read(fileOne);
int width = imageOne.getWidth();//图片宽度
int height = imageOne.getHeight();//图片高度
//从图片中读取RGB
int[] imageArrayOne = new int[width*height];
imageArrayOne = imageOne.getRGB(0,0,width,height,imageArrayOne,0,width);
//对第二张图片做相同的处理
File fileTwo = new File(secondSrcImagePath);
BufferedImage imageTwo = ImageIO.read(fileTwo);
int width2 = imageTwo.getWidth();
int height2 = imageTwo.getHeight();
int[] ImageArrayTwo = new int[width2*height2];
ImageArrayTwo = imageTwo.getRGB(0,0,width,height,ImageArrayTwo,0,width);
//ImageArrayTwo = imageTwo.getRGB(0,0,width2,height2,ImageArrayTwo,0,width2);
//生成新图片
//int width3 = (width>width2 || width==width2)?width:width2;
BufferedImage imageNew = new BufferedImage(width,height*2,BufferedImage.TYPE_INT_RGB);
//BufferedImage imageNew = new BufferedImage(width3,height+height2,BufferedImage.TYPE_INT_RGB);
imageNew.setRGB(0,0,width,height,imageArrayOne,0,width);//设置上半部分的RGB
imageNew.setRGB(0,height,width,height,ImageArrayTwo,0,width);//设置下半部分的RGB
//imageNew.setRGB(0,height,width2,height2,ImageArrayTwo,0,width2);//设置下半部分的RGB
File outFile = new File(toPath);
ImageIO.write(imageNew, imageFormat, outFile);//写图片
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 纵向拼接一组(多张)图像
* @param pics 将要拼接的图像数组
* @param type 写入图像类型
* @param dst_pic 写入图像路径
* @return
*/
public static boolean joinImageListVertical(String[] pics, String type, String dst_pic) {
//可以改进为将该组图缩放到同等宽度之后进行拼接,等后续改进吧
try {
int len = pics.length;
if (len < 1) {
System.out.println("pics len < 1");
return false;
}
File[] src = new File[len];
BufferedImage[] images = new BufferedImage[len];
int[][] imageArrays = new int[len][];
for (int i = 0; i < len; i++) {
//System.out.println(i);
src[i] = new File(pics[i]);
images[i] = ImageIO.read(src[i]);
int width = images[i].getWidth();
int height = images[i].getHeight();
imageArrays[i] = new int[width * height];// 从图片中读取RGB
imageArrays[i] = images[i].getRGB(0, 0, width, height, imageArrays[i], 0, width);
}
int dst_height = 0;
int dst_width = images[0].getWidth();
for (int i = 0; i < images.length; i++) {
dst_width = dst_width > images[i].getWidth() ? dst_width : images[i].getWidth();
dst_height += images[i].getHeight();
}
//System.out.println(dst_width);
//System.out.println(dst_height);
if (dst_height < 1) {
System.out.println("dst_height < 1");
return false;
}
//生成新图片
BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB);
int height_i = 0;
for (int i = 0; i < images.length; i++) {
ImageNew.setRGB(0, height_i, dst_width, images[i].getHeight(), imageArrays[i], 0, dst_width);
height_i += images[i].getHeight();
}
File outFile = new File(dst_pic);
ImageIO.write(ImageNew, type, outFile);// 写图片
}catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 合并图片(按指定初始x、y坐标将附加图片贴到底图之上)
* @param negativeImagePath 背景图片路径
* @param additionImagePath 附加图片路径
* @param x 附加图片的起始点x坐标
* @param y 附加图片的起始点y坐标
* @param toPath 图片写入路径
* @throws IOException
*/
public static void mergeBothImage(String negativeImagePath,String additionImagePath,int x,int y,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,x,y,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将一组图片一次性附加合并到底图上
* @param negativeImagePath 源图像(底图)路径
* @param additionImageList 附加图像信息列表
* @param imageFormat 图像写入格式
* @param toPath 图像写入路径
* @throws IOException
*/
public static void mergeImageList(String negativeImagePath,List additionImageList,String imageFormat, String toPath) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
BufferedImage image=ImageIO.read(is);
//Graphics g=image.getGraphics();
Graphics2D g = image.createGraphics();
BufferedImage image2 = null;
if(additionImageList != null){
for(int i=0;i<additionImageList.size();i++){
//解析附加图片信息:x坐标、 y坐标、 additionImagePath附加图片路径
//图片信息存储在一个数组中
String[] additionImageInfo = (String[]) additionImageList.get(i);
int x = Integer.parseInt(additionImageInfo[0]);
int y = Integer.parseInt(additionImageInfo[1]);
String additionImagePath = additionImageInfo[2];
//读取文件输入流,并合并图片
is2 = new FileInputStream(additionImagePath);
//System.out.println(x+" : "+y+" : "+additionImagePath);
image2 = ImageIO.read(is2);
g.drawImage(image2,x,y,null);
}
}
os = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, os);//写图片
//JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
//enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的指定位置
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @param position 合并位置
* @throws IOException
*/
public static void mergeBothImage(String negativeImagePath,String additionImagePath,String toPath, String position) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
mergePosition(image, image2, g, position);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 设置合并图片位置
* @param image 底图
* @param image2 附加图片
* @param g 图片对象
* @param position 附加位置
*/
private static void mergePosition(BufferedImage image,BufferedImage image2,Graphics g, String position){
if (position.equals("Topleftcorner")){//左上角
g.drawImage(image2,0,0,null);
}else if (position.equals("Toprightcorner")){//右上角
g.drawImage(image2,image.getWidth()-image2.getWidth(),0,null);
}else if (position.equals("Leftbottom")){//左下角
g.drawImage(image2,0,image.getHeight()-image2.getHeight(),null);
}else if (position.equals("Rightbottom")){//右下角
g.drawImage(image2,image.getWidth()-image2.getWidth(),image.getHeight()-image2.getHeight(),null);
}else if (position.equals("Center")){//正*
g.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,image.getHeight()/2-image2.getHeight()/2,null);
}else if (position.equals("Topcenter")){//上边*
g.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,0,null);
}else if (position.equals("Bottomcenter")){//下边*
g.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,image.getHeight()-image2.getHeight(),null);
}else if (position.equals("Leftcenter")){//左边*
g.drawImage(image2,0,image.getHeight()/2-image2.getHeight()/2,null);
}else if (position.equals("Rightcenter")){//右边*
g.drawImage(image2,image.getWidth()-image2.getWidth(),image.getHeight()/2-image2.getHeight()/2,null);
}
}
/**
* 将附加图片合并到底图的左上角
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public static void mergeBothImageTopleftcorner(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,0,0,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的右上角
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public static void mergeBothImageToprightcorner(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()-image2.getWidth(),0,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的左下角
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public static void mergeBothImageLeftbottom(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,0,image.getHeight()-image2.getHeight(),null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的右下角
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public static void mergeBothImageRightbottom(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()-image2.getWidth(),image.getHeight()-image2.getHeight(),null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的正*
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public static void mergeBothImageCenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,image.getHeight()/2-image2.getHeight()/2,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的上边*
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public static void mergeBothImageTopcenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,0,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的下边*
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public static void mergeBothImageBottomcenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()/2-image2.getWidth()/2,image.getHeight()-image2.getHeight(),null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的左边*
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public static void mergeBothImageLeftcenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,0,image.getHeight()/2-image2.getHeight()/2,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 将附加图片合并到底图的右边*
* @param negativeImagePath 底图路径
* @param additionImagePath 附加图片路径
* @param toPath 合成图片写入路径
* @throws IOException
*/
public static void mergeBothImageRightcenter(String negativeImagePath,String additionImagePath,String toPath ) throws IOException{
InputStream is= null;
InputStream is2= null;
OutputStream os = null;
try{
is=new FileInputStream(negativeImagePath);
is2=new FileInputStream(additionImagePath);
BufferedImage image=ImageIO.read(is);
BufferedImage image2=ImageIO.read(is2);
Graphics g=image.getGraphics();
g.drawImage(image2,image.getWidth()-image2.getWidth(),image.getHeight()/2-image2.getHeight()/2,null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
}catch(Exception e){
e.printStackTrace();
}finally{
if(os != null){
os.close();
}
if(is2 != null){
is2.close();
}
if(is != null){
is.close();
}
}
}
/**
* 图片灰化操作
* @param srcImage 读取图片路径
* @param toPath 写入灰化后的图片路径
* @param imageFormat 图片写入格式
*/
public static void grayImage(String srcImage,String toPath,String imageFormat){
try{
BufferedImage src = ImageIO.read(new File(srcImage));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
src = op.filter(src, null);
ImageIO.write(src, imageFormat, new File(toPath));
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 在源图片上设置水印文字
* @param srcImagePath 源图片路径
* @param alpha 透明度(0<alpha<1)
* @param font 字体(例如:宋体)
* @param fontStyle 字体格式(例如:普通样式--Font.PLAIN、粗体--Font.BOLD )
* @param fontSize 字体大小
* @param color 字体颜色(例如:黑色--Color.BLACK)
* @param inputWords 输入显示在图片上的文字
* @param x 文字显示起始的x坐标
* @param y 文字显示起始的y坐标
* @param imageFormat 写入图片格式(png/jpg等)
* @param toPath 写入图片路径
* @throws IOException
*/
public static void alphaWords2Image(String srcImagePath,float alpha,
String font,int fontStyle,int fontSize,Color color,
String inputWords,int x,int y,String imageFormat,String toPath) throws IOException{
FileOutputStream fos=null;
try {
BufferedImage image = ImageIO.read(new File(srcImagePath));
//创建java2D对象
Graphics2D g2d=image.createGraphics();
//用源图像填充背景
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null, null);
//设置透明度
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g2d.setComposite(ac);
//设置文字字体名称、样式、大小
g2d.setFont(new Font(font, fontStyle, fontSize));
g2d.setColor(color);//设置字体颜色
g2d.drawString(inputWords, x, y); //输入水印文字及其起始x、y坐标
g2d.dispose();
fos=new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
/**
* 在源图像上设置图片水印
* ---- 当alpha==1时文字不透明(和在图片上直接输入文字效果一样)
* @param srcImagePath 源图片路径
* @param appendImagePath 水印图片路径
* @param alpha 透明度
* @param x 水印图片的起始x坐标
* @param y 水印图片的起始y坐标
* @param width 水印图片的宽度
* @param height 水印图片的高度
* @param imageFormat 图像写入图片格式
* @param toPath 图像写入路径
* @throws IOException
*/
public static void alphaImage2Image(String srcImagePath,String appendImagePath,
float alpha,int x,int y,int width,int height,
String imageFormat,String toPath) throws IOException{
FileOutputStream fos = null;
try {
BufferedImage image = ImageIO.read(new File(srcImagePath));
//创建java2D对象
Graphics2D g2d=image.createGraphics();
//用源图像填充背景
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null, null);
//设置透明度
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g2d.setComposite(ac);
//设置水印图片的起始x/y坐标、宽度、高度
BufferedImage appendImage = ImageIO.read(new File(appendImagePath));
g2d.drawImage(appendImage, x, y, width, height, null, null);
g2d.dispose();
fos=new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
/**
* 画单点 ---- 实际上是画一个填充颜色的圆
* ---- 以指定点坐标为中心画一个小半径的圆形,并填充其颜色来充当点
* @param srcImagePath 源图片颜色
* @param x 点的x坐标
* @param y 点的y坐标
* @param width 填充的宽度
* @param height 填充的高度
* @param ovalColor 填充颜色
* @param imageFormat 写入图片格式
* @param toPath 写入路径
* @throws IOException
*/
public static void drawPoint(String srcImagePath,int x,int y,int width,int height,Color ovalColor,String imageFormat,String toPath) throws IOException{
FileOutputStream fos = null;
try {
//获取源图片
BufferedImage image = ImageIO.read(new File(srcImagePath));
//根据xy点坐标绘制连接线
Graphics2D g2d = image.createGraphics();
g2d.setColor(ovalColor);
//填充一个椭圆形
g2d.fillOval(x, y, width, height);
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
/**
* 画一组(多个)点---- 实际上是画一组(多个)填充颜色的圆
* ---- 以指定点坐标为中心画一个小半径的圆形,并填充其颜色来充当点
* @param srcImagePath 原图片路径
* @param pointList 点列表
* @param width 宽度
* @param height 高度
* @param ovalColor 填充颜色
* @param imageFormat 写入图片颜色
* @param toPath 写入路径
* @throws IOException
*/
public static void drawPoints(String srcImagePath,List pointList,int width,int height,Color ovalColor,String imageFormat,String toPath) throws IOException{
FileOutputStream fos = null;
try {
//获取源图片
BufferedImage image = ImageIO.read(new File(srcImagePath));
//根据xy点坐标绘制连接线
Graphics2D g2d = image.createGraphics();
g2d.setColor(ovalColor);
//填充一个椭圆形
if(pointList != null){
for(int i=0;i<pointList.size();i++){
Point point = (Point)pointList.get(i);
int x = (int) point.getX();
int y = (int) point.getY();
g2d.fillOval(x, y, width, height);
}
}
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
/**
* 画线段
* @param srcImagePath 源图片路径
* @param x1 第一个点x坐标
* @param y1 第一个点y坐标
* @param x2 第二个点x坐标
* @param y2 第二个点y坐标
* @param lineColor 线条颜色
* @param toPath 图像写入路径
* @param imageFormat 图像写入格式
* @throws IOException
*/
public static void drawLine(String srcImagePath,int x1,int y1,int x2,int y2, Color lineColor,String toPath,String imageFormat) throws IOException{
FileOutputStream fos = null;
try {
//获取源图片
BufferedImage image = ImageIO.read(new File(srcImagePath));
//根据xy点坐标绘制连接线
Graphics2D g2d = image.createGraphics();
g2d.setColor(lineColor);
g2d.drawLine( x1, y1, x2, y2);
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
/**
* 画折线 / 线段
* ---- 2个点即画线段,多个点画折线
* @param srcImagePath 源图片路径
* @param xPoints x坐标数组
* @param yPoints y坐标数组
* @param nPoints 点的数量
* @param lineColor 线条颜色
* @param toPath 图像写入路径
* @param imageFormat 图片写入格式
* @throws IOException
*/
public static void drawPolyline(String srcImagePath,int[] xPoints, int[] yPoints, int nPoints,Color lineColor,String toPath,String imageFormat) throws IOException{
FileOutputStream fos = null;
try {
//获取源图片
BufferedImage image = ImageIO.read(new File(srcImagePath));
//根据xy点坐标绘制连接线
Graphics2D g2d = image.createGraphics();
//设置线条颜色
g2d.setColor(lineColor);
g2d.drawPolyline(xPoints, yPoints, nPoints);
//图像写出路径
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
/**
* 绘制折线,并突出显示转折点
* @param srcImagePath 源图片路径
* @param xPoints x坐标数组
* @param yPoints y坐标数组
* @param nPoints 点的数量
* @param lineColor 连线颜色
* @param width 点的宽度
* @param height 点的高度
* @param ovalColor 点的填充颜色
* @param toPath 图像写入路径
* @param imageFormat 图像写入格式
* @throws IOException
*/
public static void drawPolylineShowPoints(String srcImagePath,int[] xPoints, int[] yPoints, int nPoints,Color lineColor,int width,int height,Color ovalColor,String toPath,String imageFormat) throws IOException{
FileOutputStream fos = null;
try {
//获取源图片
BufferedImage image = ImageIO.read(new File(srcImagePath));
//根据xy点坐标绘制连接线
Graphics2D g2d = image.createGraphics();
//设置线条颜色
g2d.setColor(lineColor);
//画线条
g2d.drawPolyline(xPoints, yPoints, nPoints);
//设置圆点颜色
g2d.setColor(ovalColor);
//画圆点
if(xPoints != null){
for(int i=0;i<xPoints.length;i++){
int x = xPoints[i];
int y = yPoints[i];
g2d.fillOval(x, y, width, height);
}
}
//图像写出路径
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
/**
* 绘制一个由 x 和 y 坐标数组定义的闭合多边形
* @param srcImagePath 源图片路径
* @param xPoints x坐标数组
* @param yPoints y坐标数组
* @param nPoints 坐标点的个数
* @param polygonColor 线条颜色
* @param imageFormat 图像写入格式
* @param toPath 图像写入路径
* @throws IOException
*/
public static void drawPolygon(String srcImagePath,int[] xPoints,int[] yPoints,int nPoints,Color polygonColor,String imageFormat,String toPath) throws IOException {
FileOutputStream fos = null;
try {
//获取图片
BufferedImage image = ImageIO.read(new File(srcImagePath));
//根据xy点坐标绘制闭合多边形
Graphics2D g2d = image.createGraphics();
g2d.setColor(polygonColor);
g2d.drawPolygon(xPoints, yPoints, nPoints);
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
g2d.dispose();
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
/**
* 绘制并填充多边形
* @param srcImagePath 源图像路径
* @param xPoints x坐标数组
* @param yPoints y坐标数组
* @param nPoints 坐标点个数
* @param polygonColor 多边形填充颜色
* @param alpha 多边形部分透明度
* @param imageFormat 写入图形格式
* @param toPath 写入图形路径
* @throws IOException
*/
public static void drawAndAlphaPolygon(String srcImagePath,int[] xPoints,int[] yPoints,int nPoints,Color polygonColor,float alpha,String imageFormat,String toPath) throws IOException{
FileOutputStream fos = null;
try {
//获取图片
BufferedImage image = ImageIO.read(new File(srcImagePath));
//根据xy点坐标绘制闭合多边形
Graphics2D g2d = image.createGraphics();
g2d.setColor(polygonColor);
//设置透明度
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g2d.setComposite(ac);
g2d.fillPolygon(xPoints, yPoints, nPoints);
fos = new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
g2d.dispose();
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
public static void main(String[] args)throws Exception{
String srcDir = "E:/test/fileSrc/";
String toDir = "E:/test/fileTo/";
/** 切图、缩放测试 */
//剪切图片
// String srcPath = srcDir+"c.jpg";
// String toPath = toDir+"c_cut.jpg";
// int x = 200;
// int y = 300;
// int width = 300;
// int height = 200;
// String readImageFormat = "jpg";
// String writeImageFormat = "jpg";
//ImageUtil.cutOutImage(srcPath, toPath, x, y, width, height, readImageFormat, writeImageFormat);
//按指定的长宽重置图形大小
// srcPath = srcDir+"f.jpg";
// toPath = toDir+"f_size.jpg";
// width = 400;
// height = 400;
//ImageUtil.resizeImage(srcPath, toPath, width, height);
//toPath = toDir+"f_size1.jpg";
//ImageUtil.zoomOutInImageBySize(srcPath, toPath, width, height);
//按指定长和宽的比例缩小图形
// toPath = toDir+"f_zoomIn.jpg";
// ImageUtil.reduceImageByRatio(srcPath, toPath, 3, 3);
// toPath = toDir+"f_zoomIn1.jpg";
// ImageUtil.zoomOutInImageByRatio(srcPath, toPath, 3, 3, true);
//按指定长和宽的比例放大图形
// srcPath = srcDir+"m2.png";
// toPath = toDir+"m2_zoomOut.png";
// ImageUtil.enlargementImageByRatio(srcPath, toPath, 2, 2);
// toPath = toDir+"m2_zoomOut1.png";
// ImageUtil.zoomOutInImageByRatio(srcPath, toPath, 2, 2, false);
//长高等比例缩小
// srcPath = srcDir+"f.jpg";
// toPath = toDir+"f_zoomInEqual.jpg";
// ImageUtil.reduceImageEqualProportion(srcPath, toPath, 4);
// toPath = toDir+"f_zoomInEqual1.jpg";
// ImageUtil.zoomOutInImageEqualProportion(srcPath, toPath, 4, true);
//长高等比例放大
// srcPath = srcDir+"m2.png";
// toPath = toDir+"m2_zoomOutEqual.png";
// ImageUtil.enlargementImageEqualProportion(srcPath, toPath, 2);
// toPath = toDir+"m2_zoomOutEqual1.png";
// ImageUtil.zoomOutInImageEqualProportion(srcPath, toPath, 2, false);
/** 合并图片测试 */
// String negativeImagePath = srcDir+"d.jpg";
// String additionImagePath = srcDir+"logo.png";
// int x = 200;
// int y = 200;
// String toPath = toDir+"d_merge_xy.jpg";
//
// //按指定坐标合并图片
// ImageUtil.mergeBothImage(negativeImagePath, additionImagePath, x, y, toPath);
// //合并到左上角
// toPath = toDir+"d_merge_Topleftcorner.jpg";
// ImageUtil.mergeBothImageTopleftcorner(negativeImagePath, additionImagePath, toPath);
// toPath = toDir+"d_merge_Topleftcorner1.jpg";
// ImageUtil.mergeBothImage(negativeImagePath, additionImagePath, toPath, "Topleftcorner");
// //合并到右上角
// toPath = toDir+"d_merge_Toprightcorner.jpg";
// ImageUtil.mergeBothImageToprightcorner(negativeImagePath, additionImagePath, toPath);
// toPath = toDir+"d_merge_Toprightcorner1.jpg";
// ImageUtil.mergeBothImage(negativeImagePath, additionImagePath, toPath, "Toprightcorner");
// //合并到左下角
// toPath = toDir+"d_merge_Leftbottom.jpg";
// ImageUtil.mergeBothImageLeftbottom(negativeImagePath, additionImagePath, toPath);
// toPath = toDir+"d_merge_Leftbottom1.jpg";
// ImageUtil.mergeBothImage(negativeImagePath, additionImagePath, toPath, "Leftbottom");
// //合并到右下角
// toPath = toDir+"d_merge_Rightbottom.jpg";
// ImageUtil.mergeBothImageRightbottom(negativeImagePath, additionImagePath, toPath);
// toPath = toDir+"d_merge_Rightbottom1.jpg";
// ImageUtil.mergeBothImage(negativeImagePath, additionImagePath, toPath, "Rightbottom");
// //合并到正*
// toPath = toDir+"d_merge_Center.jpg";
// ImageUtil.mergeBothImageCenter(negativeImagePath, additionImagePath, toPath);
// toPath = toDir+"d_merge_Center1.jpg";
// ImageUtil.mergeBothImage(negativeImagePath, additionImagePath, toPath, "Center");
// //合并到上边*
// toPath = toDir+"d_merge_Topcenter.jpg";
// ImageUtil.mergeBothImageTopcenter(negativeImagePath, additionImagePath, toPath);
// toPath = toDir+"d_merge_Topcenter1.jpg";
// ImageUtil.mergeBothImage(negativeImagePath, additionImagePath, toPath, "Topcenter");
// //合并到下边*
// toPath = toDir+"d_merge_Bottomcenter.jpg";
// ImageUtil.mergeBothImageBottomcenter(negativeImagePath, additionImagePath, toPath);
// toPath = toDir+"d_merge_Bottomcenter1.jpg";
// ImageUtil.mergeBothImage(negativeImagePath, additionImagePath, toPath, "Bottomcenter");
// //合并到左边*
// toPath = toDir+"d_merge_Leftcenter.jpg";
// ImageUtil.mergeBothImageLeftcenter(negativeImagePath, additionImagePath, toPath);
// toPath = toDir+"d_merge_Leftcenter1.jpg";
// ImageUtil.mergeBothImage(negativeImagePath, additionImagePath, toPath, "Leftcenter");
// //合并到右边*
// toPath = toDir+"d_merge_Rightcenter.jpg";
// ImageUtil.mergeBothImageRightcenter(negativeImagePath, additionImagePath, toPath);
// toPath = toDir+"d_merge_Rightcenter1.jpg";
// ImageUtil.mergeBothImage(negativeImagePath, additionImagePath, toPath, "Rightcenter");
/** 灰化图片测试 */
// String srcImage = srcDir+"m2.png";
// String toPath = toDir+"m2_gray.png";
// String imageFormat = "png";
// ImageUtil.grayImage(srcImage, toPath, imageFormat);
/** 拼接图片测试 */
//横向拼接两张图片
// String firstSrcImagePath = srcDir+"1.jpg";
// String secondSrcImagePath = srcDir+"2.jpg";
// String imageFormat = "jpg";
// String toPath = toDir+"12_join.jpg";
// ImageUtil.joinImagesHorizontal(firstSrcImagePath, secondSrcImagePath, imageFormat, toPath);
//横向拼接多张图片
// String[] pics = new String[]{srcDir+"1.jpg",srcDir+"1.jpg",srcDir+"1.jpg",srcDir+"1.jpg"};
//
// ImageUtil.joinImageListHorizontal(pics, "jpg", toDir+"1_join.jpg");
//纵向拼接两张图片
// String firstSrcImagePath = srcDir+"3.jpg";
// String secondSrcImagePath = srcDir+"4.jpg";
// String imageFormat = "jpg";
// String toPath = toDir+"34_join.jpg";
// ImageUtil.joinImagesVertical(firstSrcImagePath, secondSrcImagePath, imageFormat, toPath);
//纵向拼接多张图片
// String[] pics = new String[]{srcDir+"3.jpg",srcDir+"4.jpg",srcDir+"3.jpg",srcDir+"4.jpg"};
// ImageUtil.joinImageListVertical(pics, "jpg", toDir+"34_join.jpg");
/** 图片水印测试 */
// String srcImagePath = srcDir+"e.jpg";
// String appendImagePath = srcDir+"logo.png";
// float alpha = 0.2F;
// String font = "宋体";
// int fontStyle = Font.PLAIN;
// int fontSize = 32;
// Color color = Color.RED;
// String inputWords = "图片上设置水印文字 ---- 宋体 普通字体 32号字 红色 透明度0.5";
// int x = 20;
// int y = 40;
// String imageFormat = "jpg";
// String toPath = toDir+"alphaI2I-text.png";
// //设置文字水印
// ImageUtil.alphaWords2Image(srcImagePath, alpha, font, fontStyle, fontSize, color, inputWords, x, y, imageFormat, toPath);
// //设置图片水印
// toPath = toDir+"alphaI2I-img.png";
// ImageUtil.alphaImage2Image(srcImagePath, appendImagePath, alpha, x, y, 300, 200, imageFormat, toPath);
/** 图片绘制测试 */
//根据坐标数组绘制多边形
// String srcImagePath = srcDir+"2.jpg";
// int[] xPoints = {20,100,160,270,500};
// int[] yPoints = {30,150,172,295,615};
// int nPoints = 5;
// String toPath = toDir+"2_polygon.png";
// ImageUtil.drawPolygon(srcImagePath, xPoints, yPoints, nPoints, Color.MAGENTA, "jpg", toPath);
//画折线
// String srcImagePath = srcDir+"2.jpg";
// int[] xPoints = {100,150,200,240,300};
// int[] yPoints = {200,60,280,160,100};
// int nPoints = 5;
// Color lineColor = Color.RED;
// String toPath = toDir+"2_polyline.jpg";
// String imageFormat = "jpg";
// ImageUtil.drawPolyline(srcImagePath, xPoints, yPoints, nPoints, lineColor,toPath, imageFormat);
//画线段
// String srcImagePath = srcDir+"2.jpg";
// int x1 = 50;
// int y1 = 100;
// int x2 = 600;
// int y2 = 150;
// Color lineColor = Color.BLUE;
// String toPath = toDir+"2_drawline.jpg";
// String imageFormat = "jpg";
// ImageUtil.drawLine(srcImagePath, x1, y1, x2, y2, lineColor,toPath, imageFormat);
//画一个圆点
// String srcImagePath = srcDir+"2.jpg";
// int x = 100;
// int y = 200;
// int width = 10;
// int height = 10;
// Color ovalColor = Color.BLUE;
// String imageFormat = "jpg";
// String toPath = toDir+"2_drawPoint.jpg";
// ImageUtil.drawPoint(srcImagePath, x, y, width, height, ovalColor, imageFormat, toPath);
//画出一组(多个)点
// List pointList = new ArrayList();
// Point p1 = new Point(60,80);
// pointList.add(p1);
// Point p2 = new Point(160,80);
// pointList.add(p2);
// Point p3 = new Point(60,180);
// pointList.add(p3);
// Point p4 = new Point(260,180);
// pointList.add(p4);
// Point p5 = new Point(460,380);
// pointList.add(p5);
// String srcImagePath = srcDir+"2.jpg";
// int width = 10;
// int height = 10;
// Color ovalColor = Color.BLUE;
// String imageFormat = "jpg";
// String toPath = toDir+"2_drawPointList.jpg";
// ImageUtil.drawPoints(srcImagePath, pointList, width, height, ovalColor, imageFormat, toPath);
//画折线并突出显示点
// int[] xPoints = {50,100,180,400,600};
// int[] yPoints = {200,100,160,300,640};
// int width = 10;
// int height = 10;
// int nPoints = 5;
// Color ovalColor = Color.BLUE;
// Color lineColor = Color.RED;
// String imageFormat = "jpg";
// String srcImagePath = srcDir+"c.jpg";
// String toPath = toDir+"c_drawPolyPointList.jpg";
// ImageUtil.drawPolylineShowPoints(srcImagePath, xPoints, yPoints, nPoints, lineColor, width, height, ovalColor, toPath, imageFormat);
//绘制并填充多边形
// String srcImagePath = srcDir+"c.jpg";
// int[] xPoints ={50,90,180,320,640};
// int[] yPoints ={200,300,120,240,360};
// int nPoints = 5;
// Color polygonColor = Color.BLUE;
// float alpha = (float) 0.2;
// String imageFormat ="jpg";
// String toPath = toDir+"c_drawalpha.jpg";
// ImageUtil.drawAndAlphaPolygon(srcImagePath, xPoints, yPoints, nPoints, polygonColor, alpha, imageFormat, toPath);
/** 合并图片性能测试 */
//每次附加合并一张图片(循环若干次)
// String negativeImagePath = toDir+"c.jpg";
// String additionImagePath = srcDir+"logo.png";
//
// String toPath = toDir+"c.jpg";
// long start = System.currentTimeMillis();
// Random rand = new Random();
// for(int i=0;i<1000;i++){
// int x = rand.nextInt(1024);
// int y = rand.nextInt(768);
// ImageUtil.mergeBothImage(negativeImagePath, additionImagePath, x, y, toPath);
// }
// long end = System.currentTimeMillis();
// System.out.println(end-start);
//c.jpg文件大小:406K
//1000 -- 100369
/*改进思路:将mergeBothImage方法 修改为mergeImageList方法, 通过将图片的坐标点装入list容器,然后再取出一来在方法中一次性与图片合并,不再每次都打开底图、保存合成图片,关闭流*/
//叠加组合图像
// String negativeImagePath = toDir+"c.jpg";
// String additionImagePath = srcDir+"logo.png";
// String toPath = toDir+"c.jpg";
// List additionImageList = new ArrayList();
// int count = 0;
// Random rand = new Random();
// for(int i=0;i<10000;i++){
// int x = rand.nextInt(1020);
// int y = rand.nextInt(760);
// String[] str = {x+"",y +"",additionImagePath};
// additionImageList.add(str);
// count++;
// //System.out.println(x+" : "+y);
// }
// System.out.println(count);
// long start = System.currentTimeMillis();
// ImageUtil.mergeImageList(negativeImagePath, additionImageList,"jpg", toPath);
// long end = System.currentTimeMillis();
// System.out.println(end-start);
// 第一次 第二次 第二次
//100张耗时(毫秒) 515 499 515
//1000张耗时(毫秒) 2137 2153 2512
//10000张耗时(毫秒) 17796 17924 18002
//如果list.size()<=100,则调用此方法,
//如果list.size()>100,则调用Jmagick的方法。
//拼图测试
String imageFormat = "jpg";
srcDir = "E:/test/listSrc/";
String[] pics1 = {srcDir+"1.jpg",srcDir+"2.jpg",srcDir+"3.jpg",srcDir+"4.jpg",srcDir+"5.jpg",srcDir+"6.jpg",srcDir+"7.jpg",srcDir+"8.jpg",srcDir+"9.jpg",srcDir+"10.jpg",srcDir+"11.jpg"};
String[] pics2 = {srcDir+"1.jpg",srcDir+"2.jpg",srcDir+"3.jpg",srcDir+"4.jpg",srcDir+"5.jpg",srcDir+"6.jpg",srcDir+"7.jpg",srcDir+"8.jpg",srcDir+"9.jpg",srcDir+"10.jpg",srcDir+"11.jpg"};
String[] pics3 = {srcDir+"1.jpg",srcDir+"2.jpg",srcDir+"3.jpg",srcDir+"4.jpg",srcDir+"5.jpg",srcDir+"6.jpg",srcDir+"7.jpg",srcDir+"8.jpg",srcDir+"9.jpg",srcDir+"10.jpg",srcDir+"11.jpg"};
String[] pics4 = {srcDir+"1.jpg",srcDir+"2.jpg",srcDir+"3.jpg",srcDir+"4.jpg",srcDir+"5.jpg",srcDir+"6.jpg",srcDir+"7.jpg",srcDir+"8.jpg",srcDir+"9.jpg",srcDir+"10.jpg",srcDir+"11.jpg"};
String[] pics5 = {srcDir+"1.jpg",srcDir+"2.jpg",srcDir+"3.jpg",srcDir+"4.jpg",srcDir+"5.jpg",srcDir+"6.jpg",srcDir+"7.jpg",srcDir+"8.jpg",srcDir+"9.jpg",srcDir+"10.jpg",srcDir+"11.jpg"};
String[] pics6 = {srcDir+"1.jpg",srcDir+"2.jpg",srcDir+"3.jpg",srcDir+"4.jpg",srcDir+"5.jpg",srcDir+"6.jpg",srcDir+"7.jpg",srcDir+"8.jpg",srcDir+"9.jpg",srcDir+"10.jpg",srcDir+"11.jpg"};
String toPath1 = toDir+"v1.jpg";
String toPath2 = toDir+"v2.jpg";
String toPath3 = toDir+"v3.jpg";
String toPath4 = toDir+"v4.jpg";
String toPath5 = toDir+"v5.jpg";
String toPath6 = toDir+"v6.jpg";
String[] pics7 = {toPath1,toPath2,toPath3,toPath4,toPath5,toPath6};
String toPath7 = toDir+"h1.jpg";
long start = System.currentTimeMillis();
ImageUtil.joinImageListVertical(pics1, imageFormat, toPath1);
ImageUtil.joinImageListVertical(pics2, imageFormat, toPath2);
ImageUtil.joinImageListVertical(pics3, imageFormat, toPath3);
ImageUtil.joinImageListVertical(pics4, imageFormat, toPath4);
ImageUtil.joinImageListVertical(pics5, imageFormat, toPath5);
ImageUtil.joinImageListVertical(pics6, imageFormat, toPath6);
ImageUtil.joinImageListHorizontal(pics7, imageFormat, toPath7);
long end = System.currentTimeMillis();
System.out.println(end-start);
//数量 11 11x6
//11700
}
}
以上为个人对java ImageIO处理图形的一些常用的方法进行的封装。全部测试通过,main方法中的均为测试代码,如果想用这个图像处理类,可以参考mian方法的例子。
另外代码中有些地方需要改进,效率也需要进一步提高。