JAVA 批处理 调整、压缩png jpg图片大小

功能性代码,运行在控制台,批处理调整图片

package com.test.main;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

/**
 * Eclipse默认把这些受访问限制的API设成了ERROR。
 * 只要把Windows-Preferences-Java-Complicer-Errors/Warnings
 * 里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。
 */
import com.sun.image.codec.jpeg.JPEGCodec; 
import com.sun.image.codec.jpeg.JPEGEncodeParam;  
import com.sun.image.codec.jpeg.JPEGImageEncoder;


import javax.imageio.ImageIO;

public class ReSizePic {

	public static void main(String[] args) {

		ReSizePic resize = new ReSizePic();
		resize.changeByDir("D:/image", 0, 0, 0.5);
		resize.reSizeJpegByproportion("D:/image/1.jpg", "D:/image/2.jpg", 0.5);
	}
	
	public void changeByDir(String dirPath, int width, int height){
		changeByDir(dirPath, width, height, -1);
	}
	
	public void changeByDir(String dirPath, double proportion){
		changeByDir(dirPath, 0, 0, proportion);
	}

	/**
	 * 
	 * @param dirPath
	 * @param width
	 * @param height
	 * @param proportion 
	 */
	public void changeByDir(String dirPath, int width, int height,
			double proportion) {
		File dir = new File(dirPath);
		if (!dir.isDirectory()) {
			System.err.println("is not a dir");
			return;
		}
		File[] fileList = dir.listFiles();
		String outputFileDir = dir.getAbsolutePath() + "/out/";
		File outputFile = new File(outputFileDir);
		if (!outputFile.exists() && !outputFile.isDirectory()) {
			outputFile.mkdir();
		}
		boolean isProportion = false;
		if (proportion != -1) {
			isProportion = true;
		}
		for (int i = 0; i < fileList.length; i++) {
			File file = fileList[i];
			if (file.getName().endsWith(".png")) {
				if (isProportion) {
					reSizeByproportion(file.getAbsolutePath(), outputFileDir
							+ file.getName(), proportion);
				} else {

					reSizePicture(file.getAbsolutePath(),
							outputFileDir + file.getName(), width, height);
				}
			}else if(file.getName().endsWith("jpg")){
				if (isProportion) {
					reSizeJpegByproportion(file.getAbsolutePath(), 
							outputFileDir + file.getName(), proportion);
				} else {
					reSizeJepgPicture(file.getAbsolutePath(), 
							outputFileDir + file.getName(), width, height);
				}
				
			}
		}

	}

	public void reSizeByproportion(String fromFile, String toFile,
			double proportion) {
		try {
			File fromF = new File(fromFile);

			BufferedImage bi = ImageIO.read(fromF);
			int newWidth = 0, newHeight = 0;
			newWidth = (int) (((double) bi.getWidth()) * proportion);
			newHeight = (int) (((double) bi.getHeight()) * proportion);

			reSizePicture(fromFile, toFile, newWidth, newHeight);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public void reSizeJpegByproportion(String fromFile, String toFile,
			double proportion) {
		try {
			File fromF = new File(fromFile);

			BufferedImage bi = ImageIO.read(fromF);
			int newWidth = 0, newHeight = 0;
			newWidth = (int) (((double) bi.getWidth()) * proportion);
			newHeight = (int) (((double) bi.getHeight()) * proportion);

			reSizeJepgPicture(fromFile, toFile, newWidth, newHeight);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void reSizePicture(String fromFile, String toFile, int newWidth,
			int newHeight) {
		try {
			File fromF = new File(fromFile);
			BufferedImage bi = ImageIO.read(fromF);
			BufferedImage to = new BufferedImage(newWidth, newHeight,
					BufferedImage.TYPE_INT_BGR);
			Graphics2D g2d = to.createGraphics();
			to = g2d.getDeviceConfiguration().createCompatibleImage(newWidth,
					newHeight, Transparency.TRANSLUCENT);
			g2d.dispose();
			g2d = to.createGraphics();
			Image from = bi.getScaledInstance(newWidth, newHeight,
					BufferedImage.SCALE_AREA_AVERAGING);

			g2d.drawImage(from, 0, 0, null);
			g2d.dispose();

			ImageIO.write(to, "png", new File(toFile));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void reSizeJepgPicture(String fromFile, String toFile, int newWidth,
			int newHeight) {
		try {
			File fromF = new File(fromFile);
			BufferedImage bi = ImageIO.read(fromF);
			BufferedImage to = new BufferedImage(newWidth, newHeight,
					BufferedImage.TYPE_INT_BGR);
			to.getGraphics().drawImage(bi, 0, 0, newWidth, newHeight, null);
			FileOutputStream out = new FileOutputStream(toFile);  
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(to); 
            jep.setQuality(0.9f, true);
            encoder.encode(to,jep);
            
            out.close();
			bi.flush();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void reName() {
		File filePath = new File("D:/image");

		File[] fileList = filePath.listFiles();
		for (File file : fileList) {
			String fileName = file.getName();
			System.out.println("========" + fileName.replace(".png", ""));
			String newName = fileName.replace(".png", "");
			System.out.println(newName);
			if (newName.length() == 1) {
				newName = "000" + newName;
			} else if (newName.length() == 2) {
				newName = "00" + newName;
			} else if (newName.length() == 3) {
				newName = "0" + newName;
			}
			boolean bool = file.renameTo(new File("D:/image/" + newName
					+ ".png"));
			System.err.println(bool);

		}

	}
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值