import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class EvinPotoSmall
{
private String orig; // 원본이미지 (패스와 파일명)
private String thumb; // 작게만들 이미지 (패스와 파일명)
private int maxDim; // 사이즈
// 작은 이미지를 만든다.(파일이저장된 실제경로, 새로운이름, 가로사이즈)
public void createThumbnail(String orig, String thumb, int maxDim)
{
try {
// Get the image from a file.
Image inImage = new ImageIcon(
orig).getImage();
// Determine the scale.
double scale = (double)maxDim/(double)inImage.getHeight(null);
if (inImage.getWidth(null) > inImage.getHeight(null)) {
scale = (double)maxDim/(double)inImage.getWidth(null);
}
// Determine size of new image.
//One of them
// should equal maxDim.
int scaledW = (int)(scale*inImage.getWidth(null));
int scaledH = (int)(scale*inImage.getHeight(null));
// Create an image buffer in
//which to paint on.
BufferedImage outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);
// Set the scale.
AffineTransform tx = new AffineTransform();
// If the image is smaller than
//the desired image size,
// don't bother scaling.
if (scale < 1.0d) {
tx.scale(scale, scale);
}
// Paint image.
Graphics2D g2d = outImage.createGraphics();
g2d.drawImage(inImage, tx, null);
g2d.dispose();
// JPEG-encode the image
//and write to file.
OutputStream os = new FileOutputStream(thumb);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(outImage);
os.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
======================================================================================================================
EvinPotoSmall small = new EvinPotoSmall();
String imgpath = "C:\\up\\thum\\test.gif";
small.createThumbnail( imgpath, "test_thum.gif ", 120 );
'Java' 카테고리의 다른 글
Apache Commons DBCP 환경설정 값 (0) | 2015.06.10 |
---|---|
성능좋은 file 입출력(파일 이동) (0) | 2009.12.02 |
Collections를 이용한 List 객체들의 Sorting(정렬) [펌] (0) | 2009.11.26 |
불필요한 코딩을 줄이자!(IBM developerWorks) (0) | 2009.04.09 |