블로그 이미지
잡초 개발자의 좌충우돌 이야기 yysvip

카테고리

분류 전체보기 (211)
Java Programing (24)
Web Programing (20)
Database (36)
Tool (46)
까칠한 IT (27)
까칠한 정보 (42)
까칠한 Strory (16)
까칠한 Project (0)
Total906,262
Today3,745
Yesterday7,350

달력

« » 2018.10
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

공지사항

Image ThumbNail

  까칠한 남자 Strory       yysvip.tistory.com       잡초 개발자 까찰한 쑤의 좌충우돌 개발 이야기

 

The following Java source code for animated GIF processing may be freely used for any purpose. Unisys patent restrictions may apply to the LZW portions.

 

 

URL : http://fmsware.com/stuff/gif.html

package com.tistory.yysvip.image;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.tistory.yysvip.common.AnimatedGifEncoder;
import com.tistory.yysvip.common.GifDecoder;

⁄**
 * <pre>
 * com.tistory.yysvip.image
 *    |_ ThumbnailTest.java
 *
 * <⁄pre>
 * @date : XXXX. XX. XX 오전 11:50:11
 * @version :
 * @author : 까칠한쑤
 *⁄
public class ThumbnailTest {

    private Logger log = Logger.getLogger(this.getClass());

    public void createThumbnail(String load, String save, String type, int w, int h) {

        try {
            BufferedInputStream stream_file = new BufferedInputStream(new FileInputStream(load));
            createThumbnail(stream_file, save, type, w, h);
        } catch (Exception e) {
            log.error(e);
        }
    }

    public void createThumbnail(BufferedInputStream stream_file, String save, String type, int w, int h) {

        try {
            if (StringUtils.equals(StringUtils.lowerCase(type), "gif")) {
                getGifImageThumbnail(stream_file, save, type, w, h);
            } else {
                getImageThumbnail(stream_file, save, type, w, h);
            }

        } catch (Exception e) {
            log.error(e);
        }
    }

    public void getImageThumbnail(BufferedInputStream stream_file, String save, String type, int w, int h) {

        try {
            File file = new File(save);
            BufferedImage bi = ImageIO.read(stream_file);

            int width = bi.getWidth();
            int height = bi.getHeight();

            if (w < width) {
                width = w;
            }
            if (h < height) {
                height = h;
            }

            BufferedImage bufferIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            Image atemp = bi.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);

            Graphics2D g2 = bufferIm.createGraphics();
            g2.drawImage(atemp, 0, 0, width, height, null);
            ImageIO.write(bufferIm, type, file);
        } catch (Exception e) {
            log.error(e);
        }
    }

    public void getGifImageThumbnail(BufferedInputStream stream_file, String save, String type, int w, int h) {

        GifDecoder dec = new GifDecoder();
        AnimatedGifEncoder enc = new AnimatedGifEncoder();
        dec.read(stream_file);

        int cnt = dec.getFrameCount();

        int delay = 0;
        int width = 0;
        int height = 0;

        try {
            enc.start(save);
            enc.setRepeat(0);

            for (int i = 0; i < cnt; i++) {
                BufferedImage frame = dec.getFrame(i);
                delay = dec.getDelay(i);

                width = frame.getWidth();
                height = frame.getHeight();
                if (w < width) {
                    width = w;
                }
                if (h < height) {
                    height = h;
                }

                BufferedImage destimg = new BufferedImage(width, height,
                        BufferedImage.TYPE_4BYTE_ABGR);
                Graphics2D g = destimg.createGraphics();

                g.drawImage(frame, 0, 0, width, height, null);
                enc.setDelay(delay);

                enc.addFrame(destimg);
            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
            enc.finish();
        } catch (Exception ex) {
            log.error(ex);
        }
    }

    public static void main(String[] args) {
        ⁄⁄ TODO Auto-generated method stub
        String load = "D:⁄temp⁄test.jpg";
        String save = "D:⁄temp⁄s_test.jpg";
        String type = "jpg";

        int w = 100;
        int h = 100;

        ThumbnailTest thumbnailTest = new ThumbnailTest();
        thumbnailTest.createThumbnail(load, save, type, w, h);
    }
}
Posted by yysvip

최근에 달린 댓글

최근에 받은 트랙백

글 보관함