很久没有写BLOG了,贴一段java程序


这是一段用于压缩指定目录下图片的java代码,供大师参考:
这个程序由3个java类文件组成:
第1个类是程序进入的入口(zipjpg)


package zipjpg;

import java.io.File;
import java.io.FileWriter;
import java.util.Calendar;

/**
*
* @author zhaowenbing
*/
public class Zipjpg {

/**这是主类,主要在main方法中开始压缩图片
* @param args the command line arguments
*/
public static void main(String[] args) {
String CONT_END = "\n";
String path = null;
if (args.length == 0) {
System.out.println("请输入路径参数");
return;
}
path = args[0];

//创建日志文件
File logfile = new File("./ZipJPG.log");
FileWriter logfw = null;
StringBuffer logcontent = new StringBuffer();
try {
logfw = new FileWriter(logfile,true);
} catch (Exception e) {
System.out.println("创建日志文件失败,失败缘由:" + e.getMessage());
return;
}

//获取当前时间
Calendar calendar = Calendar.getInstance();
logcontent.append("========程序处理时间:");
logcontent.append(calendar.get(Calendar.YEAR));
logcontent.append("-");
logcontent.append(calendar.get(Calendar.MONTH));
logcontent.append("-");
logcontent.append(calendar.get(Calendar.DAY_OF_MONTH));
logcontent.append(" ");
logcontent.append(calendar.get(Calendar.HOUR_OF_DAY));
logcontent.append(":");
logcontent.append(calendar.get(Calendar.MINUTE));
logcontent.append(":");
logcontent.append(calendar.get(Calendar.SECOND));
logcontent.append(":");
logcontent.append(calendar.get(Calendar.MILLISECOND));
logcontent.append(CONT_END);

logcontent.append("读取压缩目录路径:");
logcontent.append(path);
logcontent.append(CONT_END);

File file_list = null;
file_list = new File(path);

File[] ary_files = file_list.listFiles(new FileNameSelector("jpg"));
int files_count = ary_files.length;

logcontent.append("读取到文件数量:");
logcontent.append(files_count);
logcontent.append(CONT_END);

for (int i = 0; i < files_count; i++) {
File file_obj = ary_files[i];
if (file_obj.isFile()) {
logcontent.append("读取文件名:");
logcontent.append(file_obj.getPath());
logcontent.append(CONT_END);

//此处处理压缩图片
String compress_res = null;
try {
compress_res = PicCompression.doCompress(file_obj, 180, 180, 1, false);
} catch (Exception e) {
logcontent.append(" 压缩图片文件发生错误,错误缘由:");
logcontent.append(e.getMessage());
logcontent.append(CONT_END);
}

logcontent.append(compress_res);
logcontent.append(CONT_END);


}
}

//记录结束时间
Calendar calendar2 = Calendar.getInstance();
logcontent.append("========程序完成时间:");
logcontent.append(calendar2.get(Calendar.YEAR));
logcontent.append("-");
logcontent.append(calendar2.get(Calendar.MONTH));
logcontent.append("-");
logcontent.append(calendar2.get(Calendar.DAY_OF_MONTH));
logcontent.append(" ");
logcontent.append(calendar2.get(Calendar.HOUR_OF_DAY));
logcontent.append(":");
logcontent.append(calendar2.get(Calendar.MINUTE));
logcontent.append(":");
logcontent.append(calendar2.get(Calendar.SECOND));
logcontent.append(":");
logcontent.append(calendar2.get(Calendar.MILLISECOND));

long endlong=calendar2.getTimeInMillis();
long startlong=calendar.getTimeInMillis();
logcontent.append(" >>>耗时:");
logcontent.append(endlong-startlong);

logcontent.append(CONT_END);
logcontent.append(CONT_END);
logcontent.append(CONT_END);

//此处输出日志文件
try {
logfw.write(logcontent.toString());
} catch (Exception e) {
System.out.println("写日志文件失败,失败缘由:" + e.getMessage());
}
try {
logfw.close();
} catch (Exception e) {
System.out.println("写日志文件失败,失败缘由:" + e.getMessage());
}
logfile = null;
}
}


第2个类:这个类主要是参考网上其他人写法,几乎没有改动,主要用于过滤文件。

package zipjpg;

import java.io.File;
import java.io.FilenameFilter;

/**
*
* @author zhaowenbing
*/
public class FileNameSelector implements FilenameFilter {

String extension = ".";

public FileNameSelector(String fileExtensionNoDot) {
extension += fileExtensionNoDot;
}

@Override
public boolean accept(File dir, String name) {
return name.endsWith(extension);
}
}


第3个类:这个类主要压缩文件。在参考其他人写法的基础上略微做了调整。

package zipjpg;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;

/**
*
* @author zhaowenbing
*/
public class PicCompression {

public static String doCompress(File f, int width, int height, float quality, boolean percentage) throws Exception {
//读取图片信息
Image srcFile = null;
int new_w = width;
int new_h = height;
srcFile = ImageIO.read(f);
int oldwidth=srcFile.getWidth(null);
int oldheight=srcFile.getHeight(null);

StringBuffer sb=new StringBuffer();
sb.append(" >>>>>原图尺寸大小:");
sb.append(oldwidth);
sb.append("*");
sb.append(oldheight);
sb.append("(");
sb.append(f.length());
sb.append(")");

if((oldwidth<=180) &&(oldheight<=180)){
sb.append(" 此图片大小符合要求,不进行处理。");
return sb.toString();
}

if (percentage) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) srcFile.getWidth(null)) / (double) width + 0.1;
double rate2 = ((double) srcFile.getHeight(null)) / (double) height + 0.1;
double rate = rate1 > rate2 ? rate1 : rate2;
new_w = (int) (((double) srcFile.getWidth(null)) / rate);
new_h = (int) (((double) srcFile.getHeight(null)) / rate);
}

// 宽高设定
BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, new_w, new_h, null);

String newImage = f.getPath();
qq FileOutputStream out = new FileOutputStream(newImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);

//设置压缩质量
jep.setQuality(quality, true);
encoder.encode(tag, jep);
out.close();

srcFile.flush();

srcFile = ImageIO.read(f);
sb.append(" 压缩后图片尺寸大小:");
sb.append(srcFile.getWidth(null));
sb.append("*");
sb.append(srcFile.getHeight(null));
sb.append("(");
sb.append(f.length());
sb.append(")");

return sb.toString();
}
}


以上代码经过测试,可以运行和压缩。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值