01: package br.com.igor.testes;
02:
03: import java.awt.Image;
04: import java.awt.image.BufferedImage;
05: import java.io.File;
06: import java.io.FileInputStream;
07: import java.io.FileNotFoundException;
08: import java.io.FileOutputStream;
09: import java.io.IOException;
10:
11: import javax.imageio.ImageIO;
12: import javax.swing.ImageIcon;
13:
14: import com.sun.image.codec.jpeg.JPEGCodec;
15: import com.sun.image.codec.jpeg.JPEGEncodeParam;
16: import com.sun.image.codec.jpeg.JPEGImageDecoder;
17: import com.sun.image.codec.jpeg.JPEGImageEncoder;
18:
19: public class CompactadorDeImagem {
20:
21: /**
22: * @param args
23: */
24: public static void main(String[] args) {
25: CompactadorDeImagem compactadorDeFotos = new CompactadorDeImagem();
26: compactadorDeFotos.compressDir("/home/igor/Documents/Fotos/");
27: }
28:
29: private void compressDir(String dir) {
30: File comp = new File(dir + File.separator + "compactadas");
31: if (!comp.exists())
32: comp.mkdirs();
33:
34: File[] imagens = getImages(dir);
35: for (int i = 0; i < imagens.length; i++) {
36: if (imagens[i].getAbsolutePath().indexOf("compactadas") != -1)
37: continue;
38: if (imagens[i].isDirectory())
39: compressDir(imagens[i].getAbsolutePath());
40: try {
41: System.out.println("Compactando imagem "
42: + imagens[i].getName());
43: writeImgToDisk(imagens[i].getAbsoluteFile(), dir,
44: imagens[i].getName());
45: } catch (FileNotFoundException e) {
46: // TODO Auto-generated catch block
47: e.printStackTrace();
48: } catch (IOException e) {
49: // TODO Auto-generated catch block
50: e.printStackTrace();
51: }
52: }
53: }
54:
55: private Image loadScaledImg(String img)
56: throws FileNotFoundException, IOException {
57: System.out.println("Fazendo escala da imagem " + img);
58: return new ImageIcon(img).getImage().getScaledInstance(800,
59: 600, Image.SCALE_SMOOTH);
60: }
61:
62: private File[] getImages(String dir) {
63: return new File(dir).listFiles();
64: }
65:
66: private void writeImgToDisk(File imagem, String dir, String nome)
67: throws IOException {
68: long start = System.currentTimeMillis();
69: String dirDestino = dir + File.separator + "compactadas"
70: + File.separator + nome;
71: if ((new File(dirDestino)).exists()) {
72: System.out.println("Esta imagem ja foi compactada.");
73: return;
74: }
75: try {
76: FileInputStream fis = new FileInputStream(imagem
77: .getAbsolutePath());
78: JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fis);
79: BufferedImage origem = decoder.decodeAsBufferedImage();
80: fis.close();
81: BufferedImage img = new BufferedImage(800, 600, 4);
82: img.createGraphics().drawImage(
83: origem.getScaledInstance(800, 600, 4), null, null);
84: FileOutputStream fos = new FileOutputStream(dirDestino);
85: JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(fos);
86: JPEGEncodeParam parm = JPEGCodec
87: .getDefaultJPEGEncodeParam(img);
88: parm.setQuality(75, false);
89: enc.encode(img, parm);
90: fos.flush();
91: fos.close();
92: } catch (Exception e) {
93: System.out.println(e.getMessage());
94: e.printStackTrace();
95: }
96: System.out.println(System.currentTimeMillis() - start);
97: }
98: }
|