자바로 만든 외부URL에서 이미지 불러와 썸네일이미지 생성소스 :: JAVA 공작소[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

JAVA 공작소
[1]
등록일:2008-04-09 23:05:28 (0%)
작성자:
제목:자바로 만든 외부URL에서 이미지 불러와 썸네일이미지 생성소스
import  java.awt.Graphics2D;
import  java.awt.Image;
import  java.awt.image.BufferedImage;
import  java.io.File;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.io.InputStream;
import  java.net.URL;
import  java.net.URLConnection;
import  java.text.DecimalFormat;

import  javax.imageio.ImageIO;
import  javax.media.jai.JAI;
import  javax.media.jai.RenderedOp;

class  thumbNail_v1_Sub  {

        Image  imgA;
        URL  imageUrl;
        public  double  max_width  =  100d;
        public  double  max_height  =  140d;
        public  double  ratio_width;
        public  double  ratio_height;
        public  double  ratio;
        public  int  stand;
        public  int  src_x;
        public  int  src_y;
        public  int  dst_width;        
        public  int  dst_height;
        
        

        public  String  getType()  {
                String  strType  =  null;
                try  {
                        URLConnection  urlLinkConnection  =  imageUrl.openConnection();
                        urlLinkConnection.setAllowUserInteraction(false);
                        InputStream  in  =  imageUrl.openStream();
                        strType  =  urlLinkConnection.guessContentTypeFromStream(in);
                        return  strType;
                }  catch  (Exception  me)  {
                }
                return  strType;

        }

        public  int  getImageSize()  {
                int  lenSize  =  0;
                try  {
                        InputStream  in  =  imageUrl.openStream();
                        FileOutputStream  out  =  new  FileOutputStream("C:\\temp.jpg");

                        byte[]  buf  =  new  byte[1024];
                        int  len;
                        lenSize  =  0;
                        while  ((len  =  in.read(buf))  >  0)  {
                                lenSize  +=  len;
                                out.write(buf,  0,  len);
                        }
                        out.close();
                        in.close();
                        return  lenSize;
                }  catch  (Exception  me)  {
                }
                return  lenSize;
        }
        
        public  void  createImage(int  zoom)  throws  IOException  //  썸네일  이미지를  생성하는  메서드
        {
                File  check_file  =  new  File(imageUrl.toString());
                File  save  =  new  File("c:\\"  +  check_file.getName());  //  썸네일  이미지  파일
                System.out.println("c:\\"  +  check_file.getName());
                RenderedOp  rOp  =  JAI.create("fileload",  "C:\\temp.jpg");  //  loadFile는                
                BufferedImage  im  =  rOp.getAsBufferedImage();
                
                ratio_width  =  im.getWidth()/max_width;
                ratio_height  =  im.getHeight()/max_height;
                
                if(ratio_width  <  ratio_height){
                        ratio  =  ratio_width;
                        stand  =  1;
                }else{
                        ratio  =  ratio_height;
                        stand  =  2;
                }

                if(ratio  >  1){

                        dst_width  =  (int)  Math.round((im.getWidth()/ratio));
                        dst_height  =  (int)  Math.round((im.getHeight()/ratio));

                }else{

                        dst_width  =  im.getWidth();
                        dst_height  =  im.getHeight();        

                }                

                int  width  =  dst_width;
                int  height  =  dst_height;
                
                System.out.println(im.getWidth());
                System.out.println(im.getHeight());
                System.out.println(ratio);
                System.out.println(width);
                System.out.println(height);
                
                Image  imgTarget  =  im.getScaledInstance(width,  height,  Image.SCALE_SMOOTH);                

                BufferedImage  thumb  =  new  BufferedImage(width,  height,
                                BufferedImage.TYPE_INT_RGB);  //  메모리에  이미지  공간을  생성                
                
                Graphics2D  g2  =  thumb.createGraphics();                
                g2.drawImage(imgTarget,  0,  0,  width,  height,  null);
                ImageIO.write(thumb,  "jpg",  save);  //  메모리에  그린이미지를  파일로  저장
        }

}

public  class  thumbNail_v1  {
        public  static  void  main(String[]  ar)  {

                thumbNail_v1_Sub  thumbNail  =  new  thumbNail_v1_Sub();

                try  {

                        thumbNail.imageUrl  =  new  URL(
                                        "http://ssiso.net/cafe/club/club1/board3/image/thumb/thumb640_javaStudy03052014270.jpg");

                        System.out.println(thumbNail.getType());
                        System.out.println(thumbNail.getImageSize());
                        //  try  {
                        //  Thread.sleep(10000);
                        //  }  catch  (InterruptedException  exc)  {
                        //  }
                        thumbNail.createImage(5);

                }  catch  (Exception  e)  {
                        //  TODO  Auto-generated  catch  block
                        e.printStackTrace();
                }

        }
}

먼저  JAI  API  다운로드를  해야합니다.  
다운로드  하는  방법은  요기  참조하세여
http://ssiso.net/cafe/club/club1/board1/content.php?board_code=javaStudy|javaNormal&idx=7267
&club=javaStudy&cp=1&cb=1&search=&search_word=

지금  집에서  진행중인  홈페이지에  필요해서  만들어  봤습니다.
여러  다른분들이  만드신  소스를  보구  짬뽕해서  만들어  봤는데,  
많이  부족할  꺼에요..
그래도  속도는  안나와도  됐다는데  만족합니다  ^^;

아~  그리고  일단  다운로드  받은후  썸네일을  만들도록  했는데  
그냥  다이렉트로  만드는  방법이  있는지  모르겠네요
암만  찾아봐도  못찾겠어서요...
혹시  아시는분은  댓글좀  부탁드려요

다음에는  인터넷  URL  에서  소스를  가져와  이미지  부분을  뽑아  썸네일  이미지를  만드는  소스를  올리도록  하겠습니다.

그럼  수고하세요...
[2008년  04월  09일  23:12:52  수정되었습니다.]
[본문링크] 자바로 만든 외부URL에서 이미지 불러와 썸네일이미지 생성소스
[1]
코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=7353
작성자
비밀번호

 

SSISOCommunity

[이전]

Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.