Thumbnamil 이미지 만들기
Posted 2007. 11. 2. 21:53Thumbnail이미지는 jdk에도 포함될 만큼 많이 또 쉽게 구현하여 사용할수 있다.
그래서...
=========================== TestThumnamil.java ===========================
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class TestThumbnail
{
public static final int VERTICAL = 0;
public static final int HORIZONTAL = 1;
public static final String IMAGE_JPEG = "jpeg";
public static final String IMAGE_JPG = "jpg";
public static final String IMAGE_GIF = "gif";
public static final String IMAGE_PNG = "png";
private ImageIcon image;
private ImageIcon thumb;
public TestThumbnail()
{
}
/*
public TestThumbnail(Image image)
{
this.image = new ImageIcon(image);
}
*/
/*
public TestThumbnail(String fileName)
{
image = new ImageIcon(fileName);
}
*/
public void setImageFile(String fileName)
{
image = new ImageIcon(fileName);
}
public Image getThumbnail(int size, int dir)
{
if (dir == HORIZONTAL)
{
thumb = new ImageIcon(
image.getImage().getScaledInstance(
size, -1, Image.SCALE_SMOOTH)
);
}
else
{
thumb = new ImageIcon(
image.getImage().getScaledInstance(
-1, size, Image.SCALE_SMOOTH)
);
}
return thumb.getImage();
}
public Image getThumbnail(int size, int dir, int scale)
{
if (dir == HORIZONTAL)
{
thumb = new ImageIcon(
image.getImage().getScaledInstance(size, -1, scale)
);
}
else
{
thumb = new ImageIcon(
image.getImage().getScaledInstance(-1, size, scale)
);
}
return thumb.getImage();
}
public void saveThumbnail(File file, String imageType)
{
if (thumb != null)
{
BufferedImage bi = new BufferedImage(
thumb.getIconWidth(),
thumb.getIconHeight(),
BufferedImage.TYPE_INT_RGB
);
Graphics g = bi.getGraphics();
g.drawImage(thumb.getImage(), 0, 0, null);
try
{
ImageIO.write(bi, imageType, file);
}
catch (IOException ioe)
{
System.out.println("Error occured saving thumbnail");
}
}
else
{
System.out.println("Thumbnail has not yet been created");
}
}
public void makeThumbnail(String oriFileName) {
setImageFile(oriFileName);
File f = new File(oriFileName);
getThumbnail(100, TestThumbnail.HORIZONTAL);
System.out.println(oriFileName.substring(0,oriFileName.indexOf(".")));
String [] fileArr = oriFileName.split(".");
//String [] fileArr = oriFileName.split(".");
System.out.println(oriFileName+","+fileArr.length);
String imageNamePath = oriFileName.substring(0,oriFileName.indexOf("."));
String imageExt = oriFileName.substring(oriFileName.indexOf(".")+1, oriFileName.length());
String IMG_TYPE = "";
if(imageExt.equalsIgnoreCase("jpg"))
IMG_TYPE = TestThumbnail.IMAGE_JPG;
if(imageExt.equalsIgnoreCase("jpeg"))
IMG_TYPE = TestThumbnail.IMAGE_JPEG;
if(imageExt.equalsIgnoreCase("gif"))
IMG_TYPE = TestThumbnail.IMAGE_GIF;
saveThumbnail(new File(imageNamePath+"_thumb."+imageExt), IMG_TYPE);
}
public static void main(String [] args)
{
TestThumbnail ct = new TestThumbnail();
ct.makeThumbnail("d:/james_start2.jpg");
/*
ct.setImageFile("d:/james_start2.jpg");
ct.getThumbnail(100, TestThumbnail.HORIZONTAL);
ct.saveThumbnail(new File("d:/james_start2_thumb.jpg"), TestThumbnail.IMAGE_JPEG);
*/
}
}
'개발노트' 카테고리의 다른 글
String을 Date로 변환시 포맷형태 정리. (0) | 2009.02.17 |
---|---|
제 9회 JCO 컨퍼런스...기대된다. (1) | 2008.01.29 |
java의 Checked Exception vs UnChecked Exception... (0) | 2007.11.01 |
[링크]java Collection Framework 에 대한 설명..... (0) | 2007.10.08 |
Hashtable vs HashMap vs LinkedHashMap (0) | 2007.10.08 |
- Filed under : 개발노트