java ImageIO處理圖像的封裝 .

java ImageIO處理圖像的封裝

package com.adam.dev.pic.easyImage;
import java.awt.AlphaComposite;
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.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

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;

/**
 * @author adam.胡昇陽
 * 創建日期 2012-2-29
 */
public class OperateImage{

    public OperateImage() {
		super();
	}

	/** 
	 * 對圖片裁剪,並把裁剪新圖片保存 
	 * @param srcPath 讀取源圖片路徑
	 * @param toPath	寫入圖片路徑
	 * @param x 剪切起始點x座標
	 * @param y 剪切起始點y座標
	 * @param width 剪切寬度
	 * @param height	 剪切高度
	 * @param readImageFormat  讀取圖片格式
	 * @param writeImageFormat 寫入圖片格式
	 * @throws IOException
	 */
    public void cropImage(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 srcImagePath 讀取圖片路徑
     * @param toImagePath 寫入圖片路徑
     * @param widthRatio	寬度縮小比例
     * @param heightRatio	 高度縮小比例
     * @throws IOException
     */
    public 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 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 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 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 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 void joinImagesHorizontal(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 height3 = (height>height2 || height==height2)?height:height2;
            BufferedImage  imageNew  =  new  BufferedImage(width*2,height,BufferedImage.TYPE_INT_RGB);    
            //BufferedImage  imageNew  =  new  BufferedImage(width+width2,height3,BufferedImage.TYPE_INT_RGB);    
            imageNew.setRGB(0,0,width,height,imageArrayOne,0,width);//設置左半部分的RGB  
            imageNew.setRGB(width,0,width,height,ImageArrayTwo,0,width);//設置右半部分的RGB 
            //imageNew.setRGB(width,0,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  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 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  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 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 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	合成圖片寫入路徑
     * @throws IOException
     */
    public 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 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 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 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 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 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 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 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 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 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 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 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 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 (IOException 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 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 (IOException 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 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 (IOException 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 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 (IOException 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 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 (IOException 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 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 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{
        OperateImage imageObj = new OperateImage();
        
        /*String srcPath = "D:/test/fileSource/004.jpg";
    	String toPath = "D:/test/desk/+e004.jpg";
    	int x = 200;
    	int y = 300;
    	int width = 300;
    	int height = 200 ;
    	String readImageFormat = "jpg";
    	String writeImageFormat = "jpg"*/;
        //imageObj.cropImage(srcPath, toPath, x, y, width, height,readImageFormat,writeImageFormat);//剪切圖片
        //imageObj.resizeImage(srcPath, toPath, 400, 400);//按指定的長寬重置圖形大小
       //imageObj.reduceImageByRatio(srcPath, toPath, 3, 3);//按指定長和寬的比例縮小圖形
       //imageObj.enlargementImageByRatio(srcPath, toPath, 2, 2);//按指定長和寬的比例放大圖形
       //imageObj.reduceImageEqualProportion(srcPath, toPath, 4);//長高等比例縮小
        //imageObj.enlargementImageEqualProportion(srcPath, toPath, 2);//長高等比例放大
       /* String negativeImagePath = "D:/test/fileSource/004.jpg";
        String additionImagePath = "D:/test/fileSource/005.jpg";
        int x = 200;
        int y = 200;
        String toPath = "D:/test/desk/004+005-rightcenter.jpg";*/
        //imageObj.mergeBothImage(negativeImagePath, additionImagePath, x, y, toPath); //按指定座標合併圖片
        //imageObj.mergeBothImageTopleftcorner(negativeImagePath, additionImagePath, toPath);//合併到左上角
        //imageObj.mergeBothImageToprightcorner(negativeImagePath, additionImagePath, toPath);//合併到右上角
        //imageObj.mergeBothImageLeftbottom(negativeImagePath, additionImagePath, toPath);//合併到左下角
        //imageObj.mergeBothImageRightbottom(negativeImagePath, additionImagePath, toPath);//合併到右下角
        //imageObj.mergeBothImageCenter(negativeImagePath, additionImagePath, toPath);//合併到正中央
        //imageObj.mergeBothImageTopcenter(negativeImagePath, additionImagePath, toPath);//合併到上邊中央
        //imageObj.mergeBothImageBottomcenter(negativeImagePath, additionImagePath, toPath);//合併到下邊中央
        //imageObj.mergeBothImageLeftcenter(negativeImagePath, additionImagePath, toPath);//合併到左邊中央
        //imageObj.mergeBothImageRightcenter(negativeImagePath, additionImagePath, toPath);//合併到右邊中央
    	
    	/*
    	String srcImage = "D:/test/fileSource/001.jpg";
    	String toPath = "D:/test/desk/001-gray.jpg";
    	String imageFormat = "jpg";
    	imageObj.grayImage(srcImage, toPath, imageFormat);//圖片灰化
    	 */    
    	
    	/*
    	String firstSrcImagePath = "D:/test/desk/003.jpg";
    	String secondSrcImagePath = "D:/test/desk/004.jpg";
    	String imageFormat = "jpg";
    	String toPath = "D:/test/desk/003-004-join.jpg";
    	imageObj.joinImagesHorizontal(firstSrcImagePath, secondSrcImagePath, imageFormat, toPath);//橫向拼接圖片
    	*/
    	
    	/*
    	String firstSrcImagePath = "D:/test/desk/001-002-join.jpg";
    	String secondSrcImagePath = "D:/test/desk/003-004-join.jpg";
    	String imageFormat = "jpg";
    	String toPath = "D:/test/desk/all-join.jpg";
    	imageObj.joinImagesVertical(firstSrcImagePath, secondSrcImagePath, imageFormat, toPath);//縱向拼接圖片
    	*/
    	
    	/*String srcImagePath = "D:/test/fileSource/002.jpg";
    	int[] xPoints = {20,100,160,270,500}; 
    	int[] yPoints = {30,150,172,295,615};
    	int nPoints = 5; 
    	String toPath = "D:/test/desk/polygon-002.png";
    	imageObj.drawPolygon(srcImagePath, xPoints, yPoints, nPoints, Color.MAGENTA, "jpg", toPath); //根據座標數組繪製多邊形
    	*/

    	/*String srcImagePath = "D:/test/fileSource/004.jpg";
    	String appendImagePath = "D:/test/fileSource/005.jpg";
    	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 = "D:/test/desk/alphaI2I-001.png";*/
    	//imageObj.alphaWords2Image(srcImagePath, alpha, font, fontStyle, fontSize, color, inputWords, x, y, imageFormat, toPath); //設置文字水印
    	//imageObj.alphaImage2Image(srcImagePath, appendImagePath, alpha, x, y, 300, 200, imageFormat, toPath);//設置圖片水印
    	
    	/*
    	String srcImagePath = "D:/test/fileSource/003.jpg";
    	int[] xPoints = {100,150,200,240,300};
    	int[] yPoints = {200,60,280,160,100};
    	int nPoints = 5;
    	Color lineColor = Color.RED;
    	String toPath = "D:/test/desk/polyline-003.jpg";
    	String imageFormat = "jpg";
    	imageObj.drawPolyline(srcImagePath, xPoints, yPoints, nPoints, lineColor,toPath, imageFormat);//畫折線
    	 */    	
    	
    	/*
    	int x1 = 50;
    	int y1 = 100;
    	int x2 = 600;
    	int y2 = 150;
    	Color lineColor = Color.BLUE;
    	imageObj.drawLine(srcImagePath, x1, y1, x2, y2, lineColor,toPath, imageFormat);//畫線段
    	 */    	
    	
    	/*
    	String srcImagePath = "D:/test/fileSource/002.jpg";
    	int x = 400;
    	int y = 500;
    	int width = 10;
    	int height = 10;
    	Color ovalColor = Color.RED;
    	String imageFormat = "jpg";
    	String toPath = "D:/test/desk/point-002.jpg";
    	imageObj.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 = "D:/test/fileSource/004.jpg";
    	int width = 10;
    	int height = 10;
    	Color ovalColor = Color.RED;
    	String imageFormat = "jpg";
    	String toPath = "D:/test/desk/points-004.jpg";
    	imageObj.drawPoints(srcImagePath, pointList, width, height, ovalColor, imageFormat, toPath);//畫出一組(多個)點
    	 */   
    	
    	/*
    	int[] xPoints = {50,100,180,400,600};
    	int[] yPoints = {200,100,160,300,640};
    	int nPoints = 5;
    	Color lineColor = Color.PINK;
    	String srcImagePath = "D:/test/fileSource/003.jpg";
    	String toPath = "D:/test/desk/showpoints-003.jpg";
    	imageObj.drawPolylineShowPoints(srcImagePath, xPoints, yPoints, nPoints, lineColor, width, height, ovalColor, toPath, imageFormat);//畫折線並突出顯示點
    	 */   
    	
    	/*
    	String srcImagePath ="D:/test/fileSource/004.jpg"; 
    	int[] xPoints ={50,90,180,320,640};
    	int[] yPoints ={200,300,120,240,360};
    	int nPoints = 5;
    	Color polygonColor = Color.PINK;
    	float alpha = (float) 0.2;
    	String imageFormat ="jpg";
    	String toPath ="D:/test/desk/drawalpha-004.jpg";
    	imageObj.drawAndAlphaPolygon(srcImagePath, xPoints, yPoints, nPoints, polygonColor, alpha, imageFormat, toPath);
    	*/
    	/*
    	String negativeImagePath = "D:/test/fileSource/001.jpg";
    	String additionImagePath = "D:/test/fileSource/006.png";
    	String  toPath = "D:/test/fileSource/001.jpg";
    	long start = System.currentTimeMillis();
    	for(int i=0;i<1000;i++){
    		Random rand = new Random();
    		int x = rand.nextInt(1024);
    		int y =  rand.nextInt(768);
    		imageObj.mergeBothImage(negativeImagePath, additionImagePath, x, y, toPath);//每次附加合併一張圖片(循環若干次)
    	}
    	long end = System.currentTimeMillis();
    	System.out.println(end-start);*/
    	//100 -- 45844
    	//1000 -- 411156
    	/*改進思路:將mergeBothImage方法 修改爲mergeImageList方法,
    	通過將圖片的座標點裝入list容器,然後再取出一來在方法中一次性與圖片合併,
    	不再每次都打開底圖、保存合成圖片,關閉流*/

    	//疊加組合圖像
    	/*String negativeImagePath = "D:/test/fileSource/001.jpg";
    	String  toPath = "D:/test/fileSource/001.jpg";
    	String additionImagePath = "D:/test/fileSource/007.png";
    	List additionImageList = new ArrayList();
    	int count = 0;
    	for(int i=0;i<100;i++){//爲什麼總是連續生成一樣的隨機數???
    		Random rand = new Random();
    		int x = rand.nextInt(1020);
    		String xStr = x+"";
    		int y =  rand.nextInt(760);
    		String yStr = y +"";
    		String[] str = {xStr,yStr,additionImagePath};
    		additionImageList.add(str);
    		count++;
    		//System.out.println(xStr+"   :     "+yStr);
    	}
    	System.out.println(count);
    	long start = System.currentTimeMillis();
    	imageObj.mergeImageList(negativeImagePath, additionImageList,"jpg", toPath);
    	long end = System.currentTimeMillis();
    	System.out.println(end-start);*/
    	//                                第一次        第二次      第三次
    	//100張耗時(毫秒)		--2003			1792			1869           1747        	1871        	1793
    	//1000張耗時(毫秒)	--15334			15200		15236         15903			16028		15545
    	//10000張耗時(毫秒)	--153010		153340 		152673       154978  		156506 		154854                               
    	//如果list.size()<=100,則調用此方法,
    	//如果list.size()>100,則調用Jmagick的方法。
    	
    	/*List iamgePathList = new ArrayList();		// D:/test/16a/
    	iamgePathList.add("D:/test/16a/12384_2492.jpg");
    	iamgePathList.add("D:/test/16a/12384_2493.jpg");
    	iamgePathList.add("D:/test/16a/12384_2494.jpg");
    	iamgePathList.add("D:/test/16a/12384_2495.jpg");
    	iamgePathList.add("D:/test/16a/12384_2496.jpg");
    	iamgePathList.add("D:/test/16a/12384_2497.jpg");
    	iamgePathList.add("D:/test/16a/12384_2498.jpg");
    	iamgePathList.add("D:/test/16a/12384_2499.jpg");
    	iamgePathList.add("D:/test/16a/12384_2500.jpg");
    	iamgePathList.add("D:/test/16a/12384_2501.jpg");
    	iamgePathList.add("D:/test/16a/12384_2502.jpg");*/
    	//String imageFormat = "jpg";
    	//String toPath = "D:/test/desk/16a_v1.jpg";
    	//imageObj.joinImageListHorizontal(iamgePathList, imageFormat, toPath);
    	
    	/*String imageFormat = "jpg";
    	String[] pics1 = {"D:/test/16a/12384_2502.jpg","D:/test/16a/12384_2501.jpg",
    			"D:/test/16a/12384_2500.jpg","D:/test/16a/12384_2499.jpg","D:/test/16a/12384_2498.jpg",
    			"D:/test/16a/12384_2497.jpg","D:/test/16a/12384_2496.jpg","D:/test/16a/12384_2495.jpg",
    			"D:/test/16a/12384_2494.jpg","D:/test/16a/12384_2493.jpg","D:/test/16a/12384_2492.jpg"};
    	
    	String[] pics2 = {"D:/test/16a/12385_2502.jpg","D:/test/16a/12385_2501.jpg",
    			"D:/test/16a/12385_2500.jpg","D:/test/16a/12385_2499.jpg","D:/test/16a/12385_2498.jpg",
    			"D:/test/16a/12385_2497.jpg","D:/test/16a/12385_2496.jpg","D:/test/16a/12385_2495.jpg",
    			"D:/test/16a/12385_2494.jpg","D:/test/16a/12385_2493.jpg","D:/test/16a/12385_2492.jpg"};
    	
    	String[] pics3 = {"D:/test/16a/12386_2502.jpg","D:/test/16a/12386_2501.jpg",
    			"D:/test/16a/12386_2500.jpg","D:/test/16a/12386_2499.jpg","D:/test/16a/12386_2498.jpg",
    			"D:/test/16a/12386_2497.jpg","D:/test/16a/12386_2496.jpg","D:/test/16a/12386_2495.jpg",
    			"D:/test/16a/12386_2494.jpg","D:/test/16a/12386_2493.jpg","D:/test/16a/12386_2492.jpg"};
    	
    	String[] pics4 = {"D:/test/16a/12387_2502.jpg","D:/test/16a/12387_2501.jpg",
    			"D:/test/16a/12387_2500.jpg","D:/test/16a/12387_2499.jpg","D:/test/16a/12387_2498.jpg",
    			"D:/test/16a/12387_2497.jpg","D:/test/16a/12387_2496.jpg","D:/test/16a/12387_2495.jpg",
    			"D:/test/16a/12387_2494.jpg","D:/test/16a/12387_2493.jpg","D:/test/16a/12387_2492.jpg"};
    	
    	String[] pics5 = {"D:/test/16a/12388_2502.jpg","D:/test/16a/12388_2501.jpg",
    			"D:/test/16a/12388_2500.jpg","D:/test/16a/12388_2499.jpg","D:/test/16a/12388_2498.jpg",
    			"D:/test/16a/12388_2497.jpg","D:/test/16a/12388_2496.jpg","D:/test/16a/12388_2495.jpg",
    			"D:/test/16a/12388_2494.jpg","D:/test/16a/12388_2493.jpg","D:/test/16a/12388_2492.jpg"};
    	
    	String[] pics6 = {"D:/test/16a/12389_2502.jpg","D:/test/16a/12389_2501.jpg",
    			"D:/test/16a/12389_2500.jpg","D:/test/16a/12389_2499.jpg","D:/test/16a/12389_2498.jpg",
    			"D:/test/16a/12389_2497.jpg","D:/test/16a/12389_2496.jpg","D:/test/16a/12389_2495.jpg",
    			"D:/test/16a/12389_2494.jpg","D:/test/16a/12389_2493.jpg","D:/test/16a/12389_2492.jpg"};
    	
    	String toPath1 = "D:/test/desk/16a_v1.jpg";
    	String toPath2 = "D:/test/desk/16a_v2.jpg";
    	String toPath3 = "D:/test/desk/16a_v3.jpg";
    	String toPath4 = "D:/test/desk/16a_v4.jpg";
    	String toPath5 = "D:/test/desk/16a_v5.jpg";
    	String toPath6 = "D:/test/desk/16a_v6.jpg";
    	
    	String[] pics7 = {toPath1,toPath2,toPath3,toPath4,toPath5,toPath6};
    	String toPath7 = "D:/test/desk/16a_h1.jpg";
    	
    	long start = System.currentTimeMillis();
    	imageObj.joinImageListVertical(pics1, imageFormat, toPath1);
    	imageObj.joinImageListVertical(pics2, imageFormat, toPath2);
    	imageObj.joinImageListVertical(pics3, imageFormat, toPath3);
    	imageObj.joinImageListVertical(pics4, imageFormat, toPath4);
    	imageObj.joinImageListVertical(pics5, imageFormat, toPath5);
    	imageObj.joinImageListVertical(pics6, imageFormat, toPath6);
    	
    	imageObj.joinImageListHorizontal(pics7, imageFormat, toPath7);
    	long end = System.currentTimeMillis();
    	System.out.println(end-start);*/
    	
    	String str = "北京\n上海\n廣州\n深圳";
    	System.out.println(str);
    	String path = "c:/relevantdata.txt";
    	FileOutputStream fops = new FileOutputStream(path);
    	fops.write(str.getBytes());
    	
    	BufferedReader inputStream = new BufferedReader(new FileReader(new File(path)));
    	String mrMsg = "";
    	while((mrMsg = inputStream.readLine())!=null){
    	System.out.println(mrMsg);
    	}
    	}
    	//數量		11			11x6
    	//縱向		375		
    	//橫向		391		3250
}

原文章地址:http://blog.csdn.net/hu_shengyang/article/details/7433988
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章