批量修改图片大小

1,既然是批量,肯定图片的名称要有规律可循,建议数字排练(如1.jpg、2.jpg......)

2,原理很简单,从文件夹循环读取图片,修改后保存

 

 

 

import java.io.*;

import java.awt.*;

import java.awt.image.*;

import com.sun.image.codec.jpeg.*;

import javax.imageio.ImageIO;

 

public static void jpg_mini(String fn, int factor) throws Exception {

String f0 = fn + ".jpg";

File f = new File(f0); // 读入文件

Image src = ImageIO.read(f); // 构造Image对象

int w0 = src.getWidth(null); // 得到源图宽

int h0 = src.getHeight(null); // 得到源图长

 

String f2 = fn + ".jpg";

int w2 = w0 / factor;

int h2 = h0 / factor;

BufferedImage tag = new BufferedImage(w2, h2,

BufferedImage.TYPE_INT_RGB);

tag.getGraphics().drawImage(src, 0, 0, w2, h2, null); // 绘制缩小后的图

System.out.println(f0 + "(" + w0 + "*" + h0 + ") \t=> " + f2 + "(" + w2 + "*" + h2 + ")");

// 保存文件

FileOutputStream out = new FileOutputStream(f2); // 输出到文件流

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(tag); // 近JPEG编码

out.close();

}

 

 

       public static void jpg_mini(int factor, String fx, int idx0, int idx2,

int iLen) throws Exception {

String fn = null;

for (int i = idx0; i <= idx2; i++) {

fn = fx + zeroInt(i, iLen); // 长度4 如:DSC_0168.JPG

try {

jpg_mini(fn, factor);

} catch (Exception e) {

System.out.println(fn + "..." + e);

}

}

}

 

         public static void main(String args[]) throws Exception {

jpg_mini(3, "D:/store/", 1,15, 2); // 缩小2倍(1-15)

}

你可能感兴趣的:(F#,sun)