티스토리 뷰

안드로이드에서 이미지를 사용하기 위해서, 보통 Bitmap 클래스를 사용하게 됩니다. 안드로이드 앱에서 이미지에 대한 Thumbnail이나 이미지의 부분을 자르기 위해서는 Bitmap 클래스로 원하는 사이즈나 원하는 부분을 쉽게 잘라낼 수 있습니다.

아래의 BitmapUtil 클래스는, Bitmap 클래스를 이용해서 이미지를 줄이거나 늘리는 기능을 제공하는 유틸리티 클래스입니다. 아래의 기능정도면, 정말 특이한 요구사항(우측아래를 기준으로 100×100 이미지를 가져와라, 이런건 간단하게 아래의 cropCenter 메쏘드를 약간 변경하면 쉽게 적용)이 없다면, 아래의 유틸리티 클래스만으로도 상당히 많은 부분을 커버할 수 있을것 같습니다.






import android.graphics.Bitmap;

/**
 * BitmapUtil Class
 *
 * @Author : mcsong@gmail.com
 * @Date : Mar 11, 2012 9:59:18 AM
 * @Version : 1.0.0
 */
public class BitmapUtil {   
    /**
     * Bitmap을 ratio에 맞춰서 max값 만큼 resize한다.
     * 
     * @param Bitmap 원본
     * @param max 원하는 크기의 값
     * @return
     */
    public static Bitmap resizeBitmap(Bitmap src, int max) {
        if(src == null)
            return null;
       
        int width = src.getWidth();
        int height = src.getHeight();
        float rate = 0.0f;
       
        if (width > height) {
            rate = max / (float) width;
            height = (int) (height * rate);
            width = max;
        } else {
            rate = max / (float) height;
            width = (int) (width * rate);
            height = max;
        }

        return Bitmap.createScaledBitmap(src, width, height, true);           
    }
   
    /**
     * Bitmap을 ratio에 맞춰서 max값 만큼 resize한다.
     *
     * @param src
     * @param max
     * @param isKeep 작은 크기인 경우 유지할건지 체크.. 
     * @return
     */
    public static Bitmap resize(Bitmap src, int max, boolean isKeep) {
        if(!isKeep)
            return resizeBitmap(src, max);
       
        int width = src.getWidth();
        int height = src.getHeight();
        float rate = 0.0f;
       
        if (width > height) {
            if (max > width) {
                rate = max / (float) width;
                height = (int) (height * rate);
                width = max;
            }
        } else {
            if (max > height) {
                rate = max / (float) height;
                width = (int) (width * rate);
                height = max;
            }
        }

        return Bitmap.createScaledBitmap(src, width, height, true);
    }
   
    /**
     * Bitmap 이미지를 정사각형으로 만든다.
     *
     * @param src 원본
     * @param max 사이즈
     * @return
     */
    public static Bitmap resizeSquare(Bitmap src, int max) {
        if(src == null)
            return null;
       
        return Bitmap.createScaledBitmap(src, max, max, true);
    }
   
   
    /**
     * Bitmap 이미지를 가운데를 기준으로 w, h 크기 만큼 crop한다.
     *
     * @param src 원본
     * @param w 넓이
     * @param h 높이
     * @return
     */
    public static Bitmap cropCenterBitmap(Bitmap src, int w, int h) {
        if(src == null)
            return null;
       
        int width = src.getWidth();
        int height = src.getHeight();
               
        if(width < w && height < h)
            return src;
       
        int x = 0;
        int y = 0;
       
        if(width > w)
            x = (width – w)/2;
       
        if(height > h)
            y = (height – h)/2;
       
        int cw = w; // crop width
        int ch = h; // crop height
       
        if(w > width)
            cw = width;
       
        if(h > height)
            ch = height;
       
        return Bitmap.createBitmap(src, x, y, cw, ch);
    }
   
}


출처 : http://sjava.net/?p=366#comments


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함