package nowcoder.pictureZoom;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageOperater {
    
    /**
     * 左上方
     */
    public static final int LEFT_UPPER = 1;
    /**
     * 右上方
     */
    public static final int RIGHT_UPPER = 2;
    /**
     * 左下方
     */
    public static final int LEFT_LOWER = 3;
    /**
     * 右下方
     */
    public static final int RIGHT_LOWER = 4;
    /**
     * 中间
     */
    public static final int CENTER = 5;
    
    private BufferedImage image;
    private String path;
    private String type;
    
    /**
     * 用File构造一个ImageOperater对象
     * @param file  图像文File对象
     */
    public ImageOperater(File file) {
        try {
            this.image = ImageIO.read(file);
            this.path = file.getAbsolutePath();
            this.type = getType(this.path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 用路径构造一个ImageOperater对象
     * @param path  图像文件路径
     */
    public ImageOperater(String path) {
        this.path = path;
        readImage(this.path);
        this.type = getType(this.path);
    }
    
    /**
     * 使用路径构造BufferedImage对象
     * @param path 图像文件路径
     */
    private void readImage(String path){
        try {
            this.image = ImageIO.read(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 获取文件类型
     * @param path
     * @return
     */
    private String getType(String path){
        return path.substring(path.lastIndexOf(".")+1);
    }
    
    /**
     * 等比放大图片
     */
    public ImageOperater blowUpImageEqualProportion(int ratio){
        //构造一个按原图放大的图片
        BufferedImage image = new BufferedImage(this.image.getWidth()*ratio, 
                this.image.getHeight()*ratio,
                BufferedImage.TYPE_INT_RGB);
        //把原图画到构造的图片上
        image.getGraphics().drawImage(this.image,0,0,
                this.image.getWidth()*ratio, 
                this.image.getHeight()*ratio,
                null);
        this.image = image;
        //返回ImageOperater对象,以便链式处理图片
        return this;
    }
    
    /**
     * 等比放大图片
     */
    public ImageOperater reduceEqualProportion(int ratio){
        //构造一个按原图缩放的图片
        BufferedImage image = new BufferedImage(this.image.getWidth()/ratio, 
                this.image.getHeight()/ratio,
                BufferedImage.TYPE_INT_RGB);
        //把原图画到构造的图片上
        image.getGraphics().drawImage(this.image,0,0,this.image.getWidth()/ratio, 
                this.image.getHeight()/ratio,
                null);
        this.image = image;
        //返回ImageOperater对象,以便链式处理图片
        return this;
    }
    
    /**
     * 缩放到指定大小
     */
    public ImageOperater cutNotEqualProportion(int width,int height){
        //构造一个指定大小的图片
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        //把原图画到构造的图片上
        image.getGraphics().drawImage(this.image,0,0,width, height,null);
        this.image = image;
        //返回ImageOperater对象,以便链式处理图片
        return this;
    }
    
    /**
     * 添加水印
     * @param waterMarkFile 水印文件
     * @param POSITION 添加水印的位置
     * @return
     */
    public ImageOperater addWatermark(File waterMarkFile,int POSITION){
        BufferedImage waterMark = null;
        try {
            waterMark = ImageIO.read(waterMarkFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        //构造一个和原图一样大小的图片
        BufferedImage image = new BufferedImage(this.image.getWidth(), 
                this.image.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        //把原图画到构造的图片上
        image.getGraphics().drawImage(this.image,0,0,this.image.getWidth(), 
                this.image.getHeight(),
                null);
        
        switch (POSITION) {
        case LEFT_UPPER:
            image.getGraphics().drawImage(waterMark,0,0,
                    waterMark.getWidth(), 
                    waterMark.getHeight(),null);
            break;
        case RIGHT_UPPER:
            image.getGraphics().drawImage(waterMark,image.getWidth()-waterMark.getWidth(),0,
                    waterMark.getWidth(), 
                    waterMark.getHeight(),null);
            break;
        case LEFT_LOWER:
            image.getGraphics().drawImage(waterMark,0,image.getHeight()-waterMark.getHeight(),
                    waterMark.getWidth(), 
                    waterMark.getHeight(),null);
            break;
        case RIGHT_LOWER:
            image.getGraphics().drawImage(waterMark,image.getWidth()-waterMark.getWidth(),
                    image.getHeight()-waterMark.getHeight(),
                    waterMark.getWidth(), 
                    waterMark.getHeight(),null);
            break;
        case CENTER:
            image.getGraphics().drawImage(waterMark,(image.getWidth()-waterMark.getWidth())/2,
                    (image.getHeight()-waterMark.getHeight())/2,
                    waterMark.getWidth(), 
                    waterMark.getHeight(),null);
            break;
        default:
            throw new RuntimeException("指定位置不正确!");
        }
        this.image = image;
        return this;
    }  
    
    /**
     * 覆盖原图保存
     */
    public ImageOperater save(){
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(path));
            //保存文件
            ImageIO.write(image, this.type, fos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //关闭流
        try {
            if(fos!=null){
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }
    
    /**
     * 保存到指定路径
     * @param path
     */
    public ImageOperater save(String path,String name){
        FileOutputStream fos = null;
        File file = new File(path);
        //如果路径不存在,尝试创建文件夹
        if(!file.exists()){
            file.mkdirs();
        }
        //如果路径不是文件则抛出异常
        if(file.isFile()){
            throw new RuntimeException("指定路径不是文件夹!");
        }
        file = new File(file, name+"."+type);
        try {
            fos = new FileOutputStream(file);
            ImageIO.write(image, this.type, fos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //关闭流
        try {
            if(fos!=null){
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }
    
    /**
     * 保存到指定路径(绝对路径,相对路径)
     * @param path
     */
    public ImageOperater save(String path,String relativePath,String name){
        return save(path+File.pathSeparator+relativePath,name);
    }
    
    /**
     * 获取BufferedImage对象
     * @return
     */
    public BufferedImage getImage(){
        return this.image;
    }
    
    /**
     * 获取图片绝对路径
     * @return
     */
    public String getAbsolutePath(){
        return this.path;
    }
    
    /**
     * 获取文件类型
     * @return
     */
    public String getImageType(){
        return this.type;
    }
    
}

Client:

package nowcoder.pictureZoom;

import java.io.File;

public class Client {
    public static void main(String[] args) {
        File file = new File("D:/0.jpg");
        File watermark = new File("D:/1.png");
        ImageOperater operater = new ImageOperater(file);
        operater.reduceEqualProportion(3)
                //缩小到三分之一并保存
                .save("D:","1")
                //在右上方加上水印并保存
                .addWatermark(watermark, ImageOperater.RIGHT_UPPER)
                
                .save("D:","2")
                //下面做个骰子
                .addWatermark(watermark, ImageOperater.LEFT_UPPER)
                .addWatermark(watermark, ImageOperater.LEFT_LOWER)
                .addWatermark(watermark, ImageOperater.RIGHT_LOWER)
                .addWatermark(watermark, ImageOperater.CENTER)
                .save("D:","3");
        
        operater = new ImageOperater(file);
        //当然你也可以按照自己的尺寸剪裁
        operater.cutNotEqualProportion(200, 300).save("D:", "cut");
        
        File folder = new File("D:/pic");
        Batch(folder, 200, 200);
    }
	
	public static void Batch(File file,int weidth,int heigth){
		if(!file.isDirectory()){
			return;
		}
		
		for(File f:file.listFiles()){
			if(f.isFile()){
				ImageOperater operater = new ImageOperater(f);
				operater.cutNotEqualProportion(weidth, heigth).save();
			}else{
				//递归处理
				Batch(f,weidth,heigth);
			}
		}
	}
} 
原始文件:



处理后:




这次顺便也把之前一期给头像加个1也一起做了