1 /**
2 * Copyright 2008 WebPhotos
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.webphotos.util.legacy;
17
18 import com.sun.image.codec.jpeg.*;
19 import java.awt.*;
20 import java.awt.image.*;
21 import java.io.*;
22 import javax.swing.ImageIcon;
23
24 /**
25 * Responsável pelas funções relacionadas a alterações de fotos. Armazena um
26 * método para reajustar o tamanho da foto.
27 *
28 * @author Guilherme
29 */
30 public class Photo {
31
32 /**
33 * Redimensiona o tamanho da imagem, alterando o arquivo.
34 *
35 * @param original Nome do arquivo original.
36 * @param resized Nome do arquivo redimensionado.
37 * @param maxSize Tamanho máximo.
38 */
39 public static void resize(String original, String resized, int maxSize) {
40 try {
41 File originalFile = new File(original);
42 ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
43 Image i = ii.getImage();
44 Image resizedImage;
45 int iWidth = i.getWidth(null);
46 int iHeight = i.getHeight(null);
47 if (iWidth > iHeight) {
48 resizedImage = i.getScaledInstance(maxSize, (maxSize * iHeight) / iWidth, Image.SCALE_SMOOTH);
49 } else {
50 resizedImage = i.getScaledInstance((maxSize * iWidth) / iHeight, maxSize, Image.SCALE_SMOOTH);
51 }
52 // This code ensures that all the
53 // pixels in the image are loaded.
54 Image temp = new ImageIcon(resizedImage).getImage();
55
56 // Create the buffered image.
57 BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
58
59 // Copy image to buffered image.
60 Graphics g = bufferedImage.createGraphics();
61 // Clear background and paint the image.
62 g.setColor(Color.white);
63 g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
64 g.drawImage(temp, 0, 0, null);
65 g.dispose();
66
67 // sharpen
68 float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0};
69 Kernel kernel = new Kernel(3, 3, sharpenArray);
70 ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
71 bufferedImage = cOp.filter(bufferedImage, null);
72
73 /* write the jpeg to a file */
74 File file = new File(resized);
75 FileOutputStream out = new FileOutputStream(file);
76
77 /* encodes image as a JPEG data stream */
78 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
79 com.sun.image.codec.jpeg.JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
80
81 // writeParam = new JPEGImageWriteParam(null);
82 // writeParam.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
83 // writeParam.setProgressiveMode(JPEGImageWriteParam.MODE_DEFAULT);
84
85 param.setQuality(0.7f, true);
86 encoder.setJPEGEncodeParam(param);
87 encoder.encode(bufferedImage);
88
89 } catch (Exception e) {
90 System.out.println(e.getMessage());
91 }
92
93 }
94 }