728x90

이미지 다운로드하는 파일을 구해서 테스트 해보니 사진 이미지 크기가 일정하지 않는 문제점이 있다.

서버에서 읽어들이는 파일 사이즈 크기를 알아보기 위해서

System.out.println("Original Photo Image width ==="+ width_tmp);

System.out.println("Original Photo Image height ==="+ height_tmp);

를 추가해서 size 를 확인한다.


 private Bitmap decodeFile(File f){
    try {
        //decode image size
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,options);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE= 70;
        int width_tmp=options.outWidth;
        int height_tmp=options.outHeight;
        System.out.println("Original Photo Image width ==="+ width_tmp);
        System.out.println("Original Photo Image height ==="+ height_tmp);
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}


수정한 코드

private Bitmap decodeFile(File f){
    try {
            //decode image size
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,options);

            //Find the correct scale value. It should be the power of 2.
            // final int REQUIRED_SIZE= 70;
            int width_tmp=options.outWidth;
            int height_tmp=options.outHeight;
            System.out.println("Original Photo Image width ==="+ width_tmp);
            System.out.println("Original Photo Image height ==="+ height_tmp);

            /***********수정 코드 *********************************************/

            // 높이 160 은 원하는 크기로 수정 사용하면 됨
            int width = (int)(width_tmp * 160.0 / height_tmp);
            Bitmap orgImage = BitmapFactory.decodeStream(new FileInputStream(f));
            return  Bitmap.createScaledBitmap(orgImage, width, 160,true);
    } catch (FileNotFoundException e) {}
    return null;
}


이미지 처리 성능 향상을 시키기 위해 가장 먼저 이미지의 크기를 확인하자.
앱에서 사용자에게 보여주는 사진 이미지는 매우 작아 해상도가 좋을 필요가 없으므로 서버에서 크기를 작게 만들고 압축률을 변경하면 클라이언트로 전달되는 파일 크기가 작아져 전송속도가 빨라지고, 체감속도가 증가한다.


ImageView의 setImageResource() 메소드 사용을 피하자
이 메소드를 사용하면 이미지를 읽고 디코딩 하는 작업을 UI Thread에서 하기 때문에 응답시간이 느려진다.

setImageDrawable 메소드나 setImageBitmap 메소드를 사용하고, BitmapFactory 클래스를 사용하는 것이 좋다.



이미지 다루는 파일 전체 소스는 CustomListView 게시글(http://link2me.tistory.com/1255)에도 있고 http://www.androidbegin.com/tutorial/android-json-parse-images-and-texts-tutorial/ 에서도 구할 수 있음


이미지 로딩 라이브러리 Glide 사용하면 위와 같은 걸 고민할 필요가 없더라.

http://link2me.tistory.com/1498 참조


블로그 이미지

Link2Me

,