1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
26
27
28
29
30 public class Photo {
31
32
33
34
35
36
37
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
53
54 Image temp = new ImageIcon(resizedImage).getImage();
55
56
57 BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
58
59
60 Graphics g = bufferedImage.createGraphics();
61
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
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
74 File file = new File(resized);
75 FileOutputStream out = new FileOutputStream(file);
76
77
78 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
79 com.sun.image.codec.jpeg.JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
80
81
82
83
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 }