2016년 12월 25일 일요일
[java]이미지 섬네일 생성하기 소스
나도 어디서 퍼왔는지 기억이 잘 나지 않는다. 포맷부분만 약간 수정 했다.
비율이 유지 되면서
thumbWidth 값과 thumbHeight 값은 알아서 셋팅 하면되고
int thumbWidth;
int thumbHeight;
final int RATIO = 0;
final int SAME = -1;
/**
* src 파일을 읽어 dest 파일에 전달 받은 format 형식으로 썸네일을 생성한다.
* 비율 유지
* 작성자 : 어셈블링블링
* @param src
* @param dest
* @param width
* @param height
* @throws IOException
*/
public void makeThumb(File src, File dest, String format) throws IOException {
Image srcImg = null;
String suffix = src.getName().substring(src.getName().lastIndexOf('.')+1).toLowerCase();
if (suffix.equals("bmp") || suffix.equals("png") || suffix.equals("gif")) {
srcImg = ImageIO.read(src);
} else {
// BMP가 아닌 경우 ImageIcon을 활용해서 Image 생성
// 이렇게 하는 이유는 getScaledInstance를 통해 구한 이미지를
// PixelGrabber.grabPixels로 리사이즈 할때
// 빠르게 처리하기 위함이다.
srcImg = new ImageIcon(src.toURL()).getImage();
}
int srcWidth = srcImg.getWidth(null);
int srcHeight = srcImg.getHeight(null);
int destWidth = -1, destHeight = -1;
if (thumbWidth == SAME) {
destWidth = srcWidth;
} else if (thumbWidth > 0) {
destWidth = thumbWidth;
}
if (thumbHeight == SAME) {
destHeight = srcHeight;
} else if (thumbHeight > 0) {
destHeight = thumbHeight;
}
if (thumbWidth == RATIO && thumbHeight == RATIO) {
destWidth = srcWidth;
destHeight = srcHeight;
} else if (thumbWidth == RATIO) {
double ratio = ((double)destHeight) / ((double)srcHeight);
destWidth = (int)((double)srcWidth * ratio);
} else if (thumbWidth == RATIO) {
double ratio = ((double)destWidth) / ((double)srcWidth);
destHeight = (int)((double)srcHeight * ratio);
}
Image imgTarget = srcImg.getScaledInstance(destWidth, destHeight, Image.SCALE_SMOOTH);
int pixels[] = new int[destWidth * destHeight];
PixelGrabber pg = new PixelGrabber(imgTarget, 0, 0, destWidth, destHeight, pixels, 0, destWidth);
try {
pg.grabPixels();
} catch (InterruptedException e) {
throw new IOException(e.getMessage());
}
BufferedImage destImg = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
destImg.setRGB(0, 0, destWidth, destHeight, pixels, 0, destWidth);
ImageIO.write(destImg, format, dest);
}
퍼갈때 댓글 하나씩 남겨주삼.
피드 구독하기:
댓글 (Atom)
[lunux]리눅스 폴더별 용량 확인
리눅스 폴더별 용량 확인 조회 하고자 하는 디렉토리 리스트가있는 경로로 이동후 du -h --max-depth=1
-
pom.xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId&g...
-
수십대의 서버에 특정 쉘을 실행한다거나 파일을 수정해야할경우 호스트 입력 아이디 입력 패스워드 입력은 여간 짜증나는일이 아닐수 없다. 이를 한방에 해주는 방법 teraterm 를 설치한다( putty 는 버리자 ) 예를 들면 19...
-
다른 페이지로 이동 해서 프로세스 실행후 다시 이전 페이지로 돌아와야할경우 page url 을 파라미터로 넘겨야 하는데 location.protocol : 프로토콜 예) https: location.host : 도메인 예) naver.com l...
댓글 없음:
댓글 쓰기