2011년 6월 9일 목요일

Nail 이미지 생성 클래스

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;   
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
* <pre>
* 사용법 java nailImgMaker [inPutFileName] [outPutFileName] [이미지 가로크기 픽셀단위] [화질 0.0F~1.0F]
* 예) java nailImgMaker craftlee.gif nail_craftlee.jpg 150 0.5F
*
* outPutFile은 JPG파일만 됩니다.
* </pre>
*/

public class nailImgMaker {
   public final MediaTracker tracker = new MediaTracker( new Component() {} );

   public Image setSize( Image image, int width, int height ) {
       return setSize( image, width, height, java.awt.Image.SCALE_DEFAULT );
   }

   public Image setSize( Image image, int width, int height, int hints ) {
       return image.getScaledInstance( width, height, hints );
   }



   public void checkImage( Image image )
   throws InterruptedException {
       waitForImage( image );
       int imageWidth = image.getWidth( null );
       int imageHeight = image.getHeight( null );

       if ( imageWidth < 1 || imageHeight < 1)
           throw new IllegalArgumentException( "File is Not ImageFile" );
   }

   private void waitForImage( Image image )
   throws InterruptedException {
       tracker.addImage( image, 0 );
       tracker.waitForID( 0 );
       tracker.removeImage(image, 0);
   }

   public void encodeJPEG( OutputStream outputStream,
                           Image outputImage,
                           float outputQuality )
   throws IOException {

       int outputWidth  = outputImage.getWidth( null );
       int outputHeight = outputImage.getHeight( null );
       BufferedImage bi = new BufferedImage( outputWidth, outputHeight,
       BufferedImage.TYPE_INT_RGB );                                                   
       Graphics2D biContext = bi.createGraphics();
       biContext.drawImage( outputImage, 0, 0, null );
       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( outputStream );
       JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam( bi );
       jep.setQuality( outputQuality, true );
       encoder.encode( bi, jep );
       outputStream.flush();      
   }
   
   public static void main( String [] args ) {
       if (args.length < 4) {
           System.out.println( "use: java ImageSizer inputFile outputFile outputWidth outputQuality" );
           System.exit(0);
       }

       String inputFileName  = args[0];
       String outputFileName = args[1];

       int outputWidth = Integer.parseInt( args[2] );
       float outputQuality = Float.parseFloat( args[3] );

       try {
           new nailImgMaker().nailMake(inputFileName,
                                       outputFileName,
                                       outputWidth,
                                       outputQuality);
       } catch(Exception e) {
           e.printStackTrace();

       }
   }   

   public void nailMake(String inputFileName,
                        String outputFileName,
                        int outputWidth)
   throws IOException, InterruptedException {
       nailMake(inputFileName, outputFileName, outputWidth, 0.5F);
   }
   
   public void nailMake(String inputFileName,
                        int outputWidth)
   throws IOException, InterruptedException {
       int lastSlash = inputFileName.lastIndexOf(".");
       String outputFileName = inputFileName.substring( 0, lastSlash + 1 )+"_nail.jpg";

       nailMake(inputFileName, outputFileName, outputWidth, 0.5F);
   }
   
   public void nailMake(String inputFileName,
                        String outputFileName,
                        int outputWidth,
                        float outputQuality)
   throws IOException, InterruptedException {

       if ( outputWidth < 1 )
           throw new IllegalArgumentException( "output width is >= 1" );
    
       if (( outputQuality < 0.0F ) || ( outputQuality > 1.0F ))
           throw new IllegalArgumentException( "0.0F <= output quality <= 1.0F " );
   
       Image inputImage = Toolkit.getDefaultToolkit().getImage( inputFileName );
       checkImage( inputImage );
      
       saveJPG(setSize(inputImage, outputWidth, -1), outputQuality, outputFileName);
   }

   private void saveJPG(Image outputImage,
                        float outputQuality,
                        String outputFileName)
   throws FileNotFoundException,
          IOException {
       FileOutputStream fos = new FileOutputStream(outputFileName);
       encodeJPEG(fos, outputImage, outputQuality);
       fos.close();
   }
}

댓글 없음:

댓글 쓰기

ETL 솔루션 환경

ETL 솔루션 환경 하둡은 대용량 데이터를 값싸고 빠르게 분석할 수 있는 길을 만들어줬다. 통계분석 엔진인 “R”역시 하둡 못지 않게 관심을 받고 있다. 빅데이터 역시 데이터라는 점을 볼때 분산처리와 분석 그 이전에 데이터 품질 등 데이...