1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.isenshi.util;
17
18 import java.awt.geom.AffineTransform;
19 import java.awt.image.AffineTransformOp;
20 import java.awt.image.BufferedImage;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.util.Iterator;
25
26 import javax.imageio.ImageIO;
27 import javax.imageio.ImageReadParam;
28 import javax.imageio.ImageReader;
29 import javax.imageio.stream.ImageInputStream;
30
31 public class ImageUtils {
32
33 public static void createThumbnail(InputStream is, OutputStream os,
34 int thumbnailWidth, String formatName) throws IOException {
35
36 ImageReader reader = getImageReader(is,formatName);
37 if(reader == null){
38 throw new IOException("failed to get ImageReader");
39 }
40 int srcWidth = reader.getWidth(0);
41 double rate = getRate(srcWidth,thumbnailWidth);
42 BufferedImage thumbnailImage = getSamplingImage(reader,rate);
43 ImageIO.write(thumbnailImage, formatName, os);
44 }
45
46 public static void createThumbnailAffine(InputStream is, OutputStream os,
47 int thumbnailWidth, String formatName) throws IOException {
48
49 ImageReader reader = getImageReader(is,formatName);
50 if(reader == null){
51 throw new IOException("failed to get ImageReader");
52 }
53 int srcWidth = reader.getWidth(0);
54 double rate = getRate(srcWidth,thumbnailWidth);
55 BufferedImage thumbnailImage = getShrinkImage(reader,rate);
56 ImageIO.write(thumbnailImage, formatName, os);
57 }
58
59
60 public static BufferedImage getShrinkImage(ImageReader reader, double rate) throws IOException{
61 BufferedImage shrinkImage = null;
62 BufferedImage srcImage = reader.read(0);
63 int srcWidth = srcImage.getWidth();
64 int srcHeight = srcImage.getHeight();
65 int targetWidth = (int)(srcWidth*rate);
66 int targetHeight = (int)(srcHeight*rate);
67
68 if(targetWidth == 0){
69 targetWidth = 1;
70 }
71 if(targetHeight == 0){
72 targetHeight = 1;
73 }
74
75 shrinkImage = new BufferedImage(targetWidth,targetHeight,srcImage.getType());
76 AffineTransformOp atOp = new AffineTransformOp(AffineTransform.getScaleInstance(rate,rate),null);
77 atOp.filter(srcImage,shrinkImage);
78 return shrinkImage;
79 }
80
81
82 public static BufferedImage getSamplingImage(ImageReader reader, double rate) throws IOException{
83 BufferedImage samplingImage = null;
84 int samplingRate = (int)(1/rate);
85 if(samplingRate < 1){
86 samplingRate = 1;
87 }
88 ImageReadParam param = reader.getDefaultReadParam();
89 param.setSourceSubsampling(samplingRate,samplingRate,0,0);
90 samplingImage = reader.read(0,param);
91 return samplingImage;
92 }
93
94 public static ImageReader getImageReader(InputStream is, String formatName){
95 ImageInputStream iis = null;
96 Iterator i = null;
97 try{
98 iis = ImageIO.createImageInputStream(is);
99 i = ImageIO.getImageReaders(iis);
100 }catch(IOException ioe){
101 i = ImageIO.getImageReadersByFormatName(formatName);
102 }
103
104 if(i.hasNext()){
105 ImageReader reader = (ImageReader)i.next();
106 reader.setInput(iis);
107 return reader;
108 }
109 return null;
110 }
111
112
113 public static double getRate(int srcWidth, int destWidth){
114 double destWidthD = destWidth;
115 double srcWidthD = srcWidth;
116
117 double rate = destWidthD/srcWidthD;
118 if(rate > 1){
119 rate = 1;
120 }
121 return rate;
122 }
123 }