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();
   }
}

댓글 없음:

댓글 쓰기

시스템 부팅시 도커 컨테이너 자동 실행

Docker 컨테이너를 운용중인 시스템이 Reboot 되버리면 컨테이너가 자동으로 올라오지 않아 불편해서 시스템 리붓시 컨테이너를 자동으로 시작되게 init 데몬에 등록하기로 했습니다. 서버는 Ubuntu 17.10 Docker는 17.0...